2021年3月12日星期五

Doubly Linked List giving error upon setting previous

I am trying to insert values in order into a doubly linked list and I started getting a null pointer exception on this line of code. rover.setPrev(temp);

So, I figured it was because because it wasn't initialized and I added the if statements below to check if it was null and then initialized it within the while loop as rover went through the list. Still getting the same error on the setPrev, I removed it at one point in troubleshooting and then I started getting an error on the if (rover.getPrev() == null) statement so I made sure that prev was initialized to null in the Record Class. Still any reference through .setPrev or .getPrev is giving me a null pointer exception at that point. I am sure I am an idiot but I have been running my head into a wall and can't see anything wrong or understand why next is seemingly working as usual and prev is broken.

Sorting through linked list.

    public void insert(Record newVal)       {                    if(front == null) {              front = newVal;              //If list is empty then newVal is inserted into front          }else {              Record rover = front;              while(rover != null && (rover.getKey().compareTo(newVal.getKey())) < 0)               {                  System.out.println(rover.getKey());                  System.out.println(newVal.getKey());                  System.out.println(rover.getKey().compareTo(newVal.getKey()));                  //Updates previous as rover moves through the linked list.                  Record temp = rover;                  rover = rover.getNext();                  rover.setPrev(temp);              }              if(rover.getPrev() == null)               {                  newVal.setPrev(rover);                  newVal.setNext(rover.getNext());                  rover.setNext(newVal);                                    //This only happens when rover is still at the front of the list                  //after the while loop has concluded so it has no prev.              }else {                  newVal.setNext(rover);                  newVal.setPrev(rover.getPrev());                  rover.getPrev().setNext(newVal);                  rover.setPrev(newVal);              }                            front = rover;          }      }  

The Record Class

public class IndexRecord {            private IndexRecord next;      private IndexRecord prev;      private String key;      private int where;              IndexRecord()      {          key = "";          where = 0;          next = null;          prev = null;      }            IndexRecord(String data, int x)      {          key = data;          where = x;          next = null;          prev = null;      }            public String getKey()       {          return key;      }            public int getWhere()       {          return where;      }            public IndexRecord getNext()       {          return next;      }            public IndexRecord getPrev()      {          return prev;      }            public void setKey(String s)       {          key = s;      }            public void setWhere(Integer i)      {          where = i;      }            public void setNext(IndexRecord i)       {          next = i;      }            public void setPrev(IndexRecord j)       {          prev = j;      }            //Basics Setters and getters.              }          
https://stackoverflow.com/questions/66610030/doubly-linked-list-giving-error-upon-setting-previous March 13, 2021 at 12:06PM

没有评论:

发表评论