Question

How do I get this portion of my List Iterator code to work? This is a...

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);
}
}

Homework Answers

Answer #1

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;

}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
/* Write a function that looks for a particular person in their respective linked list The...
/* Write a function that looks for a particular person in their respective linked list The only place you need to write code is the "find" method in the linked list class */ #include <bits/stdc++.h> using namespace std; string ltrim(const string &); string rtrim(const string &); #define BUFFLEN 10 /* Each "person" is defined by their name, zipcode, and their pet's name. Persons are hashed by their zipcode. */ //---------------------------------------------------------------------------- /* function declarations ------------------------*/ int computeKey(int); void add_to_buffer(string,int,string); void find_in_buffer(int);...
How do I make this: public class Country {     private String name;     private double area;     private...
How do I make this: public class Country {     private String name;     private double area;     private int population;     public Country(String name, double area, int population) {         this.name = name;         this.area = area;         this.population = population;     }     public double getPopulationDensity() {         return population / area;     }     public String getName() {         return name;     }     public void setName(String name) {         this.name = name;     }     public double getArea() {         return area;     }     public void setArea(double area) {         this.area = area;     }     public int getPopulation()...
Which method is correct to access the value of count? public class Person { private String...
Which method is correct to access the value of count? public class Person { private String name; private int age; private static int count = 0; } A. private int getCount() {return (static)count;} B. public static int getCount() {return count;} C. public int getCount() {return static count;} D. private static int getCount() {return count;} How can you print the value of count? public class Person { private String name; private int age; private static int count=0; public Person(String a, int...
Complete the missing code for the constructors as indicated in the comments in all three header...
Complete the missing code for the constructors as indicated in the comments in all three header files. C++ mainDriver.cpp #include <string> #include "Address.h" #include "Date.h" #include "Person.h" using namespace std; int main() {    Person p1;    Person p2("Smith", "Bobby", "[email protected]", 111, "Main St", "Clemson",            "SC", 29630, 1, 31, 1998);    cout << endl << endl;    p1.printInfo();    p2.printInfo();    return 0; } Person.h #ifndef PERSON_H #define PERSON_H #include <iostream> #include <string> using namespace std; class...
please fix code to delete by studentname import java.util.Scanner; public class COurseCom666 {     private String...
please fix code to delete by studentname import java.util.Scanner; public class COurseCom666 {     private String courseName;     private String[] students = new String[1];     private int numberOfStudents;     public COurseCom666(String courseName) {         this.courseName = courseName;     }     public String[] getStudents() {         return students;     }     public int getNumberOfStudents() {         return numberOfStudents;     }     public void addStudent(String student) {         if (numberOfStudents == students.length) {             String[] a = new String[students.length + 1];            ...
Modify the Employee9C superclass so that is an abstract superclass with a constructor to set its...
Modify the Employee9C superclass so that is an abstract superclass with a constructor to set its variables (firstName and lastName). It should contain an abstract method called payPrint. Below is the source code for the Employee9C superclass: public class Employee9C {    //declaring instance variables private String firstName; private String lastName; //declaring & initializing static int variable to keep running total of the number of paychecks calculated static int counter = 0;    //constructor to set instance variables public Employee9C(String...
Here is my java code, I keep getting this error and I do not know how...
Here is my java code, I keep getting this error and I do not know how to fix it: PigLatin.java:3: error: class Main is public, should be declared in a file named Main.java public class Main { ^ import java.io.*; public class Main { private static BufferedReader buf = new BufferedReader( new InputStreamReader(System.in)); public static void main(String[] args) throws IOException { String english = getString(); String translated = translate(english); System.out.println(translated); } private static String translate(String s) { String latin =...
This assignment is an individual assignment. For Questions 1-3: consider the following code: public class A...
This assignment is an individual assignment. For Questions 1-3: consider the following code: public class A { private int number; protected String name; public double price; public A() { System.out.println(“A() called”); } private void foo1() { System.out.println(“A version of foo1() called”); } protected int foo2() { Sysem.out.println(“A version of foo2() called); return number; } public String foo3() { System.out.println(“A version of foo3() called”); Return “Hi”; } }//end class A public class B extends A { private char service; public B()...
Please explain code 1 and code 2 for each lines code 1 public class MyQueue {...
Please explain code 1 and code 2 for each lines code 1 public class MyQueue {    public static final int DEFAULT_SIZE = 10;    private Object data[];    private int index; code 2 package test; import java.util.*; /* Class Node */ class Node { protected Object data; protected Node link; /* Constructor */ public Node() { link = null; data = 0; } /* Constructor */ public Node(Object d,Node n) { data = d; link = n; } /*...
Hello. I have an assignment that is completed minus one thing, I can't get the resize...
Hello. I have an assignment that is completed minus one thing, I can't get the resize method in Circle class to actually resize the circle in my TestCircle claass. Can someone look and fix it? I would appreciate it! If you dont mind leaving acomment either in the answer or just // in the code on what I'm doing wrong to cause it to not work. That's all I've got. Just a simple revision and edit to make the resize...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT