• 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

java.lang.NullPointerException

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What happen with my code? everytime i try to read or print the values from the arraylist , it shows java.lang.nullpointerexception... AH!!!

3000
1000
4000
2000


i am trying to read the integer of the text file above and then store them into an Integer ArrayList





This is the second class to show the results
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch!

It would help us help you better if you provide us with more information like which like does the NPE originate at. The simplest way to do it is copy past the stack trace you see on your console (Please UseCodeTags while posting it)

Typically a null pointer exception is thrown when you try to invoke any method on a null object.

This would be more suitable on the beginner forum. Moving...
 
Lu Kim Hui
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry! edited... the full codes are up there
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lu Kim Hui wrote:sorry! edited... the full codes are up there


Could you also supply the NPE stack trace exactly as you received it? The first 10 lines or so should be sufficient.

Winston
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Take a look at line 10 of the SalesCounter class. What anre't you doing there, or anywhere else in the code?
 
Lu Kim Hui
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
java.lang.NullPointerException
at SalesCounter.readFromFile(SalesCounter.java:35)
at SalesCounterDemo.main(SalesCounterDemo.java:6)
at __SHELL1.run(__SHELL1.java:6)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at bluej.runtime.ExecServer$3.run(ExecServer.java:724)
java.lang.NullPointerException
at SalesCounter.printSalesList(SalesCounter.java:48)
at SalesCounterDemo.main(SalesCounterDemo.java:7)


Sorry, this you mean?

 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, that is what he means. Enter the following line before line 35 and after line 34:
 
Lu Kim Hui
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Yes, that is what he means. Enter the following line before line 35 and after line 34:





this is the result:

Please enter file name to read from (sales.txt) :
sales.txt
salesList != null false


but the NPE errors are still the same
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lu Kim Hui wrote:but the NPE errors are still the same


Yes, but ask yourself why. An NPE only has one cause: Something is null that wasn't expected to be.

Where do you ever set salesList to anything?

Winston
 
Lu Kim Hui
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Erm... because this is an exercise given from my lecturer, and the requirement is reading from the file and store inside an INTEGER arraylist and lastly print them out ^^"

saleList was the Integer arraylist
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lu Kim Hui wrote:Erm... because this is an exercise given from my lecturer, and the requirement is reading from the file and store inside an INTEGER arraylist and lastly print them out ^^"


No. That's the what. You're trying to find out why you're getting an NPE. Have a look at my last question again.

Winston
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This line:

private ArrayList<Integer> salesList;

says "Eventually, I will create an AraryList that can hold Integers, and I will refer to it by the name salesList".

That's like saying "Someday, i will have a dog and call him 'Rover'". That doesn't mean you HAVE a dog - it just means that someday you will, and you've already picked out the name.

You never create the ArrayList. So, when you further down say "add something into the ArrayList named 'salesList'", the JVM is saying "ummm....WHAT arraylist? There is no such thing!!!"
 
Lu Kim Hui
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:

Lu Kim Hui wrote:Erm... because this is an exercise given from my lecturer, and the requirement is reading from the file and store inside an INTEGER arraylist and lastly print them out ^^"


No. That's the what. You're trying to find out why you're getting an NPE. Have a look at my last question again.

Winston


omg!! miss out the

THANKS ALOT!!!
 
Lu Kim Hui
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

fred rosenberger wrote:This line:

private ArrayList<Integer> salesList;

says "Eventually, I will create an AraryList that can hold Integers, and I will refer to it by the name salesList".

That's like saying "Someday, i will have a dog and call him 'Rover'". That doesn't mean you HAVE a dog - it just means that someday you will, and you've already picked out the name.

You never create the ArrayList. So, when you further down say "add something into the ArrayList named 'salesList'", the JVM is saying "ummm....WHAT arraylist? There is no such thing!!!"



thanks!
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lu Kim Hui wrote:THANKS ALOT!!!


Another satisfied customer. And isn't it much better when you work it out for yourself?

Winston
 
Lu Kim Hui
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:

Lu Kim Hui wrote:THANKS ALOT!!!


Another satisfied customer. And isn't it much better when you work it out for yourself?

Winston



Yup !
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another tip for you: Get used to reading your stacktraces thoroughly.

Go through them and work out exactly which statements are involved and the order they were invoked. Personally, I use a yellow marker pen; but I'm an old fart and I hate looking at large programs on screens (and I recycle my paper ).

Winston
 
Lu Kim Hui
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:Another tip for you: Get used to reading your stacktraces thoroughly.

Go through them and work out exactly which statements are involved and the order they were invoked. Personally, I use a yellow marker pen; but I'm an old fart and I hate looking at large programs on screens (and I recycle my paper ).

Winston



ok thanks man!
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Paper

There are two sorts of paper when I am around.
  • Type A paper: this is white and is a hazard to life and limb because I try to write on it and nobody can read it.
  • Type B paper: this is not all white, but has writing on already, and is a hazard to life and limb because the chances of my losing it are in proportion to the importance of the writing.

  • In my opinion, you should instantiate that List inside the constructorYou should also avoid if (...) b = true ; else b = false; Look here for a better example.
    Are you closing a Scanner using System.in? You shouldn’t do that. To find out why, add this line after line 20
     
    Winston Gutkowski
    Bartender
    Posts: 10780
    71
    Hibernate Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:Type B paper: this is not all white...


    Yup, know that stuff; except I call it type 'T' paper...

    Winston
     
    Lu Kim Hui
    Greenhorn
    Posts: 16
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:Paper

    There are two sorts of paper when I am around.

  • Type A paper: this is white and is a hazard to life and limb because I try to write on it and nobody can read it.
  • Type B paper: this is not all white, but has writing on already, and is a hazard to life and limb because the chances of my losing it are in proportion to the importance of the writing.

  • In my opinion, you should instantiate that List inside the constructorYou should also avoid if (...) b = true ; else b = false; Look here for a better example.
    Are you closing a Scanner using System.in? You shouldn’t do that. To find out why, add this line after line 20



    Thanks for the advice..... i am still a newbie
     
    Campbell Ritchie
    Marshal
    Posts: 79177
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Lu Kim Hui wrote: . . .

    Thanks for the advice..... i am still a newbie

    You’re welcome. Paper should be abolished, except in books and newspapers.
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    reply
      Bookmark Topic Watch Topic
    • New Topic