我必须完成部分编写的应用程序,其中大部分已经完成,但是我遇到了java.lang.NullPointerException,而且不确定为什么。
某些代码是由教授重写的,但错误似乎出自他写的部分,我不确定该错误是我添加的代码的结果还是他的错误。< / p>
错误来自searchFriendByName方法中的第一个if语句 一旦到达那一行,我的程序就会崩溃
package model;
/**
* Class that models an Agenda
* @author Pedro Guillermo Feijóo-García
*/
public class Agenda
{
/**
* Number of Friends
*/
private int numberOfFriends;
/*
* These are the relationships with the Friend class
*/
/**
* First friend of the Agenda
*/
private Friend friend1;
/**
* Second friend of the Agenda
*/
//COMPLETE Declare this attribute. Name it friend2.
private Friend friend2;
/**
* Third friend of the Agenda
*/
//COMPLETE Declare this attribute. Name it friend3.
private Friend friend3;
/**
* Constructor method that creates an instance of an Agenda
*/
public Agenda()
{
numberOfFriends = 0;
friend1 = null;
//COMPLETE Assign friend2 as we did for friend1;
//COMPLETE Assign friend3 as we did for friend1;
friend2 = null;
friend3 = null;
}
/*
* These methods are called "Getters"
* They are in charge of retrieving particular values of the object
*/
/**
* Method that returns the number of friends in the Agenda
* @return the number of friends
*/
public int getNumberOfFriends()
{
return numberOfFriends;
}
/**
* Method that returns the first Friend of the Agenda
* @return the first Friend
*/
public Friend getFriend1()
{
return friend1;
}
/**
* Method that returns the second Friend of the Agenda
* @return the second Friend
*/
//COMPLETE Declare and complete the code of this method
public Friend getFriend2() {
return friend2;
}
/**
* Method that returns the third Friend of the Agenda
* @return the third Friend
*/
//COMPLETE Declare and complete the code of this method
public Friend getFriend3(){
return friend3;
}
/*
* These methods correspond to the Functional Requirements
* They directly address the actions/operations/services the user wants to be able to do with the Software
*/
/**
* Method that adds a new Friend to the Agenda
* @param pFriend Friend to be added to the Agenda
* @return true if the Friend is added to the Agenda. False otherwise.
*/
public boolean addNewFriend(Friend pFriend)
{
boolean added = false;
/*
* COMPLETE
* [1] Check if there is any existing Friend with the same name of pFriend.
* Hint: Call and use the method searchFriendByName(...);
* [2] If there is no existing Friend with the same name as pFriend, add pFriend to the first empty slot.
* Hint: From friend1 to friend3, check each relation to find which one of those is the first null relation. When found, make that relation equal to pFriend.
* [3] Remember to increase the numberOfFriends when adding a new Friend to the Agenda.
*/
if(searchFriendByName(pFriend.getName()) == null) {
added = true;
}
return added;
}
/**
* Method that searches for a Friend by an input name
* @param pName name of the Friend looked for
* @return the Friend corresponding to the input name. Null if he/she does not exist.
*/
public Friend searchFriendByName(String pName)
{
Friend myFriend = null;
if(friend1.getName().equals(pName))
{
myFriend = friend1;
}else if(friend2.getName().equals(pName)) {
myFriend = friend2;
}else if(friend3.getName().equals(pName)){
myFriend = friend3;
}
//COMPLETE the code for friend2
//COMPLETE the code for friend3
return myFriend;
}
/**
* Method that returns a String with the list of friends' names and phone numbers.
* @return a String with the list of friends' names and phone numbers.
*/
public String getListOfFriends()
{
String listOfFriends = "";
if(friend1 != null)
{
//README When you successfully complete the class Friend, uncomment the next line of code
listOfFriends = listOfFriends + friend1.getName() + ": " + friend1.getPhoneNumber() + "\n";
}else if(friend2 != null) {
listOfFriends = listOfFriends + friend2.getName() + ": " + friend2.getPhoneNumber() + "\n";
}else if(friend3 != null) {
listOfFriends = listOfFriends + friend3.getName() + ": " + friend3.getPhoneNumber() + "\n";
}
//COMPLETE the code for friend2
//COMPLETE the code for friend3
return listOfFriends;
}
}
该方法运行后,应向addNewFriendMethod返回名称或null