0

I am trying to create my own LinkedList in Java and so far it is working as expected. It can take inputs and print them. But, there is a remove method that takes the index as the argument that the user wants to remove from the LinkedList which is throwing an error. Please help me get rid of that error or otherwise suggest how to implement the remove method to make it work correctly.

MyLinkedList.java

import Node.*;

public class MyLinkedList {
    Node head;
    int val;

    public MyLinkedList(int val){
        this.head = new Node(val, null);
    }

    public void insert(int val){
        if (this.head == null){
            this.head = new Node(val, null);
        }
        else{
            Node node = this.head;
            while (node.next!=null){
                node = node.next;
            }
            node.next = new Node(val, null);
        }
    }

    public void remove(int index){
        int count = 0;
        // int size = 0;
        Node node = this.head;

        while(node!=null){
            node = node.next;
            count++;
        }

        if (index == count){
            node = this.head;
            count = 0;
            while (count<index){
                node = node.next;
                count++;
            }
            node.next = null;
            return;
        }

        while(count<index){
            node = node.next;
            count++;
        }
        Node temp;
        temp = node.next;
        node = temp;
    }

    public void print(){
        Node node = this.head;
        while (node != null){
            System.out.println(node.val);
            node = node.next;
        }
    }

    public static void main(String[] args) {
        MyLinkedList ll = new MyLinkedList(10);
        ll.insert(20);
        ll.insert(30);
        ll.insert(40);
        ll.insert(50);
        ll.print();
        ll.remove(3); //error
        ll.print();
    }
}

Node.java is defined inside a folder named "Node" (package)

package Node;

public class Node {
    public int val;
    public Node next = null;

    public Node(int val, Node next){
        this.val = val;
        this.next = next;
    }
}

Output:

10
20
30
40
50
Exception in thread "main" java.lang.NullPointerException: Cannot read field "next" because "<local3>" is null
        at MyLinkedList.remove(MyLinkedList.java:50)
        at MyLinkedList.main(MyLinkedList.java:69)

Thank you for any help : )

Shubham Nanche
  • 130
  • 2
  • 11
  • 2
    Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – OH GOD SPIDERS Oct 21 '21 at 13:20
  • have you used a debugger? – the.Doc Oct 21 '21 at 13:20
  • @OHGODSPIDERS I have no idea why it is throwing NullPointerException and I am not able to comprehend the info in that link. I know that somewhere in the code there is a "null" pointer causing the error but don't know where. Please help me. – Shubham Nanche Oct 21 '21 at 13:39
  • 2
    It tells you in the stack trace that you posted in your question. Namely line 50 in file `MyLinkedList.java`. In one of the lines of code in method `remove`, where you wrote `node.next`, the `node` is null. Maybe this will help: [What is a stack trace, and how can I use it to debug my application errors?](https://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors) If you are using an IDE (like Eclipse or IntelliJ) then it has a debugger. You should learn how to use it to debug your code. – Abra Oct 21 '21 at 13:50
  • Ok. I will try to learn how to debug my code. I have used VS Code for everything so far be it Python or C++ or Java. But, in the case of Java, I have faced a lot of obstructions while running the code :( don't know why. Thank you, @Abra. – Shubham Nanche Oct 21 '21 at 13:58

0 Answers0