1

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
5
  • 1
    CandleStickChart.java:74 Which is line 74? For better help sooner, post an SSCCE (in which the answer to my question would be obvious). Commented Mar 31, 2012 at 9:24
  • final JFreeChart chart = ChartFactory.createCandlestickChart( "Candlestick Demo", "Time", "Price", dataset, false); return chart; As i have said the datset seems to have some problem but i am unable to identify it Commented Mar 31, 2012 at 9:28
  • 1
    Please use code formatting for input & output. "the datset seems to have some problem" Why not prepare an SSCCE with hard coded data that works, to test that theory? If you can achieve that, it would point to the dataset being the problem. If the hard-coded data shows the same problem, we can probably help further if you post it as an edit. Commented Mar 31, 2012 at 9:42
  • I guess the size of the array was the problem.When i reduced it it worked. Can someone please tell me how i can overcome this.(What is the maximum size i can enter). Commented Mar 31, 2012 at 10:03
  • Please cite "the tutorial from rose india." Commented Nov 13, 2012 at 14:27

2 Answers 2

1

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).

Sign up to request clarification or add additional context in comments.

3 Comments

I have removed the part where i populate my arrays. The print statement is after that to check if my arrays have been populated in a proper manner.
Ok I see. But still the NPE suggests, that an element somewhere in the date array is null.
Please see my comment at the top.
0

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))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.