gpt4 book ai didi

Java NullPointerException 即使对象存在

转载 作者:行者123 更新时间:2023-11-29 09:35:43 24 4
gpt4 key购买 nike

我在调用方法并向其传递字符串的行中收到 NullPointerException。我只能假设该字符串为空。

它是从几行之前的 BufferedReader readline 初始化的。相关代码如下:

FileInputStream tmp = null;
try {
tmp = new FileInputStream(file);
} catch (FileNotFoundException e) {
System.err.println("File not found!");
e.printStackTrace();
System.exit(1);
}
DataInputStream dis = new DataInputStream(tmp);
BufferedReader br = new BufferedReader(new InputStreamReader(dis));

create();

try {
data = br.readLine();
} catch (IOException e) {
System.err.println("First readline failed: " + e);
System.exit(1);
}
while (!data.equals(null)) {
process(data);
...

和错误:

Exception in thread "main" java.lang.NullPointerException
at enc.read(enc.java:40)
at enc.main(enc.java:15)

最佳答案

您的 nullness 检查本身会触发 NullPointerException!data.equals(null) 写成 data != null

关于Java NullPointerException 即使对象存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14613076/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com