Note that there are some explanatory texts on larger screens.

plurals
  1. PONullPointerException with while loop using compareTo
    primarykey
    data
    text
    <p>For our homework, I have to take in <code>Chair</code> objects and add them to the <code>DoublyLinkedList</code> that we made; it has to be sorted by alphabetical order, if style is the same alphabetically, we sort by color</p> <p>When I try to go through the loop, I keep getting a <code>NullPointerException</code>.</p> <pre><code>public void add(Chair element){ if(isEmpty() || first.object.style.compareTo(element.style) &gt; 0 || (first.object.style.compareTo(element.style) == 0 &amp;&amp; first.object.color.compareTo(element.color) &gt;= 0){ addFirst(element); }else if(first.object.style.compareTo(element.style) &lt;= 0){ Node temp = first; Node insert = new Node(); insert.object = element; while(temp.object.style.compareTo(element.style) &lt;= 0) //This is where the nullPointerException occurs if(temp.hasNext()) temp = temp.next; while(temp.object.style.compareTo(element.style) == 0 &amp;&amp; temp.object.color.compareTo(element.color) &lt;= 0) if(temp.hasNext()) temp = temp.next; insert.prev = temp.prev; insert.next = temp; temp.prev.next = insert; temp.prev = insert; } } </code></pre> <p>This is the code where I put the information into the DoublyLinkedList</p> <pre><code>try{ FileReader fr = new FileReader(filename); Scanner sc = new Scanner(fr); String[] temp; while(sc.hasNext()){ temp = sc.nextLine().split(" "); if(temp[0].equals("Bed")){} else if(temp[0].equals("Table")){ // tables.add(new Table(Integer.parseInt(temp[1]), Integer.parseInt(temp[2]), Integer.parseInt(temp[3]), temp[4])); }else if(temp[0].equals("Desk")){} else if(temp[0].equals("Chair")){ chairs.add(new Chair(temp[1], temp[2])); }else if(temp[0].equals("Bookshelves")){} else{ color = temp[0]; } } while(!chairs.isEmpty()) System.out.println(chairs.removeFirst().info()); System.out.println(); //while(!tables.isEmpty()) // System.out.println(tables.removeFirst().info()); }catch(Exception e){e.printStackTrace();} </code></pre> <p>This is the DoublyLinkedList class that I've made: class CDoublyLinkedList{ Node first, last;</p> <pre><code>public CDoublyLinkedList(){ first = new Node(); last = new Node(); first.prev = last.next = null; first.object = last.object = null; first.next = last; last.prev = first; } public boolean isEmpty(){ return first.object == null; } public void addFirst(Chair element){ Node insert = new Node(); insert.object = element; insert.prev = null; insert.next = first; first.prev = insert; first = insert; } public void add(Chair element){ if(isEmpty() || first.object.style.compareTo(element.style) &gt; 0 || (first.object.style.compareTo(element.style) == 0 &amp;&amp; first.object.color.compareTo(element.color) &gt;= 0){ addFirst(element); }else if(first.object.style.compareTo(element.style) &lt;= 0){ Node temp = first; Node insert = new Node(); insert.object = element; while(first.object.style.compareTo(element.style) &lt;= 0) if(temp.hasNext()) temp = temp.next; while(first.object.style.compareTo(element.style) == 0 &amp;&amp; first.object.color.compareTo(element.color) &lt;= 0) if(temp.hasNext()) temp = temp.next; insert.prev = temp.prev; insert.next = temp; temp.prev.next = insert; temp.prev = insert; } } public Chair removeFirst(){ Chair tobedeleted = first.object; Node temp = first.next; first = temp; first.prev = null; return tobedeleted; } private class Node{ Node next, prev; Chair object; public boolean hasNext(){ return next != null; } } </code></pre> <p>}</p> <p>The Chair class:</p> <pre><code>class Chair extends Furniture{ public String style, color; public Chair(String s, String c){ style = s; color = c; } public String toString(){ return color; } public String getType(){ return "Chair"; } public String info(){ return (color+", "+style); } } </code></pre> <p>Can someone please explain to me why I keep getting this error? Thank you!</p> <p>EDIT: </p> <pre><code>while(temp.object.style.compareTo(element.style) &lt;= 0) //This is where the nullPointerException occurs chairs.add(new Chair(temp[1], temp[2])); java.lang.NullPointerException at CDoublyLinkedList.add(Furnish2SS.java:119) at Furnish2SS.main(Furnish2SS.java:23) java.lang.NullPointerException at CDoublyLinkedList.add(Furnish2SS.java:119) at Furnish2SS.main(Furnish2SS.java:23) </code></pre> <p>EDIT2: SOLVED!</p> <p>I changed my while loop to:</p> <pre><code>while(temp.object != null &amp;&amp; element != null &amp;&amp; (temp.object.compareTo(element) == 0 || temp.object.compareTo(element) == -1)) </code></pre> <p>The reason I got the error was because I wasn't checking for <code>null</code> every iteration.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload