Results 1 to 4 of 4
  1. #1
    KillingKiller is offline Member
    Join Date
    Mar 2009
    Posts
    2
    Rep Power
    0

    Default Need Help with determining the problem in my Code

    I'm trying to implement a linked-list ADT for my lab course. So far, I can't figure out what am I doing wrong. After successful compilation I try to add some more objects and nodes, but after inserting the second element (the first one is created just fine) into the second object and trying to link those two I get an exception message shown below:

    --------------------------------------------------------
    Empty List
    Command: +1
    Insert 1
    1


    Command: +2
    Insert 2
    Exception in thread "main" java.lang.NullPointerException
    at SList.insert(SList.java:54)
    at TestSList.main(TestSList.java:78)
    --------------------------------------------------------


    I'll now provide all the necessary codes for the above, but as you can see, my showstructure code is wrong too as, it doesn't show the whole list at the same time, it just shows 1 element at a time, so I'd appreciate it, if anyone could help me to figure out my mistakes here. Thanks in advance.

    ------------------------------------------------------
    public void insert(Object newElement){

    if (!isFull() && newElement!=null){
    if (head==null && cursor ==null)
    {
    head = new SListNode(newElement, null);
    cursor=head;
    }
    else{
    if (cursor==head){
    cursor = cursor.getNext();
    cursor = new SListNode(newElement, cursor.getNext());
    }
    }
    }
    }
    ------------------------------------------------------
    public void showStructure(){
    if (isEmpty())
    System.out.print("Empty List");

    else{
    SListNode b;
    if (head==cursor)
    System.out.println(cursor.getElement());

    else{
    for (b=head; b!=null; b=b.getNext()){
    System.out.print(b.getElement());
    }
    }
    }
    }
    --------------------------------------------------------------

    I'll be waiting for your replies.

  2. #2
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    if you call getNext and there is no next list item, you're getting a null. thus, null pointer exception.

  3. #3
    paul pasciak is offline Senior Member
    Join Date
    Jul 2008
    Posts
    125
    Rep Power
    0

    Default A linked-list is never full.

    A suspicious part of your code (the first line below)
    tests if the list is not full:

    if (!isFull() && newElement!=null)

    A linked-list can never be full (theoretically), so
    maybe that test should be renamed, or just thrown
    out.

    Java Code:
    public void insert(Object newElement){
    
      if (!isFull() && newElement!=null){
        if (head==null && cursor ==null){
          head = new SListNode(newElement, null);
          cursor=head;
        }
        else{
          if (cursor==head){
            cursor = cursor.getNext();
            cursor = new SListNode(newElement, cursor.getNext());
          }
        }
      }
    }

  4. #4
    KillingKiller is offline Member
    Join Date
    Mar 2009
    Posts
    2
    Rep Power
    0

    Default

    I know that it's continuous, but it was asked to do it this way. The isFull operation always returns false anyway.

    Btw, thanks for the help, I figured out the problem, it really was the thing about which emceenugget told

Similar Threads

  1. wat is the problem in the code ?
    By Shyam Singh in forum New To Java
    Replies: 5
    Last Post: 08-14-2008, 10:34 PM
  2. Determining the readiness of another program
    By jmHoekst in forum New To Java
    Replies: 10
    Last Post: 06-19-2008, 08:56 AM
  3. Problem with code
    By jvasilj1 in forum New To Java
    Replies: 5
    Last Post: 02-02-2008, 08:34 AM
  4. Determining Midi Length
    By Usagi in forum New To Java
    Replies: 0
    Last Post: 12-08-2007, 11:45 PM
  5. Problem with zero in my code
    By fernando in forum New To Java
    Replies: 1
    Last Post: 08-05-2007, 06:39 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
  •