This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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 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).
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
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.
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.
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)
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?
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?
Joined: Jan 16, 2010
Hi Rob, sure, here is the code I inserted before line 47 in the original.
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?