user849657
user849657

Reputation: 9

error in casting

public class xyArea{
public static void main(String arg[]){
  XYSeries series = new XYSeries("Average Weight");
  series.add(20.0, 20.0);
  series.add(40.0, 25.0);
  series.add(55.0, 50.0);
  series.add(70.0, 65.0);
  XYDataset xyDataset = new XYSeriesCollection(series);
  JFreeChart chart = ChartFactory.createAreaChart
  ("XY Chart using JFreeChart", "Age", "Weight",
   (CategoryDataset) xyDataset, PlotOrientation.VERTICAL, true, 
  true, false);
  ChartFrame frame1=new ChartFrame("XYArea Chart",chart);
  frame1.setVisible(true);
  frame1.setSize(300,300);
  }
}

I wrote this code but error below is occured Please guide me :

Exception in thread "main" java.lang.ClassCastException: org.jfree.data.xy.XYSeriesCollection cannot be cast to org.jfree.data.CategoryDataset at xyArea.main(xyArea.java:21)

Upvotes: 0

Views: 956

Answers (2)

Stephen C
Stephen C

Reputation: 718788

But my all search Jfreechart sample code are such this if not does'nt cast another error is occuring.

Either the samples / examples are incorrect, or you are misunderstanding them. Please post a link to the samples / examples you are talking about so that someone can give you a more helpful answer.


JFreeChart chart = ChartFactory.createAreaChart ("XY Chart using JFreeChart", 
    "Age", "Weight", xyDataset, PlotOrientation.VERTICAL, true, true, false);

If that is supposed to be one of the examples, I suspect that the problem is that the example doesn't match the version of the API that you are trying to use. According to the latest version of the javadoc, createAreaChart does not take an XYDataset parameter at all, and the actual class you are trying to use is not a CategoryDataset.

The solution is to find some sample code that matches the version of JFreeChart that you are using. Or better still, read and understand the JFreeChar javadocs so that you don't have to copy other peoples' code!


The "screaming penguin" tutorial is for jfreechart 1.0.0-pre1 (circa 2004). You are apparently using a later version of JFreeChart.

Why don't you fork out some money for the documentation, which (apparently) includes lots of up-to-date examples? That way you will also be helping to support future development of the software.

Upvotes: 2

Bozho
Bozho

Reputation: 597076

Well, you can't cast to CategoryDataset because XYSeriesCollection is not a CategoryDataset.

It appears you need .createXYAreaChart(..) (rather than .createAreaChart(..))

Upvotes: 4

Related Questions