所以我使用堆栈对象执行撤消和重做功能,但当我尝试撤消将朋友添加到数组列表时,我收到此错误:
Exception in thread "main" java.lang.NullPointerException
at main.main(main.java:256)
这里是主要出现错误的行:
else if("friend".equals(temp.getAction())) //add friend
{
int newFriendUserIndex = facebook.findUser(temp.getUser2());
user.defriend(facebook.getUser(newFriendUserIndex));//Error is here
MyStackObject undo = new MyStackObject(user.toString(), "deFriend", temp.getUser2());
facebook.redoList.push(undo);
}
MyStackObject类
import java.util.*;
import java.io.Serializable;
public class MyStackObject implements Serializable
{
private String user1 = null;
private String action = null;
private String user2 = null;
private String like = null;
private FacebookUser user;
public MyStackObject(String user1, String action, String user2)
{
this.user1 = user1;
this.action = action;
this.user2 = user2;
}
public MyStackObject(String user1, String like)
{
this.user1 = user1;
this.like = like;
}
public MyStackObject(FacebookUser user, String action)
{
this.user = user;
this.user1 = user.toString();
this.action = action;
}
public FacebookUser getUser()
{
return user;
}
public void setUser(FacebookUser user)
{
this.user = user;
}
public String getUser1()
{
return user1;
}
public void setUser1(String user1)
{
this.user1 = user1;
}
public String getAction()
{
return action;
}
public void setAction(String action)
{
this.action = action;
}
public String getUser2()
{
return user2;
}
public void setUser2(String user2)
{
this.user2 = user2;
}
public String getLike()
{
return like;
}
public void setLike(String like)
{
this.like = like;
}
}
MyStack Class
//EXPLANATION:
public class MyStack implements Serializable
{
private java.util.ArrayList<MyStackObject> undoList = new java.util.ArrayList<MyStackObject>();
public MyStack()
{
}
public MyStackObject peek()
{
return undoList.get(getSize() - 1);
}
public int getSize()
{
return undoList.size();
}
public void push(MyStackObject temp)
{
undoList.add(temp);
}
public MyStackObject pop()
{
MyStackObject temp = undoList.get(getSize() - 1);
undoList.remove(getSize() - 1);
return temp;
}
public boolean isEmpty()
{
return undoList.isEmpty();
}
}
我发现其他的东西是null,这是撤销选项:
case 12://Undo
if(facebook.undoList.isEmpty() && facebook.redoList.isEmpty())
{
System.out.println("\n\tNo recent actions have been done yet!");
}
else
{
MyStackObject temp = null;
int secondMenu = undoMenu();
String userName;
String password;
boolean match = false;
boolean quit2 = false;
Scanner input2 = new Scanner(System.in);
FacebookUser user = null;
但我也不确定该改变它。