Results 1 to 7 of 7
  1. #1
    Rhez is offline Member
    Join Date
    Oct 2009
    Posts
    17
    Rep Power
    0

    Default Help: Class LinkList, Method checkAges()

    I'm trying to create a method that checks if the ages of each node are sorted in order from smallest to largest (usually they aren't):
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    
    public class LinkList extends EasyApp
    {
          public static void main(String[] args)
          {  new LinkList(); }
          
          ...
          
          Button bCheckAges = addButton("Check Ages",50,300,100,50,this);
          
          ...
          
          public LinkList()
          {  bQuit.setBackground(Color.red);
             setSize(200,500);
             setVisible(true);
          }
          
          public void actions(Object source,String command)
          {
             ...
             else if (source == bCheckAges) { checkAges();}
          }
          
          Node head = null;
          Node tail = null;
    
          class Node
          {  String name="";
             int age=0;
             Node next;
          }
          
          ...
          
          public void showList()
          {  Node temp = head;
             do
             { output( temp.name + " | " + temp.age );
                temp = temp.next;
             } while( temp != null );
          }
          
          public void checkAges()
          {  Node temp = head;
             boolean sorted = false;
             do
             { if(temp.age > temp.next.age)
                {   sorted = false;
                    break;  }
               else if(temp.age <= temp.next.age)
                {   sorted = true;
                    temp = temp.next;   }
             } while( temp != null );
             if(sorted == true) {   output("Ages sorted");  }
             else if(sorted == false)   {   output("Ages not sorted");  }
          }
    }
    As you can see, I took the template/format from the showList() method.

    After filling in the first node and another node (eg first node name is "abc" and age is 1; second node name is "cba" and age is 2), when I click on button "Check Ages" it should output "Ages sorted", but instead it showed nothing but an "Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException"; a null pointer exception... It works and outputs "Ages not sorted" when I make the ages not in order though... something about making the ages in order caused the error...
    Last edited by Rhez; 04-12-2010 at 10:28 AM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default

    What line are you getting the exception on?

  3. #3
    Rhez is offline Member
    Join Date
    Oct 2009
    Posts
    17
    Rep Power
    0

    Default

    Here is the error message when I created a first node with word "abc" and number "1" and another node with word "cba" and number "2" and then clicked on the "Check Ages" button:
    Java Code:
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    	at LinkList.checkAges(LinkList.java:101)
    	at LinkList.actions(LinkList.java:36)
    	at EasyApp.actionPerformed(EasyApp.java:281)
    	at java.awt.Button.processActionEvent(Button.java:392)
    	at java.awt.Button.processEvent(Button.java:360)
    	at java.awt.Component.dispatchEventImpl(Component.java:4667)
    	at java.awt.Component.dispatchEvent(Component.java:4497)
    	at java.awt.EventQueue.dispatchEvent(EventQueue.java:635)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
    	at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
    Last edited by Rhez; 04-13-2010 at 01:34 PM.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default

    Yes, and which line is line 101?

    ETA: I ask, but that will sort of narrow down the possibilities of what is actually null.
    Last edited by Tolls; 04-13-2010 at 02:12 PM.

  5. #5
    Rhez is offline Member
    Join Date
    Oct 2009
    Posts
    17
    Rep Power
    0

    Default

    line 101:
    Java Code:
             { if(temp.age > temp.next.age)

  6. #6
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Your while condition is temp != null, that means the last element you check will have no ancestor, meaning temp.next will be null. A better condition would be temp.next != null, since you check the current and next element, you will still traverse all of the list.

  7. #7
    Rhez is offline Member
    Join Date
    Oct 2009
    Posts
    17
    Rep Power
    0

Similar Threads

  1. Linklist
    By buckey in forum New To Java
    Replies: 3
    Last Post: 03-31-2010, 02:58 AM
  2. Child-Class Calling a Method in a Parent-Class
    By Blah_ in forum New To Java
    Replies: 5
    Last Post: 09-29-2009, 02:48 AM
  3. Sorted LinkList problem
    By koolaqua16 in forum Advanced Java
    Replies: 1
    Last Post: 08-08-2009, 06:49 AM
  4. Replies: 29
    Last Post: 09-25-2008, 07:55 PM
  5. Calling a method on original class from created class
    By kpedersen in forum Advanced Java
    Replies: 4
    Last Post: 08-20-2008, 12:25 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •