Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 之后=>;空指针_Java_Timestamp - Fatal编程技术网

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])){


}