Member Avatar for kubiak

Please help me with the double linked list, when I write the command in terminal to test main class by my source code the cmd.exe throw me:

Exception in thread "main" java.lang.NullPointerException
        at homework.List.add(Homework2.java:19)
        at homework.Homework2Main.main(Homework2Main.java:20)

In 19 line is something wrong in source code, I tag him. Please I need help.

Code:

package homework;

class Node {
    Node previous, next;
    int contents;

    Node(int val) {
        this.contents = val;
    }
}

class List {
    Node first;

    void add(int num) {
        Node x = new Node(num);
        if(first.next == null) {                  //this line 
            this.first = x;
        }else{
            Node current = this.first;
            boolean inList = false;

            while(current.next != null) {
                if(current.contents == num) inList = true;
                current = current.next;
            }

            if(!inList) {
                current.next = x;
                x.previous = current;
            }
        }
    }

    void remove(int num) {
        Node current = this.first;

        while(current.next != null) {
            if(current.contents == num) {
                if(current.previous != null) {
                    current.previous.next = current.next;
                }
                if(current.next != null) {
                    current.next.previous = current.previous;
                }
            }
            current = current.next;
        }
    }

    boolean contains(int num) {
        Node current = this.first;

        while(current.next != null) {
            if(current.contents == num) return true;
            current = current.next;
        }

        return false;
    }
}

Recommended Answers

All 5 Replies

Is this line 19:

if(first.next == null) { //this line 

If yes then according to the exception you are trying to use something which is null. In this case I assume that it is the "first"

I don't know your logic but, maybe you need this:

if(first == null) {
  ...
} else { ...

OR

if(first == null || first.next==null) {
  ...
} else { ...

In any case you need to check if "first" is null and/or first.next before you use either one.

And use code tags. Press the button (code) when you make your post

Member Avatar for kubiak

I use this offers, and doesn't work... I have some Homewrok2Main.class and my source code called Homework2.java.All that in one file called homework. In terminal I enter the command java -cp../ homework.Homework2Main and the terminal throw me same expression of exception... I don't know what to do, and I think the code is correct.

The line 15, you should initiate

Node first=null;

Then line 19 should be only if(first==null).

Please use 'code' tag provided by the forum. It's very difficult to read your code in plain text.

Member Avatar for kubiak

Here is a code

package homework;

class Node {
    Node previous, next;
    int contents;

    Node(int val) {
        this.contents = val;
    }
}

class List {
    Node first;

    void add(int num) {
        Node x = new Node(num);
        if(first.next == null) {
            this.first = x;
        }else{
            Node current = this.first;
            boolean inList = false;

            while(current.next != null) {
                if(current.contents == num) inList = true;
                current = current.next;
            }

            if(!inList) {
                current.next = x;
                x.previous = current;
            }
        }
    }

    void remove(int num) {
        Node current = this.first;

        while(current.next != null) {
            if(current.contents == num) {
                if(current.previous != null) {
                    current.previous.next = current.next;
                }
                if(current.next != null) {
                    current.next.previous = current.previous;
                }
            }
            current = current.next;
        }
    }

    boolean contains(int num) {
        Node current = this.first;

        while(current.next != null) {
            if(current.contents == num) return true;
            current = current.next;
        }

        return false;
    }
}

It doesnt help.

As I already said before, you use an object which is null. Before you use the "first" you need to check if it is null or not. The line tells you where the error is.

And of course Taywin gave you the same suggestion

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.