Java 之后=>;空指针
我得到了这段代码,我正在尝试比较一个时间戳数组:Java 之后=>;空指针,java,timestamp,Java,Timestamp,我得到了这段代码,我正在尝试比较一个时间戳数组: Timestamp oldestTStamp = timeStamp[0]; boolean found = false; int temp = 0; for (int j = 1; j<timeStamp.length; j++){ if(oldestTStamp == null && timeStamp[j] == null){} else if(oldestTStamp.after(timeSta
Timestamp oldestTStamp = timeStamp[0];
boolean found = false;
int temp = 0;
for (int j = 1; j<timeStamp.length; j++){
if(oldestTStamp == null && timeStamp[j] == null){}
else if(oldestTStamp.after(timeStamp[j])){
oldestTStamp = timeStamp[j];
//Retrieve the oldest timestamp index in array
found = true;
temp = j;
}
您的
if
条件是以一种非常奇怪的方式编写的,它只检查时间戳
是否都是null
你可能是想写点什么
if (oldestTStamp != null && timeStamp[j] != null && oldestTStamp.after(timeStamp[j])) {
...
这样,只有当
oldestTStamp
和timeStamp[j]
都非空时,才会调用after()
方法。您应该添加异常的完整堆栈跟踪,否则只是猜测
问题可能出在这一行:
if(oldestTStamp == null && timeStamp[j] == null){}
应该是
if(oldestTStamp == null || timeStamp[j] == null){}
核实
if( oldestTStamp != null &&
timeStamp[j] != null &&
oldestTStamp.after(timeStamp[j])){
}
if(oldestTStamp==null&&timeStamp[j]==null)
仅当两个值均为null
时才为真。如果oldestTStamp
是null
,但timeStamp[j]
不是,您将得到提到的NullPointerException
。试着用if(oldestTStamp==null | | timeStamp[j]==null)
来检查它们中的任何一个是否为null
。请把你的标题调整得更有意义——我不明白它此刻想要表达什么。不,这不是正确的态度。堆栈溢出的要点是创建一个持久的好问题库来帮助其他人。你的帖子目前没有这样做。如果你的态度是“去他妈的,我现在已经得到了我想要的”,你很可能会发现其他人在将来不太愿意帮助你。
if( oldestTStamp != null &&
timeStamp[j] != null &&
oldestTStamp.after(timeStamp[j])){
}