0

-This is my code:

private void loadList() {
        try {
            List tasks = (List) Reader.readTasks(new File(SAVE_DESTINATION));
            String firstTask = tasks.getItem(0);
            listModel.addElement(firstTask);

        } catch (IOException | ClassNotFoundException e) {
            System.out.println("Unable to load tasks from " + SAVE_DESTINATION);
            e.printStackTrace();
        }
    }

-I am getting this error:

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.awt.List

  • (My saved list will be longer than 1 item long, above I was trying to see if I could at least get the first element in the list to load up.)

  • I have already got save working so I can save a to do list. I want to be able to load the saved to do list when the program starts. Can anyone help??

  • 1
    Yes, change your import from `java.awt.List` to `java.util.List` – Oleg Apr 01 '20 at 20:38
  • or be much more specific in your casting: `java.util.List tasks = (java.util.List) Reader.readTasks(new File(SAVE_DESTINATION));` – Hovercraft Full Of Eels Apr 01 '20 at 20:40
  • this is what I am getting now, any suggestion would be greatly appreciated: - Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index: 1, Size: 1 – Vikaram Varpaul Apr 01 '20 at 22:06
  • private void loadList() { try { List tasks = Reader.readTasks(new File(SAVE_DESTINATION)); for (int i = 0; i < tasks.size(); i++) { listModel.addElement(tasks.get(i).toString()); } PlayMusic playMusic = new PlayMusic(LOAD_TASK_SOUND); JOptionPane.showMessageDialog(null, "Loaded From: " + SAVE_DESTINATION); } catch (IOException | ClassNotFoundException e) { System.out.println("Unable to load tasks from " + SAVE_DESTINATION); e.printStackTrace(); } } – Vikaram Varpaul Apr 01 '20 at 22:32
  • it is weird because im only getting that error sometimes... – Vikaram Varpaul Apr 01 '20 at 22:33

0 Answers0