user1092042
user1092042

Reputation: 1295

Jfree chart throwing null pointer exception

I am using the following code to get a candle stick graph following the tutorial from rose india. However when i try to populate my array it throws a null pointer at create dataset .

   public class CandleStickChart extends ApplicationFrame {
    static String date[]=new String[2000];
    static double open[]=new double[2000];
    static double close[]=new double[2000];
    static double high[]=new double[2000];
    static double low[]=new double[2000];
    static double volume[]=new double[2000];
    static Date d[]=new Date[2000];

      public CandleStickChart(String titel) {
      super(titel);

      final DefaultHighLowDataset dataset = createDataset();
      final JFreeChart chart = createChart(dataset);
      final ChartPanel chartPanel = new ChartPanel(chart);
      chartPanel.setPreferredSize(new java.awt.Dimension(600, 350));
      setContentPane(chartPanel);
      }

      private DefaultHighLowDataset createDataset() {
      DefaultHighLowDataset data = new DefaultHighLowDataset(
      "", d, high, low, open, close, volume);
      return data;
      }


      private JFreeChart createChart(final 
      DefaultHighLowDataset dataset) {
      final JFreeChart chart = ChartFactory.createCandlestickChart(
      "Candlestick Demo", "Time", "Price", dataset, false);
      return chart;
      }

      public static void main(String args[]) 
      {
    //populating arrays using data
    //checking if array is populated.      
            for(int i=0;i<temp;i++)
            {
                System.out.println(" "+high[i]+" "+low[i]+" "+open[i]+" "+close[i]+" "+volume[i]);
                System.out.println(d[i]);
            }


     CandleStickChart chart = new CandleStickChart("Candle Stick Chart");
      chart.pack();
      RefineryUtilities.centerFrameOnScreen(chart);
      chart.setVisible(true);







    }
    }  

Exception

java.lang.NullPointerException
    at org.jfree.data.xy.DefaultHighLowDataset.getX(DefaultHighLowDataset.java:147)
    at org.jfree.data.xy.AbstractXYDataset.getXValue(AbstractXYDataset.java:75)
    at org.jfree.data.general.DatasetUtilities.iterateDomainBounds(DatasetUtilities.java:777)
    at org.jfree.data.general.DatasetUtilities.findDomainBounds(DatasetUtilities.java:677)
    at org.jfree.data.general.DatasetUtilities.findDomainBounds(DatasetUtilities.java:650)
    at org.jfree.chart.plot.XYPlot.getDataRange(XYPlot.java:4551)
    at org.jfree.chart.axis.DateAxis.autoAdjustRange(DateAxis.java:1284)
    at org.jfree.chart.axis.DateAxis.configure(DateAxis.java:716)
    at org.jfree.chart.axis.Axis.setPlot(Axis.java:968)
    at org.jfree.chart.plot.XYPlot.<init>(XYPlot.java:666)
    at org.jfree.chart.ChartFactory.createCandlestickChart(ChartFactory.java:1946)
    at CandleStickChart.createChart(CandleStickChart.java:74)
    at CandleStickChart.<init>(CandleStickChart.java:30)
    at CandleStickChart.main(CandleStickChart.java:189)

What am i doing wrong I realise the exception has something to do with the date array. BUt when i print the date array this is what i get.The time may be zero but does that mean it has to throw a null pointer exception.

Wed Mar 10 00:00:00 IST 2010
Tue Mar 09 00:00:00 IST 2010
Mon Mar 08 00:00:00 IST 2010
Fri Mar 05 00:00:00 IST 2010
Thu Mar 04 00:00:00 IST 2010
Wed Mar 03 00:00:00 IST 2010
Tue Mar 02 00:00:00 IST 2010
Mon Mar 01 00:00:00 IST 2010
Fri Feb 26 00:00:00 IST 2010
Thu Feb 25 00:00:00 IST 2010
Wed Feb 24 00:00:00 IST 2010
Tue Feb 23 00:00:00 IST 2010
Mon Feb 22 00:00:00 IST 2010
Fri Feb 19 00:00:00 IST 2010
Thu Feb 18 00:00:00 IST 2010
Wed Feb 17 00:00:00 IST 2010
Tue Feb 16 00:00:00 IST 2010
Fri Feb 12 00:00:00 IST 2010
Thu Feb 11 00:00:00 IST 2010
Wed Feb 10 00:00:00 IST 2010
Tue Feb 09 00:00:00 IST 2010
Mon Feb 08 00:00:00 IST 2010
Fri Feb 05 00:00:00 IST 2010

Upvotes: 1

Views: 972

Answers (2)

nansen
nansen

Reputation: 2972

You are not providing any data, only emtpy arrays (default initialized). Thus your date array contains null values, which cause the NullPointerException. I cannot see (from your code) how printing the date array can give you such a result. I tried your code and the date (d to be more exactly) contains only null values.

The line org.jfree.data.xy.DefaultHighLowDataset.getX(DefaultHighLowDataset.java:147) accesses the date array and calls getTime on the item (maybe something different in the version you are using).

Upvotes: 1

topxebec
topxebec

Reputation: 1427

The time may be zero but does that mean it has to throw a null pointer exception. It do throws a null pointer exception means your dataset itself is not null but when it do createCandlestickChart method with a specify data in your dataset the specify data may be null. I guess you should focus on some X value of the dataset.(org.jfree.data.xy.DefaultHighLowDataset.getX(DefaultHighLowDataset.java:147))

Upvotes: 0

Related Questions