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

null pointer

Dustin Schreader
Ranch Hand

Joined: May 25, 2009
Posts: 102
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


And here is my code

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14345
    
  22

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:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19760
    
  20

Can you please post the exception stack trace? It tells us (and you as well) where the exception occurred.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Dustin Schreader
Ranch Hand

Joined: May 25, 2009
Posts: 102
Is this it?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14345
    
  22

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.
Dustin Schreader
Ranch Hand

Joined: May 25, 2009
Posts: 102
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.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14345
    
  22

Either:
  • rows is null
  • rows[w] is null
  • rows[w][0] and/or rows[w][1] is null

  • You could add some System.out.println statements before line 170 to check:

    Dustin Schreader
    Ranch Hand

    Joined: May 25, 2009
    Posts: 102
    I added on line 167 and I got

    There must be something wrong with my createArray method correct? or am I not calling the method correctly?
    James Boswell
    Bartender

    Joined: Nov 09, 2011
    Posts: 1030
        
        5

    Hi

    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:

    Dustin Schreader
    Ranch Hand

    Joined: May 25, 2009
    Posts: 102
    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?
    Aditya Jha
    Ranch Hand

    Joined: Aug 25, 2003
    Posts: 227

    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?
    Dustin Schreader
    Ranch Hand

    Joined: May 25, 2009
    Posts: 102
    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?
    Aditya Jha
    Ranch Hand

    Joined: Aug 25, 2003
    Posts: 227

    Your problem is not incorrect usage of api.
    Dustin Schreader wrote:

    What does this tell you?
    Dustin Schreader
    Ranch Hand

    Joined: May 25, 2009
    Posts: 102
    Oh, yup I see it now. Its not seeing the file somehow. Thanks
     
     
    subject: null pointer