null pointer exception problem

Reply

Join Date: Apr 2007
Posts: 10
Reputation: fowlergod09 is an unknown quantity at this point 
Solved Threads: 0
fowlergod09 fowlergod09 is offline Offline
Newbie Poster

null pointer exception problem

 
0
  #1
Apr 21st, 2007
Hi i am very new to java and have come across this problem in my code. Will someone please explain to me why i am getting a null point exception and what one is? I have tried to find out what it means but i cannot find an explanation anywhere.
Thank you

  1. class Main {
  2.  
  3. String s;
  4.  
  5. void initializeStuff() {
  6. String s = "aaa";
  7. }
  8.  
  9. void displayStuff() {
  10. System.out.print("The length of '"+s+"' is: ");
  11. System.out.print(s.length());
  12. }
  13.  
  14. public static void main(String[] args) {
  15. Main main = new Main();
  16. main.initializeStuff();
  17. main.displayStuff();
  18. }
  19. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: null pointer exception problem

 
0
  #2
Apr 21st, 2007
is it string.length, without the brackets?
Last edited by iamthwee; Apr 21st, 2007 at 8:58 am.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: null pointer exception problem

 
0
  #3
Apr 21st, 2007
LOL, where did I get that from? Erm could it be the way you're declaring it? I.e lack of public/private declarations?
Last edited by iamthwee; Apr 21st, 2007 at 9:07 am.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 10
Reputation: fowlergod09 is an unknown quantity at this point 
Solved Threads: 0
fowlergod09 fowlergod09 is offline Offline
Newbie Poster

Re: null pointer exception problem

 
0
  #4
Apr 21st, 2007
I have removed the brackets and get the following errors:
cannot find symbol variable length
Thanks anyway.
G
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: null pointer exception problem

 
0
  #5
Apr 21st, 2007
Originally Posted by fowlergod09 View Post
I have removed the brackets and get the following errors:
cannot find symbol variable length
Thanks anyway.
G
No that was a mistake on my part,I guess my memory isn't that great.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 10
Reputation: fowlergod09 is an unknown quantity at this point 
Solved Threads: 0
fowlergod09 fowlergod09 is offline Offline
Newbie Poster

Re: null pointer exception problem

 
0
  #6
Apr 21st, 2007
I have tried using both public and private declarations but all to no avail. The error message i get from the compiler points to line 11 which is:
System.out.print (s.length());
the full error message i am geting is:
The length of 'null' is: Exception in thread "main" java.lang.NullPointerException
at Main.displayStuff(main.java:11)
at Main.main(main.java:17)
I would be most grateful if you could help me understand why this is happening rather than providing me with the code. I have hit a brick wall with this i am afraid.
Thank you
G
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: null pointer exception problem

 
0
  #7
Apr 21st, 2007
I think I know what you've done. It's saying string 's' has nothing in it. I.e. it is null.

I know you've declared in as String s = "aaa"; but it doesn't work like that. It's to do with public and private scope.

Any basic java tute should tell you where you're going wrong.
Last edited by iamthwee; Apr 21st, 2007 at 9:29 am.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,358
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 252
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: null pointer exception problem

 
1
  #8
Apr 21st, 2007
The problem is, you have declared an instance variable s.

Then in initializeStuff you want to give it a value, but what you really did was declare and define a variable local to initializeStuff rather than defining the instance variable.

Change:
  1. void initializeStuff() {
  2. String s = "aaa";
  3. }
to
  1. void initializeStuff() {
  2. s = "aaa";
  3. }
or
  1. void initializeStuff() {
  2. this.s = "aaa";
  3. }
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: null pointer exception problem

 
0
  #9
Apr 21st, 2007
void initializeStuff() {
String s = "aaa";
}

Good catch, didn't notice that.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 1
Reputation: PriyaJessi is an unknown quantity at this point 
Solved Threads: 0
PriyaJessi PriyaJessi is offline Offline
Newbie Poster

Re: null pointer exception problem

 
0
  #10
Jun 13th, 2008
Originally Posted by fowlergod09 View Post
Hi i am very new to java and have come across this problem in my code. Will someone please explain to me why i am getting a null point exception and what one is? I have tried to find out what it means but i cannot find an explanation anywhere.
Thank you

  1. class Main {
  2.  
  3. String s;
  4.  
  5. void initializeStuff() {
  6. String s = "aaa";
  7. }
  8.  
  9. void displayStuff() {
  10. System.out.print("The length of '"+s+"' is: ");
  11. System.out.print(s.length());
  12. }
  13.  
  14. public static void main(String[] args) {
  15. Main main = new Main();
  16. main.initializeStuff();
  17. main.displayStuff();
  18. }
  19. }
Hi
You have not assigned any value for s which you declred as String s(as an instance variable)
So JVM considers it as null so by giving s.length it cant count the characters of null so you're getting null ponterException.
You just try by assigning some values ti s and see you will get the length of that s variable.
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC