954,510 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

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

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();
    }
}
fowlergod09
Newbie Poster
10 posts since Apr 2007
Reputation Points: 10
Solved Threads: 0
 

is it string.length, without the brackets?

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

LOL, where did I get that from? Erm could it be the way you're declaring it? I.e lack of public/private declarations?

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

I have removed the brackets and get the following errors:
cannot find symbol variable length
Thanks anyway.
G

fowlergod09
Newbie Poster
10 posts since Apr 2007
Reputation Points: 10
Solved Threads: 0
 
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.:D

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

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

fowlergod09
Newbie Poster
10 posts since Apr 2007
Reputation Points: 10
Solved Threads: 0
 

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.

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

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:

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

to

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

or

void initializeStuff() {
        this.s = "aaa";
    }
masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

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

Good catch, didn't notice that.

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

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

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();
    }
}

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.

PriyaJessi
Newbie Poster
1 post since Jun 2008
Reputation Points: 10
Solved Threads: 0
 
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.

The thread has already been answered and apparently solved, not to mention that is is 1 year old. Don't give the same answer as a previous post

javaAddict
Nearly a Senior Poster
Team Colleague
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
 
class a { String s = "aaa"; 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) { a main = new a(); main.initializeStuff(); main.displayStuff(); } }

Use this.

jeganjegan
Newbie Poster
1 post since Aug 2009
Reputation Points: 10
Solved Threads: 0
 

you have already intialized s as String. so dont use String s in void initializeStuff() method .just type the program given below

class Main { 
    
    String s;
    
    void initializeStuff() {
         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();
    }
}


[/QUOTE]

selvi_madathi
Newbie Poster
1 post since Nov 2009
Reputation Points: 10
Solved Threads: 0
 
The thread has already been answered and apparently solved, not to mention that is is 2 year old.

Also, the initializations usually take place inside the Constructor or during declaration:

class Main { 
    
    String s = "aaa";
    
    public Main() {

    }
    
    void displayStuff() {
        System.out.print("The length of '"+s+"' is: ");
        System.out.println(s.length());
    }
}


Or

class Main { 
    
    String s = null;
    
    public Main() {
           s = "aaa";
    }
    
   public Main(String s) {
           this.s = s;
    }

    void displayStuff() {
        System.out.print("The length of '"+s+"' is: ");
        System.out.println(s.length());
    }
}
javaAddict
Nearly a Senior Poster
Team Colleague
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
 

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

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 intiolized the String object 's' inside a local method called...initializeStuff() ...... But 's' is instance variable...so after completing the execution of the this local variable the 's' will hove nothing...in the sence "Null"....if u use the S.O.P statement inside the initializeStuff() method, it will work.....and prints 3...
so assign a value to 's' variable where u have defined it....then it wil give answer to you.....
otherwise it will be an error.........
thank you

penumallikoti
Newbie Poster
1 post since Feb 2010
Reputation Points: 10
Solved Threads: 0
 
You intiolized the String object 's' inside a local method called...initializeStuff() ...... But 's' is instance variable...so after completing the execution of the this local variable the 's' will hove nothing...in the sence "Null"....if u use the S.O.P statement inside the initializeStuff() method, it will work.....and prints 3... so assign a value to 's' variable where u have defined it....then it wil give answer to you..... otherwise it will be an error......... thank you


Again, you responded to a VERY old thread were the answer you given has already been provided by others, without contributing anything

javaAddict
Nearly a Senior Poster
Team Colleague
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You