NullPointerException(当 String 初始化时)
以下是抛出 java.lang.NullPointerException 的代码片段。
else if(jRadioButton2.isSelected()) {
// chrome selected
String chrome_count_S="0";
int chrome_count_I=0;
FileWriter writer = new FileWriter("D:\\UnderTest\\MyFirstPoll\\build\\classes\\poll_count\\Chrome.txt");
FileReader reader = new FileReader("D:\\UnderTest\\MyFirstPoll\\build\\classes\\poll_count\\Chrome.txt");
BufferedReader br = new BufferedReader(reader);
while((chrome_count_S = br.readLine()) != null) {
chrome_count_I = Integer.parseInt(chrome_count_S);
chrome_count_I++;
chrome_count_S = Integer.toString(chrome_count_I);
}
writer.write(chrome_count_S);
writer.close();
当遇到此代码片段时,会抛出 NullPointerException 。如果我将 writer.write(chrome_count_S);
的参数替换为 writer.write("chrome_count_S");
IE 一个 String
,我不没有任何例外。否则为什么当我初始化字符串 chrome_count_S
时会出现异常?
Following is a snippet that throws java.lang.NullPointerException
.
else if(jRadioButton2.isSelected()) {
// chrome selected
String chrome_count_S="0";
int chrome_count_I=0;
FileWriter writer = new FileWriter("D:\\UnderTest\\MyFirstPoll\\build\\classes\\poll_count\\Chrome.txt");
FileReader reader = new FileReader("D:\\UnderTest\\MyFirstPoll\\build\\classes\\poll_count\\Chrome.txt");
BufferedReader br = new BufferedReader(reader);
while((chrome_count_S = br.readLine()) != null) {
chrome_count_I = Integer.parseInt(chrome_count_S);
chrome_count_I++;
chrome_count_S = Integer.toString(chrome_count_I);
}
writer.write(chrome_count_S);
writer.close();
When this snippet is encountered NullPointerException
is thrown. If I replace the argument of writer.write(chrome_count_S);
to writer.write("chrome_count_S");
I.E. a String
, I don't get any exception. Otherwise why do I get the exception when I have initialized the string chrome_count_S
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当
readline()
为null
时,while
循环停止,并将当前值写入变量chrome_count_S
。因此,在循环之后和执行 write 命令时,
chrome_count_S
为null
。===更新===
删除循环中的
chrome_count_S
行,并在写入过程中从chrome_count_I
获取值:The
while
loop stops when thereadline()
isnull
and writes the current value to the variablechrome_count_S
.So
chrome_count_S
is benull
after the loop and at the thewrite
command.=== UPDATE ===
Remove the
chrome_count_S
row in the loop and take the value from thechrome_count_I
during writing:您的
while
循环不存在,直到chrome_count_S
为null
。因此,对 writer.write() 的调用当然会抛出 NullPointerException 。Your
while
loop doesn't exist untilchrome_count_S
isnull
. So of course the call towriter.write()
will throw aNullPointerException
.可能是因为在 writer.write 之前,有一个 while 循环
while((chrome_count_S = br.readLine()) != null)
仅当
br 时结束.readline()
将NULL
放入chrome_count_S
probably because before the
writer.write
, you have the while loopwhile((chrome_count_S = br.readLine()) != null)
which ends only when
br.readline()
puts aNULL
in thechrome_count_S
即使您初始化了它,在某些时候 br.readLine() 也会将 null 分配给 chroe_count_S。
Even though you initialized it, at some point br.readLine() assigns null to chroe_count_S.