NullPointerException on JXL Importsheet

I tried to use JAVA EXCEL API 2_6_12, and working with the importSheet function, but never success. Is there anything wrong with my code?



	public void write() throws IOException, WriteException {

		int i;
		File infile = new File(inputFile);
		File outfile = new File(outputFile);

		try {
		Workbook  sourceworkbook = Workbook.getWorkbook(infile);

		WritableWorkbook workbook =  Workbook.createWorkbook(outfile);

		Sheet sh =  sourceworkbook.getSheet(0);

		WritableSheet wSheet = workbook.importSheet("Report1", 0, sh);

			
		sourceworkbook.close();
		workbook.write();
		workbook.close();


		} catch (Exception e) {
			e.printStackTrace();
		} 

	}

Open in new window


inputFile, outputFile are string that contains the file name.  I tried to get value of cell from Sheet sh. So I sure the getSheet is working.
But the importSheet function always return following runtime error

 Exception in thread "main" java.lang.NullPointerException  
    at jxl.write.biff.SheetCopier.deepCopyCells(SheetCopier.java:996)  
    at jxl.write.biff.SheetCopier.importSheet(SheetCopier.java:542)  
    at jxl.write.biff.WritableSheetImpl.importSheet(WritableSheetImpl.java:2699)  
    at jxl.write.biff.WritableWorkbookImpl.importSheet(WritableWorkbookImpl.java:1897)  
mishelperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mick BarryJava DeveloperCommented:
code looks ok, have you tried with a different input file?
mishelperAuthor Commented:
yes I did and getting same error
CEHJCommented:
mishelper, can you make sure you've posted the full stack  trace?
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

CEHJCommented:
If this is the correct source at the link below, then there's a bug at line 996. See code  window below:

 http://www.docjar.org/html/api/jxl/write/biff/SheetCopier.java.html
//if (c.getCellFeatures() != null & c.getCellFeatures().hasDataValidation()); Produces NPE if getCellFeatures returns null

if (c.getCellFeatures() != null && c.getCellFeatures().hasDataValidation()); // << CORRECT

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mishelperAuthor Commented:
Exception in thread "main" java.lang.NullPointerException  
    at jxl.write.biff.SheetCopier.deepCopyCells(SheetCopier.java:996)  
    at jxl.write.biff.SheetCopier.importSheet(SheetCopier.java:542)  
    at jxl.write.biff.WritableSheetImpl.importSheet(WritableSheetImpl.java:2699)  
    at jxl.write.biff.WritableWorkbookImpl.importSheet(WritableWorkbookImpl.java:1897)
    at genExcel.WriteExcel.write(WriteExcel.java:58)
    at genExcel.WriteExcel.main(WriteExcel.java:155)

There are 2 more lines but I dont think it related, thanks
CEHJCommented:
OK. See my last comment
Mick BarryJava DeveloperCommented:
you don't have any charts on the sheet do you?
Have see issues with that library dealing with charts in the past
Unfortunatley support for that api has been falling, and the various problems that it has are not getting addressed.
Might be worth looking at alternate library that is more active.

One thing to try would be on an older version excel file
mishelperAuthor Commented:
objects- no I dont have the charts, actually I found that the function work with blank sheet. Even I create a sheet and add few characters in first cell then it not works.

CEHJ - so that mean I need to complie again myself or find another library right?




any other library that work with Excel you guys would suggest? thanks
CEHJCommented:
My guess is that you're NOT using the  version you think you are: the 2_6_12 version has had that bug fixed.
Mick BarryJava DeveloperCommented:
> actually I found that the function work with blank sheet.

yes expected it would, not a lot of use though

have a browse thru here
http://rgagnon.com/javadetails/java-0516.html
we mostly use POI
mishelperAuthor Commented:
CEHJ - I am using 2_6_12 but I found the bug you mentioned in the source code too.

objects - thanks, let me have a try with POI


thanks again for both your help!
CEHJCommented:
>>CEHJ - I am using 2_6_12 but I found the bug you mentioned in the source code too.

Download it again - it's not there now
mishelperAuthor Commented:

>>Download it again - it's not there now

http://www.andykhan.com/jexcelapi/jexcelapi_2_6_12.tar.gz

I just tried but found the source code still have it at line 996? And I did run it again and not working too, thanks
CEHJCommented:
Err, yes but that's not the correct site is it? ;) Don't use that: there could be other bugs too


http://downloads.sourceforge.net/project/jexcelapi/jexcelapi/2.6.12/jexcelapi_2_6_12.zip
mishelperAuthor Commented:
thanks again CEHJ,

but the link you gave me still have bug?

the source code in src\jxl\write\biff, line 996 of SheetCopier.java




anyway the link I provided is the website of JXL author.
CEHJCommented:
Sorry - my bad. I was looking at line 949 (where the bug is absent)
CEHJCommented:
Maybe you could be kind enough to report it if you're using his api ...
mishelperAuthor Commented:
Yes will do so, and thanks again for your kindly help!
CEHJCommented:
No problem ;)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.