This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes java program to convert to xml issues Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "java program to convert to xml issues" Watch "java program to convert to xml issues" New topic
Author

java program to convert to xml issues

sai rama krishna
Ranch Hand

Joined: May 29, 2009
Posts: 245
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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: May 29, 2009
Posts: 245
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

Joined: Oct 13, 2005
Posts: 38513
    
  23
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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: May 29, 2009
Posts: 245
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

Joined: Oct 13, 2005
Posts: 38513
    
  23
You can save files the same way you save anything. I would start here, and you can find out about files in general here.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java program to convert to xml issues