Note that there are some explanatory texts on larger screens.

plurals
  1. POWeird error when using foreach loop in java
    primarykey
    data
    text
    <p>I am working on a little todolist-program and i'm getting a weird bug that i never had before. I have 4 classes: 1 POJO class that contains the todo-data:</p> <pre><code>public class Todo implements Comparable { private String title; private String task; private boolean done; public Todo(String title, String task) { this.title = title; this.task = task; } public String getTitle() { return title; } public void setTitle(String newTitle) { title = newTitle; } public String getTask() { return task; } public void setTask(String newTask) { task = newTask; } public boolean isDone() { return done; } public void setDone(boolean isDone) { done = isDone; } public int compareTo(Object obj) { Todo todo = (Todo) obj; return getTitle().compareTo(todo.getTitle()); } public String toString() { StringBuilder sb = new StringBuilder(); sb.append("Todo {\n"); sb.append("Title: \""); sb.append(getTitle() + "\";\n"); sb.append("Task: \""); sb.append(getTask() + "\";\n"); sb.append("}"); return sb.toString(); } } </code></pre> <p>Then I have a class that stores and loads my todos:</p> <pre><code>import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.List; import com.google.gson.Gson; import com.google.gson.GsonBuilder; public class ListStorage { Gson gson = new GsonBuilder().setPrettyPrinting().create(); FileWriter writer; BufferedReader reader; public void storeList(List list, String filename) throws IOException { String json = gson.toJson(list); writer = new FileWriter(filename); writer.write(json); writer.close(); } public List loadList(String filename) throws FileNotFoundException { reader = new BufferedReader(new FileReader(filename)); List list = gson.fromJson(reader, List.class); return list; } } </code></pre> <p>Then I have a 'Manager' class that is basically my controller:</p> <pre><code>import java.util.ArrayList; import java.util.Collections; import java.util.List; public class Manager { private List&lt;Todo&gt; todos = new ArrayList&lt;&gt;(); private ListStorage storage = new ListStorage(); public List getTodos() { return todos; } public void setTodos(List newTodos) { todos = newTodos; } public ListStorage getStorage() { return storage; } public void add(String title, String task) { todos.add(new Todo(title, task)); sort(); try { storage.storeList(todos, "todos"); } catch(Exception e) { e.printStackTrace(); } } public void remove(int index) { todos.remove(index); sort(); try { storage.storeList(todos, "todos"); } catch(Exception e) { e.printStackTrace(); } } private void sort() { Collections.sort(todos); } } </code></pre> <p>And finally there is my main-class for testing my code (The bug seems to be here):</p> <pre><code>class CLITodo { public static void main(String[] args) { Manager man = new Manager(); man.add("Hello", "Bye"); man.add("Foo", "Bar"); try { man.setTodos(man.getStorage().loadList("todos")); } catch(Exception e) { } java.util.List&lt;Todo&gt; todos = man.getTodos(); for (Todo t : todos) { System.out.println(t); } } } </code></pre> <p>The error message I get when I leave the <code>&lt;Todo&gt;</code> in CLITodo class is:</p> <pre><code>Exception in thread "main" java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to Todo at CLITodo.main(CLITodo.java:13) </code></pre> <p>When I remove <code>&lt;Todo&gt;</code> in CLITodo I get this error:</p> <pre><code>CLITodo.java:13:19: error: incompatible types for (Todo t : todos) { ^ required: Todo found: Object </code></pre> <p>Why does this error occur? My Manager classes getTodos()-Method returns a List of type Todo yet the compiler tells me that it is just an Object (which it is of course but it is a collection as well, which should actually work). This is the first time this error occured and I really can't seem to find what is causing it.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload