Genzi
Genzi

Reputation: 83

NullPointerException when access to array in class

I am new to this forum and Java. The code below compiles but when I try enter a value to a variable I get NullPointerException. What is wrong?

class output_harm
{
    public int[] timestamp;
    public int[] state;

    public output_harm(){
        timestamp = new int[8];
        state = new int[8];
    }
}

output_harm[][] outputs =  new output_harm[7][6];   

outputs[0][0].state[0] = 0; //java.lang.NullPointerException

Upvotes: 3

Views: 117

Answers (6)

Problem output_harm[][] outputs = new output_harm[7][6];

This is just to initialize an array. When you call constructor output_harm() then only it will do initialization. state = new int[8]; here state is initialized in constructor and lead to NullPointerException.

Solution: First you need to initialize an object for each output_harm(if you need total array to be initialized)

output_harm[][] outputs =  new output_harm[7][6];   
    for(int i=0;i<7;i++){
        for(int j=0;j<6;j++){
            outputs[i][j] = new output_harm();
        }
    }

    outputs[0][0].state[0] = 1;

Upvotes: 2

earthmover
earthmover

Reputation: 4525

in your line

output_harm[][] outputs =  new output_harm[7][6];   

you have only initialized your array, the array doesn't contain any value till now.

and, after that you called

outputs[0][0].state[0] = 0;

which leads you to the NullPointerException.

After array initialization, it contains default values (for your array-type [output_harm] it will be null) and you called a variable on null, which throws NullPointerException.

So, first you have to initialize your array index which you are using, in your case, do

outputs[0][0] = new output_harm();

then outputs[0][0].state[0] = 0;

Upvotes: 1

Macrosoft-Dev
Macrosoft-Dev

Reputation: 2185

Defining Array means that now output_harm is not null. But

output_harm[][] outputs =  new output_harm[7][6];   

outputs[0][0].state[0] = 0; //java.lang.NullPointerException

is accsessing the first index which is null yet . so it will be nullPointerException

Upvotes: 1

Pratik Shelar
Pratik Shelar

Reputation: 3212

Just a pointer for you to work on.

  output_harm[][] outputs =  new output_harm[7][6];   
  outputs[0][0] =  new output_harm();
  outputs[0][0].state[0] =  0;
  outputs[0][0].state[1] =  1;

Upvotes: 1

RMachnik
RMachnik

Reputation: 3694

In line

outputs[0][0].state[0] = 0; //java.lang.NullPointerException

your outputs[0][0] is null so you can not perform any operation on null so this is the root cause of NPE.

Please be sure that you create that object before that line so simply paste before sth like this:

outputs[0][0] = new output_harm();

Upvotes: 1

Baby
Baby

Reputation: 5102

Doing output_harm[][] outputs = new output_harm[7][6] without initialize the values, all values will be set to default, which is null

You need to add:

outputs[0][0]=new output_harm();

then you can do:

outputs[0][0].state[0] = 0;

Upvotes: 1

Related Questions