我正在使用以下代码从Rose India获得本教程之后的烛台图。但是,当我尝试填充数组时,它会在创建数据集时抛出空指针。
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);
}
}
例外
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)
我究竟做错了什么
我意识到异常与日期数组有关。当我打印日期数组时,这就是我得到的时间可能是零,但这并不意味着它必须抛出空指针异常。
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
最佳答案
您没有提供任何数据,仅提供了空数组(默认初始化)。因此,您的日期数组包含空值,这会导致NullPointerException。我(从您的代码中)看不到如何打印日期数组可以为您提供这样的结果。我尝试了您的代码,日期(确切地说是d)仅包含空值。
org.jfree.data.xy.DefaultHighLowDataset.getX(DefaultHighLowDataset.java:147)行访问date数组并对该项目调用getTime(可能与您使用的版本不同)。
关于java - Jfree图表抛出空指针异常,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9954652/