1 Replies - 347 Views - Last Post: 17 June 2010 - 06:03 PM Rate Topic: -----

#1 jadawgis732  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 04-April 09

Two Linked Object Lists (null pointer exception)

Posted 17 June 2010 - 05:26 PM

Hi, I'm having a problem with a null pointer exceptiont. Here's an image that should explain the project. Posted Image

We have two linked object lists type Person and type Friend.

So we have a friends package consisting of three files (Friend.java Person.java PersonList.java) and a driver.
The null pointer exception is with line 8 in Friends.java called by line 11 in Person.jave, the Person constructor, in the firstFriend instance variable. This Friend object uses a dummy header.
package friends;

public class PersonList {
         Person firstPerson;
        
         public PersonList( ) {
             firstPerson = new Person("", null);

        }        public Person lookup(String name){
                Person ptr = new Person("", null);
                for (ptr = firstPerson; ptr.nextPerson != null; ptr = ptr.nextPerson) {
                        if (ptr.name == null) {
                                ptr.name = name;
                                return ptr;
                        }
                        else if (ptr.name == name) {
                                return ptr;
                        }
                }
                ptr.name = name;
                return ptr;
        }
         void addFriend(Person who, String friendName){
                if (firstPerson == null) {
                	System.out.println("returned");
                	return;
                }
                        
                Person ptr = firstPerson;
                while (ptr != who) {
                	System.out.println("looped");
                    ptr = ptr.nextPerson;
                }
                if (ptr == who)
                {
                	System.out.println("found");
                    if (ptr.firstFriend.who == null)
                            ptr.firstFriend.who.name = friendName;
                    Friend ptr2 = ptr.firstFriend;
                    while (ptr2.nextFriend != null) {
                            ptr2 = ptr2.nextFriend;
                    }
                    ptr2.nextFriend.who.name = friendName;
                }
        }
}
 


Person.java:
package friends;

public class Person {
	String name;
	Friend firstFriend;
	Person nextPerson;
	
	public Person(String name, Person nextPerson) {
		this.name = name;
		this.nextPerson = nextPerson;
		this.firstFriend = new Friend(nextPerson, null);
	}
	
	public String friendString(){
		String friends = "Friends: ";
		Friend ptr = this.firstFriend;
		friends += ptr.who.name;
		if (this.firstFriend.nextFriend != null) {
			ptr = this.firstFriend.nextFriend;
			friends += ptr.who.name;
			while (ptr.nextFriend != null) {
				friends += ptr.nextFriend.who.name;
			}
		}
		return friends;
		
	}
	
	public void addFriend(Person friend){
		if (this.firstFriend == null) {
			this.firstFriend = new Friend(null, null);
			this.firstFriend.who = friend;
			return;
		}
		Friend ptr = this.firstFriend;
		while (ptr.nextFriend != null) {
			ptr = ptr.nextFriend;
		}
		if (ptr.who == null)
			ptr.who = friend;
		
	}
	
	public void removeFriend(Person friend){
		if (this.firstFriend.who == friend){
			if (this.firstFriend.nextFriend != null) {
				this.firstFriend = this.firstFriend.nextFriend;
			}
			else {
				this.firstFriend = null;
			}
		}
		Friend ptr = this.firstFriend;
		while (ptr.nextFriend.who != friend) {
			if (ptr.nextFriend != null) 
				ptr = ptr.nextFriend;
		}
		if (ptr.nextFriend.who != null) {
			if (ptr.nextFriend.nextFriend.who != null) {
				ptr.nextFriend = ptr.nextFriend.nextFriend;
			}
			else {
				ptr.nextFriend = null;
			}
		}	
	}
}




Friend.java
package friends;

public class Friend {
	Friend nextFriend;
	Person who;
	
    Friend(Person who, Friend nextFriend) {
		Friend ptr = who.firstFriend;
		while (ptr.nextFriend != null) {
			ptr = nextFriend;
		}
		ptr.who = who;
		ptr.nextFriend = nextFriend;

   		
	}
	
	void addFriend(Person newWho){
		if (this.who.firstFriend == null) 
			this.who.firstFriend.who = newWho;
	}
		
	Friend findBefore(Person who){
		return null;
		
		
	}
	
	void removeFriend(Person who){
// fill in here
	
	}

}
}

Here is the driver which we cannot modify:
package driver;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import friends.PersonList;
import friends.Person;

public class DriveFriends {
	public static void main(String [] args)
		throws IOException {
		
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		char cmd=' ';
		PersonList people = new PersonList();

		
		while (cmd != 'q'){
			System.out.println("type:  for:");
			System.out.println(" q     quit");
			System.out.println(" a     add friend");
			System.out.println(" d     delete friend");
			System.out.println(" p     print friend list");
			
			Person who;
			String line = br.readLine();
			if (line.length() == 0){
				cmd = ' ';
			} else {
				cmd = line.charAt(0);
			}
			
			switch (cmd){
			case 'a': who = readPerson("person", people, br);
					  who.addFriend(readPerson("friend", people, br));
					  break;
			case 'd': who =  readPerson("person", people, br);
			  	      who.removeFriend(readPerson("friend", people, br));
			  	      break;
			case 'p': who = readPerson("person", people, br);
			 		  System.out.println(who.friendString());
			 		  break;
			case 'q': System.out.println("Bye.");
					  break;
			default:  System.out.println("Bad option.");
			
			}
		}
	}
		
		static Person readPerson(String kind, PersonList people, BufferedReader br)
			throws IOException {
			System.out.println("type "+ kind + "'s name: ");
			Person who = people.lookup(br.readLine());
			return who;
		}
}



Any ideas?

Edited by macosxnerd101: Fixed code tags. :)

This post has been edited by macosxnerd101: 17 June 2010 - 05:57 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Two Linked Object Lists (null pointer exception)

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Two Linked Object Lists (null pointer exception)

Posted 17 June 2010 - 06:03 PM

Sounds more than famniliar to me
There is already a thread treating this problem
Please avoid double posting
This thread closed
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1