• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

NullPointerException (reading from Excel file)

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

I have an Excel-file with a column called 'TRAIN_NUM'. Some examples of the trainnumbers are: 10, 1149, 1150/1, 1173/2, ...
We don't work with an Integer all the time so I decided to store these values as String values.


TRAIN_NUM.add(cell.toString());



But when I run the program I get the following error:
Exception in thread "main" java.lang.NullPointerException
at ReadTMSdata.main(ReadTMSdata.java:122)

But there are no gaps in the TRAIN_NUM column, so there are no null values. Any ideas?
Thanks and happy Halloween!
 
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It would be more useful if you posted a snippet of your code where the problem is occurring rather than simply one line. That way, you are more likely to get help faster.
 
Jil Van Wetter
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

James Boswell wrote:It would be more useful if you posted a snippet of your code where the problem is occurring rather than simply one line. That way, you are more likely to get help faster.


I hope this helps:

 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So either TRAIN_NUM or cell could be null - which one is it?
 
Jil Van Wetter
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ulf Dittmer wrote:So either TRAIN_NUM or cell could be null - which one is it?


The ArrayList TRAIN_NUM is empty in the beginning, but that shouldn't be a problem. My column in Excel has no null values (see attachment).

I don't understand where this error is coming from.
untitled.JPG
[Thumbnail for untitled.JPG]
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You misunderstood - the NullPointerException happens in line 122 of your ReadTMSdata class. If that line is indeed "TRAIN_NUM.add(cell.toString())", then either the *TRAIN_NUM* or the *cell* Java objects are null (which is what causes NullPointerExceptions). There *may* be a connection to the contents of the spreadsheet, but the problem could also be something entirely different.

So you need to find out which object is null - maybe by printing out their respective values *before* line 122.
 
James Boswell
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The ArrayList TRAIN_NUM is empty in the beginning, but that shouldn't be a problem.


That depends. How have you initialised the list?
 
Jil Van Wetter
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

James Boswell wrote:

The ArrayList TRAIN_NUM is empty in the beginning, but that shouldn't be a problem.


That depends. How have you initialised the list?


Like this:


ArrayList <String> TRAIN_NUM = new ArrayList <String>();

 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you ascertained by now whether either TRAIN_NUM or cell is null?
 
Jil Van Wetter
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ulf Dittmer wrote:Have you ascertained by now whether either TRAIN_NUM or cell is null?


I think neither are null, the cells in the column are never empty.
In the beginning ArrayList TRAIN_NUM is empty but it should be filled with the values from the Excel column TRAIN_NUM.
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're confusing something in the spreadsheet having some value with a Java object being null. There may be a connection, but there needn't be.

But you're getting a NullPointerException, which means that some Java object is definitely null. The stack trace even tells you in which line that causes a problem. Finding out which object that is is the obvious next step in debugging this. That's why I keep asking about it.
 
Jil Van Wetter
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ulf Dittmer wrote:You're confusing something in the spreadsheet having some value with a Java object being null. There may be a connection, but there needn't be.

But you're getting a NullPointerException, which means that some Java object is definitely null. The stack trace even tells you in which line that causes a problem. Finding out which object that is is the obvious next step in debugging this. That's why I keep asking about it.



Okay, I think I have found the problem.
I am working with CSV-files which I split up in several xlsx files.
I get the amount of rows from

int rows = Sname.getLastRowNum();


When I work with this value in a for loop, I get the NullPointerException.
But when I choose a fixed value for int rows (smaller than the amount of rows) I don't get the error.
I think Sname.getLastRowNum() takes a row where there is nothing filled in. Is this possible and how can I change this? Because it's really not practical to check each file for the last filled in row.
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I seem to recall that getLastRowNum does not return the last row number that has data, but the overall last row number (or something like that). So you need to check all the data you get from the file whether or not they actually contain valid data (or are null, since you keep getting NPEs).
 
Jil Van Wetter
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ulf Dittmer wrote:I seem to recall that getLastRowNum does not return the last row number that has data, but the overall last row number (or something like that). So you need to check all the data you get from the file whether or not they actually contain valid data (or are null, since you keep getting NPEs).


I understand that, but in another program he gets the lastrownum with data in it but here the file hasn't been modified.
If I cut a part at the end of the file and save and get the lastrownum, I get the NPE.
There must be a way to fix this (maybe an update button in Excel or something like that), since I have a lot of files to work with and I cannot enter for each file seperately the last row number with data in it.
 
Ranch Hand
Posts: 679
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jil Van Wetter wrote:There must be a way to fix this (maybe an update button in Excel or something like that), since I have a lot of files to work with and I cannot enter for each file seperately the last row number with data in it.


I don't know if there is a way to fix the files in Excel, but you can fix the problem in your code by wrapping lines 14 - 23 in the code you posted above in an if statement that checks if cell is null



Edit: This of course assumes that it is the cell variable that is null. As Ulf said, the first thing you should do is find out which variable is causing the NPE.
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, as I have said a number of times by now, you need to find out which Java object is null. Then you can work back from that and try to figure out *why* it is null (and then take further steps based on that). So far it seems -possibly a mistaken impression- that you're trying to guess what might be going wrong, instead of taking concrete steps to find out for sure what is going wrong.

And no, you don't need to determine for each file how big it is, you just need to write code that performs proper checks on the data it reads.
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem you are running into is because every Row can have a different number of Cells, they don't have to start at index 0, and they don't have to end at the same width. If there is a row with missing values you can get nulls. You could do what Stuart suggests - test every cell for null. If you do this, you might also consider looping for each Row as the outer loop, and for each Cell as the inner loop, and using the First Cell in the Row and the Last Cell in the Row as your looping conditions. This will reduce the number of possible nulls (but you still need to do the null check because inner cells may be empty as well).

Or you can try to avoid nulls from being generated by setting the MissingCellPolicy to RETURN_NULL_AS_BLANK when you create your workbook.
 
Jil Van Wetter
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ulf Dittmer wrote:Well, as I have said a number of times by now, you need to find out which Java object is null. Then you can work back from that and try to figure out *why* it is null (and then take further steps based on that). So far it seems -possibly a mistaken impression- that you're trying to guess what might be going wrong, instead of taking concrete steps to find out for sure what is going wrong.

And no, you don't need to determine for each file how big it is, you just need to write code that performs proper checks on the data it reads.


I have followed your advice and since there was no null in the ArrayList, I investigated the Excel file I edited and that is how I found out that the null occured after the last row with data.
I have changed this and the problem is fixed.

Stuart A. Burkett wrote:

Jil Van Wetter wrote:There must be a way to fix this (maybe an update button in Excel or something like that), since I have a lot of files to work with and I cannot enter for each file seperately the last row number with data in it.


I don't know if there is a way to fix the files in Excel, but you can fix the problem in your code by wrapping lines 14 - 23 in the code you posted above in an if statement that checks if cell is null



Edit: This of course assumes that it is the cell variable that is null. As Ulf said, the first thing you should do is find out which variable is causing the NPE.


This fixed the problem, thank you!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic