• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

null pointer

 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:

 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is this it?
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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?
     
    Bartender
    Posts: 1051
    5
    Hibernate Eclipse IDE Chrome
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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?
     
    Ranch Hand
    Posts: 227
    Eclipse IDE Spring Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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 Spring Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Oh, yup I see it now. Its not seeing the file somehow. Thanks
     
    Why am I so drawn to cherry pie? I can't seem to stop. Save me tiny ad!
    a bit of art, as a gift, the permaculture playing cards
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic