3

Here is my code:

class Myclass {

    private static int[] array;

    public static void main(String[] args) {
        Myclass m = new Myclass();

        for (int i = 0; i < 10; i++) {
            m.array[i] = i;
            System.out.println(m.array[i]);
        }
    }

    public Myclass() {
        int[] array = new int[10];
    }
}

It throws a java.lang.nullPointerException when trying to do this:

m.array[i] = i;

Can anybody help me please?

5 Answers 5

4

You have declared a local variable array in your constructor, so you're not actually initializing the array declared in Myclass.

You'll want to refer directly to array in the constructor. Instead of

int[] array = new int[10];

Use this

array = new int[10];

Additionally, you've declared array static in the scope of your Myclass class.

private static int[] array;

You only have one instance of Myclass here, so it doesn't matter, but normally this would not be static, if you're initializing it in a constructor. You should remove static:

private int[] array;
Sign up to request clarification or add additional context in comments.

Comments

3

In your constructor you are making your assignment to a local variable names array, not the static class variable also named array. This is a scope problem.

I'm also guessing that since you access array via m.array, you want a member variable and not a static one. Here's the fix

class Myclass {

  private int[] array;

  public static void main(String[] args) {
    Myclass m = new Myclass();
    for (int i = 0; i < 10; i++) {
        m.array[i] = i;
        System.out.println(m.array[i]);
    }
  }

  public Myclass() {
        rray = new int[10];
  }

}

3 Comments

+1, I didn't downvote your answer, but your answer is correct.
@rgettman I wasn't accusing you, at the time every answer was at 0. I also upvoted yours.
That's ok. But I figured others might read it like it was, because at the time my answer was the only one with a positive score.
0

in MyClass() type this

this.array = new int [10];

instead of this

int[] array = new int[10];

Comments

0

Your code should be as below. In the constructor, instead of initializing the instance variable you created a new local variable and the instance variable was left uninitalized which caused the NullPointerException. Also the instance variable shouldn't be static.

class Myclass {

  private int[] array;

public static void main(String[] args) {
 Myclass m = new Myclass();
 for (int i = 0; i < 10; i++) {
    m.array[i] = i;
    System.out.println(m.array[i]);
 }
}

public Myclass() {
      array = new int[10];
}

}

Comments

0

First, if you plan to use array as a field of m (i.e. m.array) don't declare it as static, but:

private int[] array;

Next thing you have to do is to initialize it. Best place to do that is in the constructor:

public MyClass() {
    array= new int[10]; //just array = new int[10]; don't put int[] in front of the array, because the variable already exists as a field.
}

The rest of the code should work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.