-1

I wish to implement a Queue based in a simple linked list class, without using java.util.

When I call the addEnd method in List class through enqueue method, I receive a java.lang.NullPointerException, though I expect the second element.

Which solution can I take?

The node class

public class Node {
    private int value;
    private Node next;

    public Node(int val) { 
        value = val; 
    }

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

    public Node(Node next) { 
        this.next=next;   
    }

    public int getValue() {
        return value;
    }

    public Node getNext() {
        return next;
    }

    public void setNext(Node next) {
        this.next = next;
    }

    public void displayNode() { 
        System.out.print(" "+value+" "); 
    }
}

My interface

public interface MyQueue {
    void enqueue(int oVal);
    int dequeue();
}

The List

public class List {
    private Node first;
    private Node last;
    private int counter;

    public List() {
        first = null;
        last = null;
    }

    public boolean isEmpty() { 
        return first==null; 
    }

    public void addEnd(int val) {
        Node n1 = new Node(val);
        if( isEmpty() ) {
            first = n1;
        } else {
            last.setNext(n1);
            last = n1;
        }
    }

    public int deleteStart() {
        int temp = first.getValue();
        if(first.getNext() == null){
            last = null;
            first = first.getNext();
        }
        return temp;
    }

    public void displayList() {
        Node current = first;
        while(current != null) {
            current.displayNode();
            current = current.getNext();
        }
        System.out.println("");
    }

    public int size() {
        return counter;
    }
}

The Queue

public class Queue implements MyQueue {
    private List listQ;

    public Queue() {
        listQ = new List(); 
    }

    public boolean isEmpty() { 
        return listQ.isEmpty(); 
    }

    public void enqueue(int oVal) { 
        listQ.addEnd(oVal); 
    }

    public int dequeue() { 
        return listQ.deleteStart(); 
    }

    public void displayQueue() {
        System.out.print("Queue  ");
        listQ.displayQueue();
    }
}



public class App {
    public static void main(String[] args) {
        Queue q1 = new Queue();
        System.out.println("Two insertions");
        q1.enqueue(4);
        q1.enqueue(64);
        q1.displayQueue();
        System.out.println("Insert at the end : ");
        q1.enqueue(23);
        q1.displayQueue();
        System.out.println("Delete an element at the begining of the queue");
        q1.dequeue();
        q1.displayQueue(); 
    }
}
Ilya Rochev
  • 157
  • 1
  • 3
  • 13
  • 1
    Welcome to SO:SE. There is no need to provide all the code for locating and fixing a *NPE*. Run a debugger, locate the object which is null, understand why it is null (at this stage, you can ask for help here). See [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – mins Apr 24 '15 at 17:15
  • Thank you very much! I'll try as you said, next time – eduardo fadrqiue garcía May 09 '15 at 18:50

2 Answers2

1

What @pens-fan-69 said is true. I'd like to add on to that. In order to make your code work, all you have to do is make sure last is set to first during the first insert:

 public void addEnd(int val) {
   Node n1 = new Node(val);
       if( isEmpty() ) {
          first=last=n1;
        } else {
            last.setNext(n1);
            last = n1;
        }             
   }

I tried running the code in online compiler and it works: http://goo.gl/99FyfY

attaboy182
  • 2,039
  • 3
  • 22
  • 28
0

You need to set the last reference when inserting to the empty list. The NullPointerException is because you use last before ever setting it.

pens-fan-69
  • 979
  • 7
  • 11