Hello I've been fighting with this code for the past few hours and I don't know why i'm getting a null pointer. For some reason my createArray method is making arrays that all contain null.
Here is the infile ServiceMaster.csv
Do you get a NullPointerException with a stack trace printed?
Carefully look at the stack trace. It will tell you exactly in which line of your code the NullPointerException happens and what chain of method calls happened to get there. Look through your code at the line where the exception happens and where the call came from. Then try to discover what is null when it shouldn't be, and try to reason what exactly happened (the stack trace will tell you exactly what the chain of method calls was - use that information).
I haven't looked at your code in detail, but one common mistake made with arrays is forgetting that creating an array fills the whole array with nulls; it does not fill the array with new objects:
Yes. Note that the second line tells you that the exception happened in line 171 of ServiceMaster.java, which was called from line 263 of ServiceMasterForm.java, which was called from line 16 of ServiceMasterForm.java etc.
Looking at the code you posted, the line numbers probably don't correspond exactly with what you have in your source file exactly, because line 171 only contains a System.out.println statement that can't normally cause a NullPointerException. It's probably line 170:
Most likely rows[w][0] or rows[w][1] is null. When you try to call a method on a null, as you are trying to call the equals method, then you'll get a NullPointerException.
Make sure that the array actually contains objects, or check for null before calling the equals method on whatever is in the array.
Yes, I figured that the problem was coming from so that must mean that the array i'm creating is null but I looked through the createArray method and I don't see how it could be null.
Is there a problem when the CSV file is read? I couldn't help but notice that you are 'swallowing' any IOException in the createArray method. This is generally bad practice. At a minimum, add the following to your catch block:
I usually have something there but netbeans was flagging it for some reason so I just deleted it for now. The stack trace looks different now. Why would there be a problem at line 120, I'm not even calling that method?
Dustin Schreader wrote:I usually have something there but netbeans was flagging it for some reason so I just deleted it for now. The stack trace looks different now. Why would there be a problem at line 120, I'm not even calling that method?
Never leave a catch block empty. It just increases headaches later.
As you can see in the snippet above, line 120 of class FileInputStream is called from line 79 of the same class, which has been invoked from line 41 of class FileReader, which you are using in your program at line 21 in method createArray.
The error message is printed quite clearly. Do you think you are able to track down the real problem now?
Lol I wish I could see things as clearly as you. It looks like there is an issue with the file i/o with the buffer reader at line 21. I'm assuming it is the incorrect use of file i/o?