系列 setTitle 的 apache-poi 4.0 NullPointer
apache-poi 4.0 NullPointer for series setTitle
Exception in thread "main" java.lang.NullPointerException at org.apache.poi.xddf.usermodel.chart.XDDFChartData$Series.setTitle(XDDFChartData.java:122)
代码如下:
CellReference cellref = new CellReference("A6");
//A6 value = "My Title"
XDDFLineChartData.Series series3 = (XDDFLineChartData.Series)data.addSeries(xs, ys3);
series3.setMarkerSize((short) 6);
series3.setMarkerStyle(MarkerStyle.DIAMOND);
series3.setTitle("My Title",cellref);
我查看了文档,它需要 arg0
的字符串和 arg1
的 CellReference
。
我一直以 NullPointerException
结尾。我错过了什么吗??
感谢您的回复。
回答XDDFChartData.Series.setTitle
中的bug如何修复的问题:
在 XDDFChartData.Series.setTitle getSeriesText()
is used without null check. But XDDFLineChartData.Series.getSeriesText() 中当然可能 return 为空,因为 series.getTx()
可能 return 为空。所以我们需要确保在使用 XDDFChartData.Series.setTitle
.
之前已经有一系列文本元素
...
XSSFChart chart = drawing.createChart(anchor);
...
XDDFCategoryAxis bottomAxis = chart.createCategoryAxis(AxisPosition.BOTTOM);
XDDFValueAxis leftAxis = chart.createValueAxis(AxisPosition.LEFT);
...
XDDFChartData data = chart.createData(ChartTypes.LINE, bottomAxis, leftAxis);
data.addSeries(...);
data.addSeries(...);
chart.plot(data);
if (chart.getCTChart().getPlotArea().getLineChartArray(0).getSerArray(0).getTx() == null)
chart.getCTChart().getPlotArea().getLineChartArray(0).getSerArray(0).addNewTx();
data.getSeries().get(0).setTitle("Series 1 Title", null);
if (chart.getCTChart().getPlotArea().getLineChartArray(0).getSerArray(1).getTx() == null)
chart.getCTChart().getPlotArea().getLineChartArray(0).getSerArray(1).addNewTx();
data.getSeries().get(1).setTitle("Series 2 Title", null);
...
//setting the axis Ids to the LineChart
chart.getCTChart().getPlotArea().getLineChartArray(0).addNewAxId().setVal(bottomAxis.getId());
chart.getCTChart().getPlotArea().getLineChartArray(0).addNewAxId().setVal(leftAxis.getId());
...
Exception in thread "main" java.lang.NullPointerException at org.apache.poi.xddf.usermodel.chart.XDDFChartData$Series.setTitle(XDDFChartData.java:122)
代码如下:
CellReference cellref = new CellReference("A6");
//A6 value = "My Title"
XDDFLineChartData.Series series3 = (XDDFLineChartData.Series)data.addSeries(xs, ys3);
series3.setMarkerSize((short) 6);
series3.setMarkerStyle(MarkerStyle.DIAMOND);
series3.setTitle("My Title",cellref);
我查看了文档,它需要 arg0
的字符串和 arg1
的 CellReference
。
我一直以 NullPointerException
结尾。我错过了什么吗??
感谢您的回复。
回答XDDFChartData.Series.setTitle
中的bug如何修复的问题:
在 XDDFChartData.Series.setTitle getSeriesText()
is used without null check. But XDDFLineChartData.Series.getSeriesText() 中当然可能 return 为空,因为 series.getTx()
可能 return 为空。所以我们需要确保在使用 XDDFChartData.Series.setTitle
.
...
XSSFChart chart = drawing.createChart(anchor);
...
XDDFCategoryAxis bottomAxis = chart.createCategoryAxis(AxisPosition.BOTTOM);
XDDFValueAxis leftAxis = chart.createValueAxis(AxisPosition.LEFT);
...
XDDFChartData data = chart.createData(ChartTypes.LINE, bottomAxis, leftAxis);
data.addSeries(...);
data.addSeries(...);
chart.plot(data);
if (chart.getCTChart().getPlotArea().getLineChartArray(0).getSerArray(0).getTx() == null)
chart.getCTChart().getPlotArea().getLineChartArray(0).getSerArray(0).addNewTx();
data.getSeries().get(0).setTitle("Series 1 Title", null);
if (chart.getCTChart().getPlotArea().getLineChartArray(0).getSerArray(1).getTx() == null)
chart.getCTChart().getPlotArea().getLineChartArray(0).getSerArray(1).addNewTx();
data.getSeries().get(1).setTitle("Series 2 Title", null);
...
//setting the axis Ids to the LineChart
chart.getCTChart().getPlotArea().getLineChartArray(0).addNewAxId().setVal(bottomAxis.getId());
chart.getCTChart().getPlotArea().getLineChartArray(0).addNewAxId().setVal(leftAxis.getId());
...