| | |
null pointer exception problem
![]() |
•
•
Join Date: Apr 2007
Posts: 10
Reputation:
Solved Threads: 0
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
Thank you
Java Syntax (Toggle Plain Text)
class Main { String s; void initializeStuff() { String s = "aaa"; } void displayStuff() { System.out.print("The length of '"+s+"' is: "); System.out.print(s.length()); } public static void main(String[] args) { Main main = new Main(); main.initializeStuff(); main.displayStuff(); } }
•
•
Join Date: Apr 2007
Posts: 10
Reputation:
Solved Threads: 0
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
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
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.
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*
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:
to
or
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)
void initializeStuff() { String s = "aaa"; }
Java Syntax (Toggle Plain Text)
void initializeStuff() { s = "aaa"; }
Java Syntax (Toggle Plain Text)
void initializeStuff() { this.s = "aaa"; }
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
----------------------------------------------
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
•
•
Join Date: Jun 2008
Posts: 1
Reputation:
Solved Threads: 0
•
•
•
•
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)
class Main { String s; void initializeStuff() { String s = "aaa"; } void displayStuff() { System.out.print("The length of '"+s+"' is: "); System.out.print(s.length()); } public static void main(String[] args) { Main main = new Main(); main.initializeStuff(); main.displayStuff(); } }
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.
![]() |
Similar Threads
- Java Null Pointer Exception (Java)
- help with sort using Calendar class getting null pointer exception (Java)
Other Threads in the Java Forum
- Previous Thread: need help to correct java code
- Next Thread: Error in program
| Thread Tools | Search this Thread |
6 @param actuate android api applet application arc array arrays automation balls binary bluetooth bold business byte c++ chat class client code codesnippet collections compare component coordinates database defaultmethod detection doctype dragging ebook eclipse educational error file fractal froglogic game givemetehcodez graphics gui guitesting helpwithhomework hql html ide ideas image ingres input integer internet intersect invokingapacheantprogrammatically j2me java javaexcel javaprojects jni jpanel jtextarea julia linux list map method methods mobile mysql netbeans newbie nextline parameter php pong problem program programming project recursion recursive scanner sell server set sms sort sql string sun swing swt terminal threads tree web websites windows






