Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NullPointerException problem

 
Michael Comerford
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys, this problem has been frustrating me for hours, I've tried changing the code with little tweaks and commenting out bits to test it and dumping stuff to system.out to see what is happening along the way but I can't find a reason for this exception.

I'm building a csv parser (yes I know some already exist but my supervisor wants me to do this as part of a project) My code is included below

the test csv file I use looks like this:

"CaseId","Age","Name","Height [m]"
1,21,"Alan",1.82
2,45,"Steve",1.53
3,54,"Rick",1.72
4,32,"Jon",1.49
5,67,Chris,1.93

The exception occurs at line 47
all I'm doing in that section is differentiating between types, so storing the type of each column of data as either "s" for a string, "r" for a double, and "i" for an integer and then adding it to my 'table'(which is an arraylist of arraylists).

Any help greatly appreciated!

 
fred rosenberger
lowercase baba
Bartender
Posts: 12146
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i don't know what your problem is, but I do know that "System.out.println()" is your best friend. I would suggest printing out the values of i just before your line 47 and see what it is when you throw your NPE.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49411
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To add to what Fred said:
There are four object references in line 47, table, whatever get(i) returns and tokens and tokens[i]. Since two are used in line 46, they are unlikely to be null in line 47.
Look in the Formatter class, and find out what the %b (for "boolean") tag prints. Now you want to print out the four objects, with a print statement:That line goes after 46 and before 47. You will need to check my spelling carefully. If you don't get that to work, divide it into four separate lines.

You are reminded that when you set up an array of object references, it is initially populated with nulls, so you need to fill the array with something real before trying to use it.
 
Rob Spoor
Sheriff
Pie
Posts: 20550
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't try to print out both table and table.get(i) at the same time if table is null - the printing won't even occur because the NPE will already been thrown. Better split it into separate print outs.
 
Michael Comerford
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys, thanks for the help narrowing this down

the console returns the folllowing when I included the System.out.prinf code

This object table is not null: true
This object table.get(i) is not null: true
This object tokens not null: true
This objects tokens[i] not null: trueException in thread "main" java.lang.NullPointerException
at StataColumn.add(StataColumn.java:32)
at CsvIn.main(CsvIn.java:61)

So tokens[i] is null?
tokens is a String array that takes it input from a Scanner that scans the csv file and splits it into elements and stores it in tokens[], so is it saying the i'th element is null?
Or am I missing something?

 
Rob Spoor
Sheriff
Pie
Posts: 20550
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are printing tokens[i] without calling any methods on it, then it's more likely that (although your check says otherwise) tokens itself is null. Can you show us the code that prints out these lines?
 
Michael Comerford
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rob, sure, here is the code I inserted before line 47 in the original.

 
rob michael
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From your stack trace snippet posted in your above system.out example, we see that:



You can see that the exception is on line 61 of your main method (i assume this is the same as line 47 - but has just been increased because of the newly added system.outs) - and then that then is being thrown on StataColumn.add (line 32 of the StataColumn class) - which makes sense as line 47 calls the stataColumn.add() method on line 47:



Can you show us what is happening in the add method for StataColumn?

 
Michael Comerford
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No problem is here is the StataColumn class


 
rob michael
Greenhorn
Posts: 27
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, heres the problem:

in your main class on line 30 (original post line 30) you call the StataColumn constructor as follows:




tokens is initialised as an array of strings, so you are calling the following constructor in your StataColumn class:




instead of calling one of the other constructors that intialises your arrayList




That finally means you get an NPE when your main class calls the StataColumn.add() method on line 47 as it tries to perform this:




and values is not initialised.


To fix this, either call the constructor passing in a MyString object, or change the String constructor to initialise that arrayList.

Hope that helps!
 
Michael Comerford
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it nailed thanks for your help guys!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic