Results 1 to 2 of 2
  1. #1
    Eric is offline Senior Member
    Join Date
    Jun 2007
    Posts
    111
    Rep Power
    0

    Default Problem with LinkedList

    Hi.. I have a problem 'bout single linkedlist..

    In my list class (PhotoManager), I create a method to storing the data in ascending order.

    When i compile, it gives me no error.. but when i run it, it gives me null pointer exception..


    Java Code:
    // 1 indicates order by increasing date/time order
    else if(photoOrder == 1) {
    if(current == null) {
    newNode = new PhotoNode(newPhoto);
    newNode.next = head; // di sini
    head = newNode; // di sini
    }
    else {
    while(current != null) {
    if(current.photo.getDatePhotoTaken().compareTo(newNode.photo.getDatePhotoTaken()) <= 0) {
    previous = current;
    current = current.next;
    }
    else {
    break;
    }
    }
    
    if(previous == null) {
    newNode.next = head; // di sini
    head = newNode; // di sini
    }
    
    else {
    previous.next = newNode;
    newNode.next = current;
    }
    }
    }
    i'm gonna use that method in my main class, that is make use of the current list (listed in random order) to create a new list (in ascending order)

    Java Code:
    PhotoNode oldPhotoListCurrent, newPhotoListCurrent;
    PhotoManager newPhotoList = new PhotoManager();
    
    oldPhotoListCurrent = photoManager.head;
    newPhotoList.head = null;
    
    while(oldPhotoListCurrent != null) {
    newPhotoList.addPhoto(oldPhotoListCurrent.photo, 1);
    oldPhotoListCurrent = oldPhotoListCurrent.next;
    }
    Can anyone help me to solve this problem??
    Thanks
    Eric

  2. #2
    Daniel is offline Member
    Join Date
    Jun 2007
    Posts
    91
    Rep Power
    0

    Default

    I confess I only took a quick look at this, but I thought I'd point something out in case it helps.

    In addPhoto method
    Java Code:
    PhotoNode current = head, previous = null; // di sini
    ....
    if(previous == null) {
    newNode.next = head;
    head = newNode;
    }
    As far as I can tell, the variable previous will always be null when you hit this if-condition as it doesn't seem that it's ever set otherwise after you declare it as null.

    So, I'm not sure this is what you had intended. Just thought I'd point it out.

    Greetings
    Daniel:o

Similar Threads

  1. Making a stack from a LinkedList
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-16-2008, 11:28 PM
  2. Problem with LinkedList Java
    By paul in forum Advanced Java
    Replies: 2
    Last Post: 08-07-2007, 05:57 AM
  3. Please help me with LinkedList in Java
    By lenny in forum New To Java
    Replies: 2
    Last Post: 07-31-2007, 03:24 PM
  4. how to use LinkedList
    By fred in forum Advanced Java
    Replies: 1
    Last Post: 07-24-2007, 02:52 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
  •