• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java program to convert to xml issues

 
sai rama krishna
Ranch Hand
Posts: 432
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am trying following example to convert between xls to xml.I am getting nullpointer exception


Exception in thread "main" java.lang.NullPointerException
at com.aaa.bbb.ccc.ExcelTooXml.ProcessRow(ExcelToXml.java:70)
at com.aaa.bbb.ccc.ExcelToXml.main(ExcelToXml.java:137)

Any ideas, suggestions, sample code, links, source code highly appreciated. Thanks in advance
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The error message says that the NPE is ocurring at line 70. Which line is that? The one that's marked as 70 in the code cannot cause an NPE, so your actual line numbers must be different.

Look at line 70 and figure out what's null there. If you can't figure it out, add println() statements right before it to print out everything that you're assuming is not null and that could be causing the problem. If you still can't figure it out, post again, indicating which line is causing the problem.
 
sai rama krishna
Ranch Hand
Posts: 432
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see.


System.out.println("sheet is"+sheet);
row = sheet.getRow(nLoop);


Sheet is null. What shall i initialize it with. Please advise



following is already there
private static HSSFSheet sheet ;

I now changed it as

private static HSSFSheet sheet =null;


But it did not work. Getting same NPE
 
Campbell Ritchie
Sheriff
Pie
Posts: 49470
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course. You should beware of nulls, which are nasty things which hide in the corner and then come back to bite you. If sheet is a field, where was it initialised? Why didn’t you initialise it in the constructor?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sai rama krishna wrote:
I now changed it as

private static HSSFSheet sheet =null;

But it did not work. Getting same NPE


Why would you expect that to fix anything. You already know that the problem is that sheet is null. Now you're saying you changed it to explicitly set sheet to null. So sheet is just as null as it was before.

What shall i initialize it with


Null means that reference does not point ot an object. So you need an HSSFSheet object, created appropriately according to the documentation and your requirements and use case.

And why is sheet static?

It would probably be a good idea for you to take a step back and spend some time learning the fundamentals of the Java language before attempting to use a complex API like POI.

 
sai rama krishna
Ranch Hand
Posts: 432
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure.


sheet = workbook.getSheetAt(0);


Fixed above problem and printing the xml on console successfully.

How do I save and see it under say 'C' drive with say sampleXml.xml filename. Please advise.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49470
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can save files the same way you save anything. I would start here, and you can find out about files in general here.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic