Reputation: 25
This program is supposed to allow the user to insert integers into a Linked list and keep them always sorted. I am stuck on why I am getting a null pointer exception at this point after I return the value back my other method. It feels like I am going in circles at this point. I have dummy print statements to try and figure out the problem.
Class Node:
public class Node {
Comparable data;
Node next;
Node(Node n, Comparable a) {
this.data = a;
this.next = n;
}
}
Class SortedLinkedList:
public class SortedLinkedList {
Node head = null;
private Comparable SortedLinkedList;
public SortedLinkedList() {
this.head = null;
this.SortedLinkedList = SortedLinkedList ;
}
public Node insert(Node head, Comparable a){
if (head == null || (head.data.compareTo(a)> 0))
{
System.out.println("In insert first if");
head = new Node( head, a);
//head = new Node(head, 22);
System.out.println("Head = " + head.data + " before return");
return head;
}
Node pointer = head;
while (pointer.next != null)
{
if (pointer.next.data.compareTo(a) > 0){
System.out.println("In insert, in while, in if");
break;
}
pointer = pointer.next;
}
return head;
}
public void print(Node head){
System.out.println("In print outside of for" + head);
for (Node pointer = head; pointer != null ; pointer = pointer.next)
{
System.out.println("In print");
System.out.print(pointer.data);
}
}
}
Class TestInteger
public class TestInteger implements Comparable{
// This is the User Interface for manipulating the List
static SortedLinkedList sll = new SortedLinkedList ();
public static void nodeMenu() {
Node head = sll.head;
System.out.println(head);
int option;
while(true){
System.out.println();
System.out.println("**** Integer Node Menu ****");
System.out.println("****************************");
System.out.println("** 1. Insert **");
System.out.println("** 2. Delete **");
System.out.println("** 3. Clear **");
System.out.println("** 4. Smallest **");
System.out.println("** 5. Largest **");
System.out.println("** 6. Return to Main Menu **");
System.out.println("****************************");
Scanner sc = new Scanner(System.in);
try{
option = sc.nextInt();
switch (option){
case 1:{
try{
System.out.println("Type an integer to insert: ");
int x = sc.nextInt();
Integer insertItem = new Integer(x);
sll.insert(head, insertItem);
System.out.println("After insert back in case1 head = " + head.data);
sll.print(head);
}catch(InputMismatchException e){
System.out.println("Enter only integers");
sc.nextLine();
}
nodeMenu();
}
It prints correctly in the actual insert method within class SortedLinkedList but gets a null pointer in class TestInteger. Here is output:
1
Type an integer to insert:
5
In insert first if
Head = 5 before return
Exception in thread "main" java.lang.NullPointerException
at CS_240_HW_2.TestInteger.nodeMenu(TestInteger.java:58)
at CS_240_HW_2.Main.mainMenu(Main.java:52)
at CS_240_HW_2.Main.main(Main.java:30)
Upvotes: 0
Views: 1950
Reputation: 12985
There's a lot to change with your code. For example, you only need one head and you only need the one Node
class for data. The SortedLinkedList
class can be a utility class that only has some methods used to fool with the nodes in a particular way.
So I suggest a change to Node
. This class holds all the data, all but the head itself.
public class Node {
Comparable data;
Node next;
Node(Comparable a) {
this.data = a;
}
}
Then these changes to the inserter class. This class is just some helpful methods that are used to do handy things with your linked list and/or its nodes.
public class SortedLinkedList {
public Node insert(Node head, Comparable a){
Node curr = head;
Node prev = null;
while (curr != null && curr.data.compareTo(a) > 0) {
prev = curr;
curr = curr.next;
}
if (prev = null) {
return new Node(a);
}
prev.next = new Node(a);
prev.next.next = curr;
return head;
}
// print doesn't need changing
}
And for the the test class not too many things to change:
public class TestInteger implements Comparable{
// This is the User Interface for manipulating the List
static SortedLinkedList sll = new SortedLinkedList ();
Node head = null;
public static void nodeMenu() {
// no changes in this part ...
Integer insertItem = new Integer(x);
head = sll.insert(head, insertItem);
}catch(InputMismatchException e){
System.out.println("Enter only integers");
sc.nextLine();
}
}
Be sure to take out the recursive call to nodeMenu()
Upvotes: 0
Reputation: 691715
You initialize a list:
static SortedLinkedList sll = new SortedLinkedList ();
In this constructor, the head of the list is set to null:
this.head = null;
Then you initialize a variable with the head of the list:
Node head = sll.head;
So head
is null.
Then you try to print the value of head.data
:
System.out.println("After insert back in case1 head = " + head.data);
And since head
is null, you get a NullPointerException.
Upvotes: 0
Reputation: 2089
head = sll.insert(head, insertItem);
instead of
sll.insert(head, insertItem);
?
Upvotes: 1