0

I am trying to make a LinkedSet object class that implements a modified Set interface. I am getting a NullPointerException when I try and check if the firstNode is pointing to null or not. I'm not really sure how to solve this issue.

Here is relevant code.

Constructor for overall Set object

public class LinkedSet<T> implements Set<T> {

    private Node firstNode;

    public LinkedSet() {
        firstNode = null;
    } // end Constructor

Method that is holding me up

public int getSize() {
    int size = 1;
    Node current = firstNode;

    while ((current.next) != null) {
        size++;
        current = current.next;
    }
    return size;
} // end getSize()

isEmpty() method

public boolean isEmpty() {
    Node next = firstNode.next; //Get error here
    if (next.equals(null)) {
       return true;
    }
    return false;
} // end isEmpty()

Here is private inner class for Node objects

private class Node {
    private T data;
    private Node next; //Get Error here

    private Node(T data, Node next) {
        this.data = data;
        this.next = next;
    } // end Node constructor

    private Node(T data) {
        this(data, null);
    }// end Node constructor
 } // end Node inner Class

And lastly here is the main tester method.

public class SetTester {

    public static void main(String[] args) {
        LinkedSet<String> set = new LinkedSet<String>();
        System.out.println(set.getSize()); //Get error here
    }
}
Harrison Bergman
  • 169
  • 1
  • 12
  • 2
    firstNode = null; at your constructor is throwing that – Khalil M Feb 17 '17 at 13:06
  • 1
    In addition to the other suggestions: in `getSize()` I believe you should initialize `size` to 0 and use `current != null` as your `while` condition. – Ole V.V. Feb 17 '17 at 13:29

4 Answers4

4

Your set is empty if it has no nodes. Therefore your isEmpty() implementation is your problem, since it assumes you always have a firstNode even though you explicitly set it to null in the constructor.

Try this:

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

Edit after the first problem was edited away:

You still access null (which causes the NullPointerException) since you set current to firstNode which in turn has never been set to anything but null.

DuneCat
  • 788
  • 6
  • 17
2
public boolean isEmpty() {
    Node next = firstNode.next; //Get error here
    if (next.equals(null)) {
        return true;
    }
    return false;
} // end isEmpty()

This line gives you NullPointerException, I hope:

Node next = firstNode.next; //Get error here

Because firstNode is probably null and not pointing anywhere so far. It's also best practice to handle NullPointerException. So, what you should do is:

public boolean isEmpty() {
    if (firstNode == null) { return true;}
    return false;
} // end isEmpty()

Also, do not check null as:

next.equals(null)

Always check it as:

null == next or next == null

SSC
  • 2,956
  • 3
  • 27
  • 43
1

You need to check if firstNode is null before you try to access it in the line with the error, since you initialize it with null.

Marius Loewe
  • 236
  • 1
  • 6
0

In

public class LinkedSet<T> implements Set<T> {

    private Node firstNode;

    public LinkedSet() {
        firstNode = null;
    } // end Constructor

firstNode is null and you are not initializing the memory to the node and accessing it afterwards.That's the reason you are getting null pointer exception because you are accessing null. Change it to.

public class LinkedSet<T> implements Set<T> {
private Node firstNode;

public LinkedSet() {
    firstNode = new Node();
} // end Constructor

To check if empty

public boolean isEmpty() {
   return firstNode==null;
} // end isEmpty()

Node Class

private class Node {
    private T data;
    private Node next; //Get Error here
    private Node(T data, Node next) {
        next= new Node();
        this.data = data;
        this.next = next;
    } // end Node constructor

    private Node(T data) {
        this(data, null);
    }// end Node constructor
} // end Node inner Class

Main

public class SetTester {

    public static void main(String[] args) {
        LinkedSet<String> set = new LinkedSet<String>();
        System.out.println(set.isEmpty());
    }
}
Hamza Anis
  • 2,475
  • 1
  • 26
  • 36