aspose file tools*
The moose likes Beginning Java and the fly likes NullPointerException problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "NullPointerException problem" Watch "NullPointerException problem" New topic
Author

NullPointerException problem

Michael Comerford
Greenhorn

Joined: Jan 16, 2010
Posts: 13
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

Joined: Oct 02, 2003
Posts: 11231
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
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

Joined: Oct 27, 2005
Posts: 19670
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Michael Comerford
Greenhorn

Joined: Jan 16, 2010
Posts: 13
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

Joined: Oct 27, 2005
Posts: 19670
    
  18

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

Joined: Jan 16, 2010
Posts: 13
Hi Rob, sure, here is the code I inserted before line 47 in the original.

rob michael
Greenhorn

Joined: May 22, 2011
Posts: 27
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?


Have a look at my blog: http://automateddeveloper.blogspot.com/
Or even my Android Apps (website): http://www.mobile.thirdmindmedia.co.uk
Michael Comerford
Greenhorn

Joined: Jan 16, 2010
Posts: 13
No problem is here is the StataColumn class


rob michael
Greenhorn

Joined: May 22, 2011
Posts: 27
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

Joined: Jan 16, 2010
Posts: 13
Got it nailed thanks for your help guys!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: NullPointerException problem