#1084 ClassCastException thrown in the add(Block, Object) method

1.0.x
closed
David Gilbert
General (896)
5
2013-11-21
2012-03-30
Shin Hwei Tan
No

The method "add" in the org.jfree.chart.block.BorderArrangement" class and "org.jfree.chart.block.BlockContainer " class does not check the parameter "key" for the correct type. It throws ClassCastException when called with a null block and an org.jfree.chart.LegendItem object (i.e., "add((Block)null, new org.jfree.chart.LegendItem("label"))"). The exception thrown is due to an explict cast(i.e., casting parameter "key" of type Object to RectangleEdge).

Suggested Fixes: Replace the else statement in the method body for the class "BorderArrangement" by changing it like this:

public void add(Block block, Object key) {

if (key == null) {
this.centerBlock = block;
}
else if(key instanceof RectangleEdge) {
RectangleEdge edge = (RectangleEdge) key;
...
}
}

Discussion

  • Shin Hwei Tan
    Shin Hwei Tan
    2012-03-30

    • assigned_to: nobody --> mungady
     
  • David Gilbert
    David Gilbert
    2013-11-21

    I've changed the method so that if the key is not an instance of RectangleEdge then the block is added at the center (same as null key). This will be included in the upcoming JFreeChart 1.0.17 release.

     
  • David Gilbert
    David Gilbert
    2013-11-21

    • status: open --> closed