NullPointerException(当 String 初始化时)

发布于 2024-12-12 06:43:03 字数 1073 浏览 0 评论 0原文

以下是抛出 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

禾厶谷欠 2024-12-19 06:43:03

readline()null 时,while 循环停止,并将当前值写入变量 chrome_count_S

while((chrome_count_S = br.readLine()) != null)

因此,在循环之后和执行 write 命令时,chrome_count_Snull

===更新===

删除循环中的chrome_count_S行,并在写入过程中从chrome_count_I获取值:

while((chrome_count_S = br.readLine()) != null) {
    chrome_count_I = Integer.parseInt(chrome_count_S);
    chrome_count_I++;
}
writer.write(Integer.toString(chrome_count_I));

The while loop stops when the readline() is null and writes the current value to the variable chrome_count_S.

while((chrome_count_S = br.readLine()) != null)

So chrome_count_S is be null after the loop and at the the write command.

=== UPDATE ===

Remove the chrome_count_S row in the loop and take the value from the chrome_count_I during writing:

while((chrome_count_S = br.readLine()) != null) {
    chrome_count_I = Integer.parseInt(chrome_count_S);
    chrome_count_I++;
}
writer.write(Integer.toString(chrome_count_I));
风流物 2024-12-19 06:43:03

您的 while 循环不存在,直到 chrome_count_Snull。因此,对 writer.write() 的调用当然会抛出 NullPointerException 。

Your while loop doesn't exist until chrome_count_S is null. So of course the call to writer.write() will throw a NullPointerException.

娇俏 2024-12-19 06:43:03

可能是因为在 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 loop

while((chrome_count_S = br.readLine()) != null)

which ends only when br.readline() puts a NULL in the chrome_count_S

迎风吟唱 2024-12-19 06:43:03

即使您初始化了它,在某些时候 br.readLine() 也会将 null 分配给 chroe_count_S。

Even though you initialized it, at some point br.readLine() assigns null to chroe_count_S.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文