md1hunox
md1hunox

Reputation: 3955

NullPointerException while accessing method in an object in 2 dimensional array

I'm trying to create a 2 dimensional array of "Node" objects as follows

    public static void main(String[] args) throws IOException {
            length=getNumber("Enter the length of the field: ");
            breadth=getNumber("Enter the breadth of the filed: ");
            node n = new node();
            node [][] field = new node[length][breadth];
            for(i=0;i<=length;i++){
                for(j=0;j<=breadth;j++){
                    F =getNumber("Enter the F value");
                    field[i][j].setF(F);
                    System.out.println("  "+field[i][j].getF(F);
                }
            }

    }

in above code getNumber is a function wherein i print and accept the number Here is my node class:

public class node {
public int F;
public int G;
public int H;
public boolean isVisited;
public boolean isCurrent;
public void node(int F,int G,int H,boolean isVisited, boolean isCurrent){
    this.F=F;
    this.G=G;
    this.H=H;
    this.isVisited=isVisited;
    this.isCurrent=isCurrent;

}
public int getF() {
    return G+H;
}
public void setF(int f) {
    F = f;
}
public int getG() {
    return G;
}
public void setG(int g) {
    G = g;
}
public int getH() {
    return H;
}
public void setH(int h) {
    H = h;
}
public boolean isVisited() {
    return isVisited;
}
public void setVisited(boolean isVisited) {
    this.isVisited = isVisited;
}
public boolean isCurrent() {
    return isCurrent;
}
public void setCurrent(boolean isCurrent) {
    this.isCurrent = isCurrent;
}

}

all i want to do is, to store/access various values of F,G,H etc in each of the node objects, the problem however is i'm getting java.lang.NullPointerException for field[i][j].setF(F); i dont know where i'm going wrong, need some help.

Upvotes: 1

Views: 412

Answers (2)

Grims
Grims

Reputation: 807

Try this:

for(i=0;i<=length;i++){
    for(j=0;j<=breadth;j++){
        F =getNumber("Enter the F value");

        node tmp = new node();
        tmp.setF(F);

        field[i][j] = tmp;
        System.out.println("  "+field[i][j].getF(F);
    }
}

PS in java it is convention for class names to start with a capital and should be written in CamelCase

[edit] Be careful with your get/setF() functions as they do not operate on the same variables

not related to your question, but you might want to read through this document this will teach you about naming conventions in java and help you write code that is easier to read

Upvotes: 0

hvgotcodes
hvgotcodes

Reputation: 120168

You initialized the array, but you did not populate it.

Consider this line:

field[i][j].setF(F);

When you do

field[i][j]

you are accessing the array; i.e. getting what is in the array at that position. Since you didn't put anything in the array, you get a null. But you immediately try to call setF.

I noticed you do

node n = new node();

outside the loop. You probably want to do that in the loop.

node n = new node();
n.setF(F);
field[i][j] = n;

This code creates a node instance, sets a value on it, and then puts it in the array at the specified position. A bit more fancy approach would be to do something like

node n = field[i][j];
if (n == null) { // initialize n at the position if it doesn't exist
  n = new node();
  field[i][j] = n;
}

field[i][j].setF(f);

Alternatively, you could loop over the array and put a new node at each position, right after you initialize the array.

Finally, in Java standard practice is to start class names with capital letters. node should be Node.

Upvotes: 2

Related Questions