0

I am currently studying up on linked lists in Java. I have a sample program that works as expected for certain input, and not at all for other input.

//Constructor in class List of People
ListOfPeople() {
    Person listHead = new Person("LIST HEAD");
    personList = listHead;
    lastPerson = listHead;
    numberPeople = 0;
}
//Adds person to the beginning of the list
public void addFirst(Person newP) {
    newP.next = personList.next;
    personList.next = newP;
    numberPeople++;
}
//Adds person to the end of the list
public void addLast(Person lastP) {
    lastPerson.next = lastP;
    lastPerson = lastP;
    numberPeople++;
}

For the Person class, I have the following code:

//From the Person class
String name;
Person next;

Person(String n) {
    name = n;
    next = null;
}

Suppose I add two different people to the beginning of the list:

Person mulan = new Person("Mulan");
myFriends.addFirst(mulan);
Person mushu = new Person("Mushu");
myFriends.addFirst(mushu);

Then, the code works without problems. And I get the output: "Mushu, Mulan". HOWEVER, if I add one person at the beginning of the list, and another at the end, I get a NullPointerException. If I try to invoke the addLast(String name) method on both Person objects, there seems to be no problem.

Any tips are highly appreciated!

3
  • I'm missing the fields of your ListOfPeople class and the exact point where the NPE is thrown. NPEs at this level can be easily spotted runnning a debugger, you should try to use it. That said, think about what should happen to lastPerson if you add a Person with addFirst to an empty list... Commented Feb 9, 2014 at 9:09
  • ... And I get the output: "Mushu, Mulan". please show how do you fetch this names Commented Feb 9, 2014 at 9:16
  • I have written a method in the Person class: "public void write() { System.out.println(name); }. As well as another method in the ListOfPeople class: "public void writeAll() { Person p = personlist.next; for(int i = numberPeople; i>0; i--) {p.write(); p = p.next; }" Commented Feb 9, 2014 at 9:22

1 Answer 1

1

Try something like:

 // I left out getters and setters for brevity.
 class PersonNode {
      Person current;
      PersonNode next;
      PersonNode previous;
 }

 class PersonList {
       PersonNode head; 
       PersonNode tail;

      public PersonList(){ 
          head.previous = null;
          tail.next = null;
      }
      void addFront(Person p){ 
          if (head.person == null) {
                head.person = p; 
          }
          else {
               PersonNode temp = head; head= new PersonNode(p);               
               temp.previous = head; head.next = temp;
               }
          }
     void addBack(Person p) {
          if (tail.person == null) {
                tail.person = p; 
          }
          else {
              PersonNode temp = tail;
              tail= new PersonNode(p);               
              temp.next = tail;
              tail.previous = temp;
      }

     int count() {
          int c = 0;
          for(PersonNode n = head; head.next != null; n = head.next){
              if (n.Person !=null){
                  ++c;
              }
          }
          return c;
     }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.