为什么在下面的第二个 if 语句中出现空指针错误?
如果数组字符串为空,我正在检查该 if 语句scores
,但它总是崩溃说空指针异常。如果有帮助,我还添加了 LogCat。
String[] scores;
String times = "";
public void Read() {
FileInputStream inputStream = null;
try {
inputStream = openFileInput("timesfile");
byte[] reader = new byte[inputStream.available()];
while (inputStream.read(reader) != -1) {
}
times = new String(reader);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
if (!times.isEmpty() && times != null && times != ""
&& times.length() != 0)
{
scores = new String(times).split(",");
Arrays.sort(scores);
}
if (scores.length != 0 && scores != null && scores.length >= 4) { //I get a nullpointer exception here!
better = false;
if (Integer.parseInt(endTime.split(":")[0]) > Integer
.parseInt(scores[0].split(":")[0])) {
better = true;
} else if (Integer.parseInt(endTime.split(":")[1]) > Integer
.parseInt(scores[0].split(":")[1])) {
better = true;
} else if (Integer.parseInt(endTime.split(":")[2]) > Integer
.parseInt(scores[0].split(":")[2])) {
better = true;
} else {
better = false;
}
}
}
日志猫:
11-16 22:32:21.195: E/AndroidRuntime(28584): FATAL EXCEPTION: Thread-2278
11-16 22:32:21.195: E/AndroidRuntime(28584): java.lang.NullPointerException
11-16 22:32:21.195: E/AndroidRuntime(28584): at com.example.app.TestActivity$TestView.scoreRead(TestActivity.java:426)
11-16 22:32:21.195: E/AndroidRuntime(28584): at com.example.app.TestActivity$TestView.over(TestActivity.java:303)
11-16 22:32:21.195: E/AndroidRuntime(28584): at com.example.app.TestActivity$TestView.run(TestActivity.java:267)
11-16 22:32:21.195: E/AndroidRuntime(28584): at java.lang.Thread.run(Thread.java:856)