Bug 57362

Summary: Unable to get Chart Axis
Product: POI Reporter: srivastavavijay
Component: XSSFAssignee: POI Developers List <dev>
Status: RESOLVED FIXED    
Severity: critical CC: srivastavavijay
Priority: P2 Keywords: PatchAvailable
Version: 3.10-FINAL   
Target Milestone: ---   
Hardware: PC   
OS: All   
Attachments: SampleExcel

Description srivastavavijay 2014-12-17 08:00:43 UTC
I have excel having chart with primary and secondary axis.  I am to retrieve the chart Axis details to update the maximum value. 

I am having XSSFSheet instance after loading the given excel on which i am retrieving drawing and chart as below:

        final XSSFDrawing drawing = xsh.createDrawingPatriarch();
        final XSSFChart chart = drawing.getCharts().get(0);
        final List<XSSFChartAxis> axisList = (List<XSSFChartAxis>) chart.getAxis();

While debugging, it seems that XSSFChart.getAxis() is not having null check on axis variable and its not initialized. Its just checking the empty check. Which is why its failing. axis is initialized in the constructor. However in this case, its not initialized.

I think in this case it should not come as null or it should be initialized on getAxis also.
Comment 1 Nick Burch 2014-12-17 08:11:21 UTC
Can you produce a small junit unit test showing this problem? And ideally also a patch if you can see the right fix!
Comment 2 srivastavavijay 2014-12-19 08:34:10 UTC
Created attachment 32306 [details]
SampleExcel
Comment 3 srivastavavijay 2014-12-19 08:34:27 UTC
I just downloaded the src from svn and trying to get used to build process and creating patch . 
Meanwhile I just provide the test case and possible fix (which, I had tried out to be working).

Test Examples:
//Load existing excel with some chart on it having primary and secondary axis.
final File res = new File("SampleExcel.xlsx");
final Workbook workbook = new XSSFWorkbook(OPCPackage.open(new FileInputStream(res)));
        final Sheet sh = workbook.getSheetAt(0);
        final XSSFSheet xsh = (XSSFSheet) sh;
        final XSSFDrawing drawing = xsh.createDrawingPatriarch();
        final XSSFChart chart = drawing.getCharts().get(0);
        final List<XSSFChartAxis> axisList = (List<XSSFChartAxis>) chart.getAxis();
//Note: getAxis fails as axis variable inside chart is null.
        for(XSSFChartAxis axis: axisList){
           //Do Some operation.
        }


Fix:XSSFChart.java
=====================
public List<? extends XSSFChartAxis> getAxis() 
{
 //ADDED NULL Check and Initialize the axis.
 if(axis == null){
   axis = new ArrayList<XSSFChartAxis>();
 }
 if (axis.isEmpty() && hasAxis()) {
   parseAxis();
 }
return axis;
}
Comment 4 Dominik Stadler 2014-12-22 14:24:14 UTC
Thanks for the Patch, this is now applied via r1647317, although I reworked it slightly to not need to query in getAxis() constantly, we rather now create the List of Axises always.
Comment 5 srivastavavijay 2014-12-24 05:25:34 UTC
(In reply to Dominik Stadler from comment #4)
> Thanks for the Patch, this is now applied via r1647317, although I reworked
> it slightly to not need to query in getAxis() constantly, we rather now
> create the List of Axises always.

Thanks for applying the patch.