Suh*_*pta 0 java string nullpointerexception
以下是抛出的片段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();
Run Code Online (Sandbox Code Playgroud)
当遇到这个片段时NullPointerException会抛出.如果我更换的参数writer.write(chrome_count_S);,以writer.write("chrome_count_S");IE浏览器String,我没有得到任何的异常.否则,为什么在初始化字符串时会出现异常chrome_count_S?
在while当循环停止readline()是null和电流值写入到变量chrome_count_S.
while((chrome_count_S = br.readLine()) != null)
Run Code Online (Sandbox Code Playgroud)
所以chrome_count_S是null循环之后和write命令.
===更新===
删除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));
Run Code Online (Sandbox Code Playgroud)