Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

null pointer

 
Dustin Schreader
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15214
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

 
Rob Spoor
Sheriff
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you please post the exception stack trace? It tells us (and you as well) where the exception occurred.
 
Dustin Schreader
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this it?
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15214
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15214
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    Posts: 102
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 1051
    5
    Chrome Eclipse IDE Hibernate
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 102
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 227
    Eclipse IDE Java Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 102
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 227
    Eclipse IDE Java Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Your problem is not incorrect usage of api.
    Dustin Schreader wrote:

    What does this tell you?
     
    Dustin Schreader
    Ranch Hand
    Posts: 102
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Oh, yup I see it now. Its not seeing the file somehow. Thanks
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic