944,126 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 17876
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Apr 21st, 2007
0

null pointer exception problem

Expand 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

Java Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
fowlergod09 is offline Offline
10 posts
since Apr 2007
Apr 21st, 2007
0

Re: null pointer exception problem

is it string.length, without the brackets?
Last edited by iamthwee; Apr 21st, 2007 at 8:58 am.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Apr 21st, 2007
0

Re: null pointer exception problem

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.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Apr 21st, 2007
0

Re: null pointer exception problem

I have removed the brackets and get the following errors:
cannot find symbol variable length
Thanks anyway.
G
Reputation Points: 10
Solved Threads: 0
Newbie Poster
fowlergod09 is offline Offline
10 posts
since Apr 2007
Apr 21st, 2007
0

Re: null pointer exception problem

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.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Apr 21st, 2007
0

Re: null pointer exception problem

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
fowlergod09 is offline Offline
10 posts
since Apr 2007
Apr 21st, 2007
0

Re: null pointer exception problem

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.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Apr 21st, 2007
1

Re: null pointer exception problem

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:
Java Syntax (Toggle Plain Text)
  1. void initializeStuff() {
  2. String s = "aaa";
  3. }
to
Java Syntax (Toggle Plain Text)
  1. void initializeStuff() {
  2. s = "aaa";
  3. }
or
Java Syntax (Toggle Plain Text)
  1. void initializeStuff() {
  2. this.s = "aaa";
  3. }
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
Apr 21st, 2007
0

Re: null pointer exception problem

void initializeStuff() {
String s = "aaa";
}

Good catch, didn't notice that.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Jun 13th, 2008
0

Re: null pointer exception problem

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

Java Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
PriyaJessi is offline Offline
1 posts
since Jun 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: how can i solve this ?
Next Thread in Java Forum Timeline: error when writing objexts to file





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC