Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Using JXL - Error encountered

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everyone. I am currently using JXL to read Excel files. And came to a problem.

I have this excel files that have rows with data, rows with spaces, rows with colors, rows with colors and data, etc. Here is my code:

public void readColor() throws Exception{
Workbook workbook = Workbook.getWorkbook(
new File("c:\\myexcel.xls"));
Sheet sheet = workbook.getSheet(2);
for(int i=3; i<sheet.getRows(); i++){
System.out.println(i + 1 + " - " +sheet.getCell(0, i).getCellFormat().getBackgroundColour().getDescription());
}
workbook.close();

}

when I run this, somewhere in the middle, it produced a java.lang.NullPointerException.

I tried to investigate, the error stopped a row blank row. I was wondering because it did not generate an error for the previous blank rows. I went to my excel file, and, i highlighted the sell, and set the background color to "no fill" (current is already no color/no fill). After that, code went thru...

Is there a way for me to code this that it will detect the right color of the cell, without throwing this exceptions? I think the exception is really weird..>
 
Rancher
Posts: 43027
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Start by finding out which object is null.
 
Joseph Siao
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ulf Dittmer, thank you for the reply.

I tried adding this code:

System.out.println(sheet.getCell(0, i).getContents());

it gave me a blank output. I also tried to put in an if statement to check if the code below returns a null. But it doesn't return null..
sheet.getCell(0, i).getCellFormat().getBackgroundColour().getDescription()

I am at a lost how to get around this..
 
Ulf Dittmer
Rancher
Posts: 43027
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The getContents call isn't in the code you posted, so it couldn't have been the cause of the NullPointerException. You can help us help you by posting the full stack trace of the exception.
 
Joseph Siao
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ulf Dittmer,

The getContents is not in the initial code I gave. I just played around to check what seems to be the problem.

Here is the stacktrace thrown by the Exception (below). The first 3 lines are success. Just to give you an idea. The number are the row numbers in excel. Then after the dash, is the color description of the cell.

After row 28, it gave me the null pointer exception. The supposedly 29 row is a blank row. Then it continued to the next.

26 - default background
27 - default background
28 - grey 25%
java.lang.NullPointerException
at wb.util.UtilData.readColor(UtilData.java:223)
at wb.eecompare.EECheck.main(EECheck.java:44)
30 - default background
31 - default background
32 - default background

I have attached an image of the excel data to better explain. Please advice.
image.png
[Thumbnail for image.png]
screenshot of excel
 
Ulf Dittmer
Rancher
Posts: 43027
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So the exception occurred in a method called readColor in a class named wb.util.UtilData - but that method isn't used in the code you posted, either.
reply
    Bookmark Topic Watch Topic
  • New Topic