0

Node

private Object data;
private Node link;
private Node next;
private Node prev;

public Object getData() {
    return data;
}

public void setData(Object data) {
    this.data = data;
}

public Node getLink() {
    return link;
}

public void setLink(Node link) {
    this.link = link;
}

public Node(Object data) {
    this.data = data;
    this.link = null;
}

public Node getNextNode() {
    return next;
}
public Node getPrevNode() {
    return prev;
}
public void setNextNode(Node n) {
    next = n;
}
public void setPrevNode(Node n) {
    prev = n;
}

Item

private int id;
private String name;
private String type;
private double price;

public Item(int id, String name, String type, double price) {
    this.id = id;
    this.name = name;
    this.type = type;
    this.price = price;
}

public int getId() {
    return id;
}

public String getName() {
    return name;
}

public String getType() {
    return type;
}

public double getPrice() {
    return price;
}

public void setId(int id) {
    this.id = id;
}

public void setName(String name) {
    this.name = name;
}

public void setType(String type) {
    this.type = type;
}

public void setPrice(double price) {
    this.price = price;
}

@Override
public String toString() {
    return "Item: " + "ID: " + id + ", Name: " + name + ", Type: " + type + ", Price: " + price;
}

LinkedList

private Node head;  // first node in the linked list
private int count;

public int getCount() {
    return count;
}

public Node getHead() {
    return head;
}

public LinkedList() {
    head = null;    // creates an empty linked list
    count = 0;
}

public void addFront(int n) {
    Node newNode = new Node(n);

    newNode.setLink(head);
    head = newNode;

    count++;
}

public void deleteFront() {
    if (count > 0) {
        head = head.getLink();
        count--;
    }
}

public void AddItemToFront(Item p) {

    Node newNode = new Node(p);
    newNode.setLink(head);
    head = newNode;

    count++;
}

public void DisplayItems() {
    Node temp = head;
    while(temp != null) {
        System.out.println(temp.getData());
        temp = temp.getLink();
    }
}

public void RemoveItemAtPosition(int n) {
    if(n == 1) {
        Node x = head;
        head = x.getLink();
        count--;
    }
    else if (n > count || n < 0) {
        System.out.println("The index you entered is out of bound.");
    }
    else {
     Node x = head;
     for (int i = 1; i < n; i++) {
        x = x.getNextNode();
        }

     Node temp = x;
     x = temp.getPrevNode();
     x.setNextNode(temp.getNextNode());
     temp = null;
     count--;
    }
}

I'm trying to remove a Node at a position given integer n. I tried researching on SO before posting here and the above is the code that i came out with. However, the code returned me an error saying >java.lang.NullPointerException at LinkedList.java:74 at main:35

The Node is actually an object that is being added to the LinkedList

X-men
  • 121
  • 1
  • 2
  • 8

2 Answers2

0

I checked your code I see that you have some bugs.

your mistakes are:

1 -you use linked object.

2- try to access previous and next without initialization.

3- you only care on the next node.

I removed link form Node class and I do some changes into LinkedList:

 public class LinkedList {
    private Node head;  // first node in the linked list
    private int count;

    public int getCount() {
        return count;
    }

    public Node getHead() {
        return head;
    }

    public LinkedList() {
        head = null;    // creates an empty linked list
        count = 0;
    }

    public void addFront(int n) {
        Node newNode = new Node(n);

        if (head == null) {
            head = newNode;
        } else {
            Node node = head;
            head = newNode;
            head.setNextNode(node);
            node.setPrevNode(head);
        }

        count++;
    }

    public void deleteFront() {
        if (count > 0) {
            head = head.getNextNode();
            head.setPrevNode(null);
            count--;
        }
    }

    public void AddItemToFront(Item p) {
        Node newNode = new Node(p);

        if (head == null) {
            head = newNode;
        } else {
            Node node = head;
            head = newNode;
            head.setNextNode(node);
            node.setPrevNode(head);
        }

        count++;
    }

    public void DisplayItems() {
        Node temp = head;
        while (temp != null) {
            System.out.println(temp.getData());
            temp = temp.getNextNode();
        }
    }

    public void RemoveItemAtPosition(int n) {
        if (n == 1) {
            deleteFront();
        } else if (n > count || n < 0) {
            System.out.println("The index you entered is out of bound.");
        } else {
            Node x = head;
            for (int i = 1; i < n; i++) {
                x = x.getNextNode();
            }

            Node temp = x;
            temp.getPrevNode().setNextNode(temp.getNextNode());
            temp.getNextNode().setPrevNode(temp.getPrevNode());
            temp = null;
            count--;
        }
     }
   }
Ahmad Al-Kurdi
  • 2,248
  • 3
  • 23
  • 39
0

When checking the provided source code, in the function AddItemToFront(Item p) only the linked-list is managed with newNode.setLink(head);. Both Node next; and Node prev; are never initialized and never used before in the function removeItemAtPosition(int n).

Warning: your Linked-List is managed on reverse (due to the function void AddItemToFront(Item p)).

A simple way to solve your problem should to use only Node link; also in the function removeItemAtPosition(int n).

Step 1 - in RemoveItemAtPosition(int n), modify the search of the nth Node in the for-loop

 Node x = head;
 for (int i = 1; i < n; i++) {
    x = x.getLink(); // Use Node link
    }

Instead of

 Node x = head;
 for (int i = 1; i < n; i++) {
    x = x.getNextNode();
    }

Step 2 - in RemoveItemAtPosition(int n), connect the next node link to the node before

 Node temp = x.getLink();
 x.setLink(temp.getLink());
 count--;

Instead of

 Node temp = x;
 x = temp.getPrevNode();
 x.setNextNode(temp.getNextNode());
 temp = null;
 count--;
J. Piquard
  • 1,665
  • 2
  • 12
  • 17