1

I am trying to build a linked list as is shown below:

**list-> [C] - [B] - [A] -> NULL**

Here is my main:

import java.util.*; 

class LinkedListF
{
    public static void main (String [] args)
    {    
        Node aNode = new Node ("A", null);
        Node bNode = new Node ("B", aNode);
        Node list = new Node ("C",bNode);  

        DynamicList dynamicList = new DynamicList();   
        dynamicList.insertFirst("A");
        dynamicList.insertFirst("C");
        dynamicList.insertAfter(list, "B");
        //the line above is where I am struggling with.

        System.out.println(dynamicList.getList().getInfo());
        System.out.println(dynamicList.getList().getNext().getInfo());
        System.out.println(dynamicList.getList().getNext().getNext().getInfo());
    }
}

The problem I am having now, as I mark in the codes, is when the code "dynamicList.insertAfter(list, "B")" runs, it does not insert "B" after the Node "list" in the dynamicList that I construct. I am very confused about what is happening here.

My output looks like this:

----jGRASP exec: java LinkedListF

C
A
Exception in thread "main" java.lang.NullPointerException
    at LinkedListF.main(LinkedListF.java:27)

 ----jGRASP wedge2: exit code for process is 1.
 ----jGRASP: operation complete.

The line 27 is my third System.out.print statement.

Below is the insertAfter method in my DynamicList class:

public void insertAfter(Node p, String x)
{
    if (isEmpty())
    {
        System.out.println("VOID INSERTION.");
        System.exit(1);
    }

    Node q = new Node (x, p.getNext());
    p.setNext(q);
}

Below is the Node class that I use to construct each node:

class Node
{
    private String data;
    private Node next;

    public Node (String data, Node next)
    {
        this.data = data;
        this.next = next;
    }

    String getInfo()
    {
        return data;
    }

    Node getNext()
    {
        return next;
    }

    void setInfo(String data)
    {
        this.data = data;
    }

    void setNext(Node next)
    {
        this.next = next;
    }
}
xsami
  • 1,312
  • 16
  • 31
  • 1
    "java.lang.NullPointerException at LinkedListF.main(LinkedListF.java:27)". What is on line 27 ? – Thilo Apr 06 '15 at 19:51
  • @Thilo *"The line 27 is my third System.out.print statement."* – m0skit0 Apr 06 '15 at 19:55
  • 2
    You do understand that `list` is never added to `dynamicList`, right? – azurefrog Apr 06 '15 at 19:58
  • There is a difference between `list` and the node created by `insertFirst("C")` – Thilo Apr 06 '15 at 19:59
  • @azurefrog Yeah, it's a bit strange way to do it, the method could be in fact static, but as I see it, it is added to `dynamicList` because he passes the node `list` (which is in `dynamicList`). – m0skit0 Apr 06 '15 at 20:00
  • 1
    @m0skit0 He adds a new "B" node after `list`, but `list` has never been added to `dynamicList`. the `insertAfter()` method only adds to the `Node` passed to it, not to any node already in the `DynamicList`. He's created `list->"B"->bNode->aNode` on their own. `dynamicList` only consists of `"C"->"A"`. – azurefrog Apr 06 '15 at 20:03
  • @azurefrog Yeah, just saw that, that's right, I think you can answer him. – m0skit0 Apr 06 '15 at 20:04
  • @m0skit0 It's done. Any suggestions? – azurefrog Apr 06 '15 at 20:29

1 Answers1

1

Problem

The problem is that you are creating several bare Node objects, which are not part of dynamicList, so your list is shorter than you think it is, and your print statements are running off the end.

Let's look at what your code is actually doing:

 Node aNode = new Node ("A", null);
 Node bNode = new Node ("B", aNode);
 Node list = new Node ("C", bNode);  

This will create three Nodes which are in a chain

list -> bNode -> aNode -> null

You then create a DynamicList and add a couple Strings to it:

 DynamicList dynamicList = new DynamicList();   
 dynamicList.insertFirst("A");
 dynamicList.insertFirst("C");

You haven't shown the code for insertFirst(String) but we can safely assume it creates a new Node for each String you add, which leaves you with this:

(stand-alone)  list -> bNode -> aNode -> null
(dynamicList)  "C" -> "A" -> null

Now let's examine what happens when you dynamicList.insertAfter(list, "B");

First the method checks to see if the list is empty (which it isn't, since you've added "A" and "C" to it). However, it then creates a new Node based on the String you give it and inserts it after the parameter Node. Nowhere does it check to see if that Node actually exists in the DynamicList.

This means that you end up with this:

(stand-alone)  list -> "B" -> bNode -> aNode -> null
(dynamicList)  "C" -> "A" -> null

This is what is leading to the NPE when you try to print:

System.out.println(dynamicList.getList().getInfo());
(dynamicList)  "C" -> "A" -> null
               ^^^                  prints "C"

System.out.println(dynamicList.getList().getNext().getInfo());
(dynamicList)  "C" -> "A" -> null
                      ^^^           prints "A"

System.out.println(dynamicList.getList().getNext().getNext().getInfo());
(dynamicList)  "C" -> "A" -> null
                             ^^^    null has no info to get

Solution

Instead of calling the insert method which takes a String and creates a new Node, you need to insert the Nodes you've already created (assuming such methods exist):

 Node aNode = new Node ("A", null);
 Node list = new Node ("C", aNode);
 // creates list ("C") -> aNode ("A") -> null

 DynamicList dynamicList = new DynamicList();
 dynamicList.insertFirst(list);
 // dynamicList = list ("C") -> aNode ("A") -> null
 dynamicList.insertAfter(list, "B");
 // dynamicList = list ("C") -> "B" -> aNode ("A") -> null

An even simpler solution would be to never manually create any Node and just let the DynamicList do all the work:

 DynamicList dynamicList = new DynamicList();
 dynamicList.insertFirst("A");
 dynamicList.insertFirst("B");
 dynamicList.insertFirst("C");
 // dynamicList = "C" -> "B" -> "A" -> null
azurefrog
  • 10,785
  • 7
  • 42
  • 56
  • Hello azurefrog, thank you for your answer! I tried the second solution you mentioned and it worked! I used "insertAfter" method because I want to make sure this method works too. – bluecheese2008 Apr 06 '15 at 22:00
  • Hello azurefrog, I followed you suggestion to create an "insertMethod (Node x)." This method takes Nodes as parameters. And this is what I have: dynamicList.insertFirst(list); dynamicList.insertAfter(list,"A"); dynamicList.insertAfter(list, "B"); It is working. Is it because now when I run 'insertAfter(list, "A") for instance, Java knows the Node "list" exists in the dynamicList, so list is relatable to the dynamicList? – bluecheese2008 Apr 06 '15 at 22:30