Link to home
Start Free TrialLog in
Avatar of jlscot
jlscot

asked on

NullPointerException

I think that smallIndex, index, and temp all have values, so Im not sure why I am getting the error. Can anyone explain to me why this is happening? The error message is:

Exception in thread "main" java.lang.NullPointerException

   public class LinkedList
   {
      public class LinkedListNode
      {
         public int info;
         public LinkedListNode next;
         public LinkedListNode back;
      
         public LinkedListNode()
         {
            info = 0;
            next = null;
            back = null;
         }
         public LinkedListNode(int item)
         {
            info = item;
            next = null;
            back = null;
         }
         public void displayInfo()
         {
            System.out.print(info + " ");
         }
      }
      protected int count;
      protected LinkedListNode first;
      protected LinkedListNode last;
      
      public LinkedList()
      {
         first = null;
         last = null;
         count = 0;
      }
      public void initializeList()
      {
         first = null;
         last = null;
         count = 0;
      }
      public boolean isEmpty()
      {
         return (first == null);
      }
      public int length()
      {
         return count;
      }
      public void print()
      {
         LinkedListNode current = first;
         while (current != null)
         {
            current.displayInfo();
            current = current.next;
         }
      }
      public void insertNode(int insertItem)
      {
         LinkedListNode newNode = new LinkedListNode(insertItem);
         if (isEmpty())
         {
            first = newNode;
            last = newNode;
            count++;
         }
         else
         {
            last.next = newNode;
            newNode.back = last;
         }
         last = newNode;
      }
      public LinkedListNode partition(LinkedList list,
      LinkedListNode first, LinkedListNode last)
      {
         LinkedListNode smallIndex = first;
         LinkedListNode index = smallIndex.next;
         LinkedListNode temp = new LinkedListNode();
         int pivot = first.info;
      
         while (index != last.next)
         {
            if((index.info) < pivot)
            {
               smallIndex = smallIndex.next;
               temp.info = index.info;
               index.info = smallIndex.info;
               smallIndex.info = temp.info;
            }
            index = index.next;
         }
         temp.info = first.info;
         first.info = smallIndex.info;
         smallIndex.info = temp.info;
         System.out.print("The list after QuickSort is: "); 
         list.print();
         System.out.print("\n");
         return smallIndex;
      }
      public void recQuickSort(LinkedList list, LinkedListNode first,
      LinkedListNode last)
      {
         while(first != last)
         {
            LinkedListNode pivotLocation = partition(list, first, last);
            recQuickSort(list, first, pivotLocation.back);
            recQuickSort(list, pivotLocation.next, last);
         }
      }
      public void quickSortLinkedList(LinkedList list)
      {
         recQuickSort(list, list.first, list.last);
      }
   
   }

Open in new window


   import java.util.*;
   
   public class testLinkedListQuickSort
   {
      static Scanner console = new Scanner(System.in);
   
      public static void main(String[] args) 
      {
         LinkedList linkedlist = new LinkedList();
         int num;
         
         System.out.println("Enter numbers to add to linked list:");
         num = console.nextInt();
         while (num != 0)
         {
            linkedlist.insertNode(num);
            num = console.nextInt();
         }
         linkedlist.quickSortLinkedList(linkedlist);
         linkedlist.print();
      }
   }

Open in new window


stack trace:

 ----jGRASP exec: java testLinkedListQuickSort

Enter numbers to add to linked list:
78
1002
654
2
99
0
The list after QuickSort is: 2 78 654 1002 99
The list after QuickSort is: 2 78 99 654 1002
The list after QuickSort is: 2 78 99 654 1002
Exception in thread "main" java.lang.NullPointerException
      at LinkedList.partition(LinkedList.java:85)                              
                                                                         // 85 - if((index.info) < pivot)

      at LinkedList.recQuickSort(LinkedList.java:107)  
                                            // 107 - LinkedListNode pivotLocation =partition(list, first, last);

      at LinkedList.recQuickSort(LinkedList.java:108)
                                       // 108 - recQuickSort(list, first, pivotLocation.back);
 
      at LinkedList.recQuickSort(LinkedList.java:109)
                                                    // 109 - recQuickSort(list, pivotLocation.next, last);

      at LinkedList.quickSortLinkedList(LinkedList.java:114)
                                                  // 114 -recQuickSort(list, list.first, list.last);

      at testLinkedListQuickSort.main(testLinkedListQuickSort.java:19)
                                                    // 19- linkedlist.quickSortLinkedList(linkedlist);


 ----jGRASP wedge2: exit code for process is 1.
 ----jGRASP: operation complete.
Avatar of BottalicoM
BottalicoM
Flag of Italy image

try to change while control from
while (index != last.next) 

Open in new window


to
while (index!=null && last!=null && index != last.next) 

Open in new window


on the method :
public LinkedListNode partition(LinkedList list, LinkedListNode first, LinkedListNode last) 

Open in new window


this solve the null pointer exception but not the infinite loop.

Pay attention to recursive call.

Regards
Avatar of jlscot
jlscot

ASKER

Thanks for you help. I tried what you suggested, but I still got errors. The stack trace is way too long to post.
Avatar of krakatoa
null is being passed somewhere, you can bet.

Can you describe what your code is doing, so that it doesn't need to be figured out from your source? That way, if your abstract is correct, it's easier to look at the code and follow the logic flow in it to look for the hole. Otherwise, for code as lengthy and iterative as yours, it can get tedious.
ASKER CERTIFIED SOLUTION
Avatar of BottalicoM
BottalicoM
Flag of Italy image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial