0

I'm trying to make my add method work as you can see from my code below & it's giving me tremendous difficulty figuring out what's wrong with my code.

NodeFN class:

public class NodeFN {
    private String data; // Data for node.
    private NodeFN next; // Next node.

public NodeFN(String data) {
    this.data = data; // Take the data value passed in & store it in the data field.
    this.next = null; // Take the next node & store it in the next field.
}

// Mutator functions.
public String getData() {return data;}
public NodeFN getNext() {return next;}
public void setData(String d) {data = d;}
public void setNext(NodeFN n) {next = n;}
}

Queue class:

public class Queue {
    NodeFN head = null; // Head of node.
    public String n;

public Queue() {} // Default constructor.

public Queue(String n) { 
    head = new NodeFN(n); // head is now an object of NodeFN which holds a string.
}

public void add(String n) {
    NodeFN nn = new NodeFN(n); // nn is now an object of NodeFN which holds a string.
        if(nn == null) { // If the new node (nn) is empty.
            head = nn; // Then allow head to equal to the new node(nn).
        } else {
            // If new node (nn) alphabetically comes first compared to head 
            if(nn.getData().compareTo(head.getData()) < 0) { 
                nn.setNext(head); 
                head = nn;
            }       
        }
    }

public static void main(String[] args) {
    Queue q = new Queue();
    q.add("some string to test to see if it prints in console");

    System.out.println(q);
    }
}
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
pellepelle
  • 27
  • 2
  • 4
  • 2
    Printing objects to debug them, is a bad practice, by the way. You should learn to use a debugger. – OneCricketeer Nov 03 '16 at 21:16
  • 1
    Single step through the execution in a debugger and see what the code you wrote actually does. You will learn a lot by figuring out why it does what it does (rather than what you thought it would do). – Rob Nov 03 '16 at 21:17
  • Where do I get a debugger? – pellepelle Nov 03 '16 at 21:34

3 Answers3

2

Lets start here:

NodeFN nn = new NodeFN(n); 
if(nn == null) { 

Those two lines dont make sense together. In Java, the first line is guaranteed to return something that is not null. The only thing that could happen is: the constructor could throw an exception, but then you would never reach the next line.

Thus: you are never going to take the "then" branch of your if.

Then:

if(nn.getData().compareTo(head.getData())

... will only work ... if head is != null.

But surprise: out of your two constructors for the queue class, only the second one makes sure head is != null.

Long story short: that is how a NullPointerException comes into existence! When you change

Queue q = new Queue();

to

Queue q = new Queue("whatever");

your example shouldn't fail any more. But of course, the code is still buggy then. The key thing to fix: make sure that head is initialized; and make sure that you compare against null ... in those occasions where null is possible!

GhostCat
  • 137,827
  • 25
  • 176
  • 248
0

You should check if head is null:

 if(head == null) {
            head = nn;
        } else {
           ...    
        }

then you should also consider this else condition:

if(nn.getData().compareTo(head.getData()) < 0) { 
    nn.setNext(head); 
    head = nn;
} 
else {
    head.setNext(nn)
}

Finally, you should implement the toString() method in Queue class..

user6904265
  • 1,938
  • 1
  • 16
  • 21
0

New node will never be null, you've jest created it.