Java Node array node mutation error - Null pointer

Saif Mahmud :

I know this is a very simple question/answer. However I just wanted to be sure of the appropriate answer. Why can't I change the node with this addNode method? my code:

public class NODETEST {
public static void main(String[] args) {
    Table myTable = new Table(13);

    String word = "myWord";
    int index = 0;

    myTable.addNode(word, index);
    System.out.println(myTable.array[index].item);
    }
}// end NodeTest
class Table {
    Node[] array;
    public Table(int size) 
    {
        array = new Node[size];
        for(Node x: array)
        {
            x = null;
        }
    }
    public void addNode(String word, int index) {
        Node curr = this.array[index];
        if(curr == null)
        {
            curr = new Node(word, null);
        }
    }
}// table class
class Node {
    String item;
    Node next;
    public Node (String item, Node next)
    {
        this.item = item;
        this.next = next;
    }
}// Node class

output ->

Exception in thread "main" java.lang.NullPointerException
at NODETEST.main(NODETEST.java:10)

if I change this line inside the addNode method =>

curr = new Node(word, null);

with this line=>

this.array[index] = new Node(word, null);

It works.

I know there is something with this curr variable. It has to be something related to the reference. What's exactly happening here?

If I can not use a curr variable, how can I add the 3rd or 4th node to the same array[index]; like a linked list. Because, If I have to use "this.array[index]" to directly access, how can I access/modify the 9th or 10th Node at this exact array[index]?

Maybe it's basic fundamentals, however, help would be appreciated.

Mark :

This is because that's how java referencing behaves. I'll break down step by step below

Node curr = this.array[index];

The above code tells the curr variable to point to the memory address of this.array[index];. Thus the value of cur will solely depend on the value of this.array[index];. So whatever changes you do in this.array[index]; it will reflect in curr.

curr = new Node(word, null);

However the above code sets curr to point another memory address which is the memory address of new Node(word, null);. Thus from that code there's no more relationship regarding curr and this.array[index];, so whatever you do to curr will not affect the value of this.array[index]; and vice versa.

If I can not use a curr variable, how can I add the 3rd or 4th node to the same array[index]; like a linked list. Because, If I have to use "this.array[index]" to directly access, how can I access/modify the 9th or 10th Node at this exact array[index]?

You have just to modify your addNodefunction to this. Note that this just appends a node to the last node of that index.

public void addNode(String word, int index) {
    this.array[index];
    if(this.array[index] == null)
    {
        this.array[index] = new Node(word, null);
    } else {
        Node curr = this.array[index]
        // loop so that you can have the address of the last node
        // then stop once the .next is null since this means that it is the last node
        while(curr.next != null){
            curr = curr.next;
        }
        curr.next = new Node(word, null); // set the node
    }
}

NOTE: If you want to add a node in between nodes in that index then you'll have to edit my code above. I'm not gonna spoon feed you though, thus just try to implement it on your own and post another question in SO if you have any problems.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=300179&siteId=1