1

I am trying to implement a min priority queue with a sorted arraylist. However I am facing trouble for the insertion method. I am getting null pointer exception error. I don't know how to fix it. I have a getKey() and compare method in my AbstractPriorityQueue class. Any help will be really appreciated. I've been stuck with this problem for a long time. My class:

import java.util.ArrayList;

public class SortedListPQ<K,V> extends AbstractPriorityQueue<K,V>  {
    
    private ArrayList<Entry<K,V>> list;
    private Comparator<K> comp;
    
      /**
       *  Constructor
       */
      public SortedListPQ() {
        list = new ArrayList<Entry<K,V>>();
      }
      
      /**
       * to check if the queue is empty
       */
      public boolean isEmpty() {
            return list.size() == 0;
          }
      
      /**
       * inserting an element in the sorted list (right position)
       */
      public Entry<K,V> insert(K key, V value) {
          checkKey(key); 
          Entry<K,V> newest = new PQEntry<>(key, value);
          
          //if the list is empty, then add the entry to the list
          if(list.isEmpty()) {
              list.add(0,newest);         
              }
          
          else {
              
          
          //if the new key is the smallest, add it to the beginning
          Entry<K,V>start=list.get(0); 
          if (comp.compare(start.getKey(),newest.getKey())>0) {
              list.add(0,newest);
          }
          
          //if the key is largest, we'll add it to the end
          Entry<K,V>last=list.get(list.size()-1);
          if(comp.compare(last.getKey(), newest.getKey())<0) {
              list.add(list.size()-1,newest);
          }
          
          //to add the key between two keys
          int p=1;
          while (p!=list.size()-1) {
              Entry<K,V> before = list.get(p);
              Entry<K,V> after = list.get(p+1);
              
              if ((comp.compare(before.getKey(),newest.getKey())<0) && 
 (comp.compare(after.getKey(),newest.getKey())>0)) {
                  list.add(p+1,newest);
              }
              p++;
          }
          }
          return newest;
          
      }
      public Entry<K,V> min() {
            if (list.size() == 0)
              return null;
            else
              return list.get(0);   // the first element is smallest
          }
      
      public Entry<K,V> removeMin() {
            if (list.size() == 0)
              return null;
            else                                  // shrink the size
              return list.remove(0);  // and return the smallest element
          }

}

I also have this compare method in my AbstractPriorityQueue class

protected int compare(Entry<K,V> a, Entry<K,V> b) {
        return comp.compare(a.getKey( ), b.getKey( ));
    }
red_rum
  • 119
  • 2
  • 8
  • Can you explain exactly what your problem is? "unlike arrays, I can not access the indexes directly" is not clear. – tgdavies Nov 07 '20 at 00:59
  • I have edited my code and the error that I'm getting. – red_rum Nov 07 '20 at 01:38
  • You need to show the entire stack trace of the NullPointerException and indicate which line number in your program it is happening at. – tgdavies Nov 07 '20 at 01:40

0 Answers0