Menu

#851 Exception in getIndex() method DefaultKeyedValues.java

closed-fixed
General (896)
5
2008-04-07
2008-04-04
Anonymous
No

Submitted by Mark Parmenter
Email: mark <dot> parmenter <at> mwaintel <dot> com

This is the following code:

public int getIndex(Comparable key) {
if (key == null) {
throw new IllegalArgumentException("Null 'key' argument.");
}
int i = 0;
Iterator iterator = this .data.iterator();
while (iterator.hasNext()) {
KeyedValue kv = (KeyedValue) iterator.next();
if (kv.getKey().equals(key)) { //<---------------- THIS LINE IS THE PROBLEM
return i;
}
i++;
}
return -1; // key not found
}

Since we know that key cannot be null in the line indicated above, it should be the object upon which the equals() method is called. Substituting this line would prevent the NullPointerException that this code currently generates:

if (key.equals(kv.getKey())) { //key cannot be null given the check performed at the beginning of this method

Discussion

  • David Gilbert

    David Gilbert - 2008-04-07
    • assigned_to: nobody --> mungady
    • status: open --> closed-fixed
     
  • David Gilbert

    David Gilbert - 2008-04-07

    Logged In: YES
    user_id=112975
    Originator: NO

    Thanks for the report. In fact the DefaultKeyedValue class should never allow the key to be null in the first place, and a change went into Subversion back in February to correct this. That fix will be included in the 1.0.10 release.

    Regards,

    Dave Gilbert
    JFreeChart Project Leader

     

Log in to post a comment.