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

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

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

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

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

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.

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";
    }
Comments
His reply was a great help. Thankyou
void initializeStuff() {
String s = "aaa";
}

Good catch, didn't notice that.

Edited 3 Years Ago by happygeek: fixed formatting

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.

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

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.

Edited 3 Years Ago by mike_2000_17: Fixed formatting

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

Edited 3 Years Ago by Reverend Jim: Fixed formatting

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

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

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

This article has been dead for over six months. Start a new discussion instead.