gpt4 book ai didi

java - 为什么没有在正确的行上报告 NPE

转载 作者:行者123 更新时间:2023-12-03 11:20:09 26 4
gpt4 key购买 nike

运行以下内容:

public class NPESample {

String value;

public static void main(String[] args) {
NPESample npeSample = null;
"foo"
.replaceAll(
"f",
npeSample.value);
}
}
会给:
Exception in thread "main" java.lang.NullPointerException
at NPESample.main(NPESample.java:9)
报告的第 (7) 行是 "foo"而不是发生 NPE 的实际线路 (10) npeSample.value .
为什么是这样?我在调试代码时感到惊讶,并被代码中报告的“错误”行号所抛弃。
要重现运行这个:
cat > ./NPESample.java <<DELIM
public class NPESample {

String value;

public static void main(String[] args) {
NPESample npeSample = null;
"foo"
.replaceAll(
"f",
npeSample.value);
}
}
DELIM

javac NPESample.java
java NPESample
输出是:
Exception in thread "main" java.lang.NullPointerException
at NPESample.main(NPESample.java:7)
javac -version
javac 13.0.1
java -version
openjdk version "13.0.1" 2019-10-15
OpenJDK Runtime Environment (build 13.0.1+9)
OpenJDK 64-Bit Server VM (build 13.0.1+9, mixed mode, sharing)

最佳答案

对我来说,这看起来不像是一个错误。具体来说,好像是自由映射不是一对一的,as the SPEC says :

That is, LineNumberTable attributes may together represent a given line of a source file, and need not be one-to-one with source lines.


JDK-15报告错误的行:

Cannot read field "value" because "npeSample" is null


在线 9 .

关于java - 为什么没有在正确的行上报告 NPE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64022079/

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