Uploaded image for project: 'BlueJ'
  1. BlueJ
  2. BLUEJ-137

Error message on for-each loops seems backwards



    • Type: Task
    • Status: Closed
    • Priority: Low
    • Resolution: Won't Do
    • Affects Version/s: 2.5.0
    • Fix Version/s: 3.0.0
    • Component/s: Compiler


      This one is sort of Java's fault, but BlueJ really needs to compensate for it.

      If you write a for-each loop but put the type of the collection rather than the type of the elements (a common beginner error), your for-loop header looks like this:

          for (ArrayList<String> str : list)

      What BlueJ says about this is:
      :Incompatible types: found String but expected ArrayList<String>
      This is exactly backwards from what the student needs to see, because in fact they put ArrayList<String> but needed String.

      The reason BlueJ says this is because the raw javac error is:

      StringListProcessor.java:26: incompatible types
      found   : String
      required: java.util.ArrayList<String>
              for (ArrayList<String> str : list) {

      That caret (not shown in BlueJ) is the giveaway: having seen the first part of the line, it thinks we want to iterate over a collection of ArrayLists, but then list would need to be an Iterable<ArrayList<String>>, but it's only an Iterable<String>.

      Nevertheless, this is really confusing for beginner students. (It was pretty confusing for me until I fired up javac and saw what java was trying to do with it.)




            • Assignee:
              davmac Davin McCall
              external External via trac
            • Votes:
              0 Vote for this issue
              0 Start watching this issue


              • Created: