I worked on some rather complicated excel files and ran into a problem with copying sheets. Whenever I try to copy a sheet that is not completely empty, I get the following message:
Exception in thread "main" java.lang.NullPointerException at jxl.write.biff.WritableSheetCopier.shallowCopyCells(WritableSheetCopier.java:499) at jxl.write.biff.WritableSheetCopier.copySheet(WritableSheetCopier.java:239) at jxl.write.biff.WritableSheetImpl.copy(WritableSheetImpl.java:1622) at jxl.write.biff.WritableWorkbookImpl.copySheet(WritableWorkbookImpl.java:987) at excelCalc.main(excelCalc.java:18)
I wonder what the problem is. Why not even have a ".copySheet (" function "if it cannot be used for sheets with information in them. In an attempt to reproduce the problem on a simpler scale, I created the code that you see below. See 2 identical sheets with a cell ( 0.0) labeled “test.” One sheet called “Stream” another, “copy.” Any ideas as to why this gives this null pointer?
import java.io.File; import jxl.*; import jxl.write.*; public class excelCalc { public static void main(String[] args) throws Exception { WritableWorkbook outputBook = Workbook.createWorkbook(new File("C:/Users/Kevin Brey/CS243/ExcelTest/files/output", "output.xls")); WritableSheet rSheet = outputBook.createSheet("Flows", 0); rSheet.addCell(new Label(0, 0, "test")); outputBook.copySheet(0, "copy", 0); outputBook.write(); outputBook.close(); } }
EDIT: This code also gives the same exception:
import java.io.File; import jxl.*; import jxl.write.*; public class excelCalc { public static void main(String[] args) throws Exception { WritableWorkbook outputBook = Workbook.createWorkbook(new File("C:/Users/Kevin Brey/CS243/ExcelTest/files/output", "output.xls")); WritableSheet sheet1 = outputBook.createSheet("Sheet1", 0); WritableSheet sheet2 = outputBook.createSheet("Sheet2", 1); sheet1.addCell(new Label(0, 0, "Label1")); sheet2.addCell(new Label(0, 0, "Label2")); outputBook.copySheet(0, "Copy", 1); outputBook.write(); outputBook.close(); } }
One of my ideas about what might be wrong is that since the sheet is open and edited, it cannot be copied. I really don't know how to get around this, though.