How do I get this portion of my List Iterator code to work?
This is a portion of the code in the AddressBookApp that I need to work. Currently it stops at Person not found and if it makes it to the else it gives me this
Exception in thread "main" java.lang.NullPointerException
at AddressBookApp.main(AddressBookApp.java:36)
iter.reset();
Person findPerson = iter.findLastname("Duck");
if (findPerson == null)
System.out.println("Person not found.");
else
findPerson.displayEntry();
findPerson = iter.findLastname("Duck");
if (findPerson == null)
{
System.out.println("Person not found.");
System.out.println();
}
else
findPerson.displayEntry();
This is my list Iterator code:
import java.util.LinkedList;
import java.util.ListIterator;
class listIterator {
int pointer = 0;
AddressBook list;
public listIterator() {
}
public listIterator(AddressBook list) {
this.list = list;
}
public void reset() {
pointer = 0;
}
public boolean atEnd() {
return pointer == list.getSize() - 1;
}
public void nextLink() {
pointer++;
}
public Person getCurrent() {
if (list.getSize() > pointer)
return list.get(pointer);
return null;
}
public void insertFront(Person entry) {
list.setFirst(entry);
}
public Person findLastname(String lname) {
for (int i = pointer; i < list.getSize(); i++) {
if (list.get(i).returnName().lastName().equals(""))
{
return list.get(pointer);
}
}
return null;
}
public Person deleteCurrent() {
Person p = list.get(pointer);
list.deletePerson(p);
return p;
}
I basically need to get findLastname on my listIterator code to work.
Related code:
class MailInfo
{
private String Address;
private String City;
private String State;
private String Zipcode;
public MailInfo(String add, String cty, String st, String
zip)
{
Address = add;
City = cty;
State = st;
Zipcode = zip;
}
public void setAddress(String add, String cty, String st, String
zip)
{
Address = add;
City = cty;
State = st;
Zipcode = zip;
}
public String returnAddress()
{ return Address; }
public String returnCity()
{ return City; }
public String returnState()
{ return State; }
public String returnZipcode()
{ return Zipcode; }
}
class Date
{
private int month;
private int day;
private int year;
public Date(int mon, int dy, int yr)
{
month = mon;
day = dy;
year = yr;
}
public void setDate(int mon, int dy, int yr)
{
month = mon;
day = dy;
year = yr;
}
public String returnDate()
{
return Integer.toString(month) + "/" + Integer.toString(day) + "/"
+ Integer.toString(year);
}
public int returnMonth()
{ return month; }
public int returnDay()
{ return day; }
public int returnYear()
{ return year; }
}
class Person
{
private Name name;
private MailInfo mailInfo;
private Date dateOfBirth;
private String familyFriendAssociate;
public Person next;
public Person(Person entry)
{
name = entry.returnName();
mailInfo = entry.returnMailInfo();
dateOfBirth = entry.returnDateOfBirth();
familyFriendAssociate = entry.returnFamilyFriendAssociate();
}
public Person(Name theName, MailInfo theMailInfo, Date dob, String
fFA)
{
name = theName;
mailInfo = theMailInfo;
dateOfBirth = dob;
familyFriendAssociate = fFA;
}
public void setPerson(Name theName, MailInfo theMailInfo, Date dob,
String fFA)
{
name = theName;
mailInfo = theMailInfo;
dateOfBirth = dob;
familyFriendAssociate=fFA;
}
public Name returnName()
{ return name; }
public MailInfo returnMailInfo()
{ return mailInfo; }
public Date returnDateOfBirth()
{ return dateOfBirth; }
public String returnFamilyFriendAssociate()
{ return familyFriendAssociate; }
public void displayEntry()
{
System.out.println(name.returnName());
System.out.println(mailInfo.returnAddress());
System.out.print(mailInfo.returnCity());
System.out.print(", ");
System.out.print(mailInfo.returnState());
System.out.print(" ");
System.out.print(mailInfo.returnZipcode());
System.out.println();
System.out.println(dateOfBirth.returnDate());
System.out.println();
}
}
class Name
{
private String Lastname;
private String Firstname;
public Name(String fname, String lname)
{
Lastname = lname;
Firstname = fname;
}
public void setName(String fname, String lname)
{
Lastname = lname;
Firstname = fname;
}
public String returnName()
{
return Firstname + " " + Lastname;
}
public String lastName()
{
return Lastname;
}
public String firstName()
{
return Firstname;
}
}
import java.util.LinkedList;
import java.util.ListIterator;
class AddressBook {
private LinkedList<Person> addressbook;
private listIterator iter = new listIterator(this);
public AddressBook() {
addressbook = new LinkedList<Person>();
}
public boolean isEmpty() {
return addressbook.isEmpty();
}
public Person getFirst() {
return addressbook.getFirst();
}
public void setFirst(Person f) {
addressbook.add(f);
}
public listIterator getIterator() {
return iter;
}
public int getSize() {
return addressbook.size();
}
public Person get(int index) {
return addressbook.get(index);
}
public boolean deletePerson(Person p) {
return addressbook.remove(p);
}
}
In listIterator.java line 30 .equals("") is given hence null is always returned
public Person findLastname(String lname) {
for (int i = pointer; i < list.getSize(); i++) {
if (list.get(i).returnName().lastName().equals(""))
{
return list.get(pointer);
}
}
return null;
}
should be rewritten as
public Person findLastname(String lname) {
for (int i = pointer; i < list.getSize(); i++) {
if
(list.get(i).returnName().lastName().equals(lname))
{
return list.get(pointer);
}
}
return null;
}
public Person
findLastname(String lname) {
for (int i = 0; i < list.getSize(); i++) {
if (list.get(i).returnName().lastName().equals(lname))
{
return list.get(i);
}
}
return null;
}
Get Answers For Free
Most questions answered within 1 hours.