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
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
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.
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
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.
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.