我有这段代码:
EventSnapshot snapshotEvent = new EventSnapshot();
EventSnapshot currentEvent = new EventSnapshot();
do
{
...
currentEvent.setContent(in_stats);
...
if(!snapshotEvent.equals(currentEvent))
snapshotEvent.setContent(in_stats);
}
while(true);
在第一个周期我得到一个NullPointerException
,因为显然对象 snapshotEvent 是空的。在第二个周期这不会发生。如何避免这个问题?
编辑
这是错误,它发生在这里:
java.lang.NullPointerException
at org.w3c.jigmonitor.EventSnapshot.equals(EventSnapshot.java:40)
public class EventSnapshot
{
private Statistics content;
public EventSnapshot()
{
super();
}
/**
* @return the content
*/
public Statistics getContent()
{
return content;
}
/**
* @param content the content to set
*/
public void setContent(Statistics content)
{
this.content = content;
}
public boolean equals(EventSnapshot es)
{
return this.content.equals(es.getContent()); // here...
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "EventSnapshot [content=" + content + "]";
}
}