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?
The
whileloop stops when thereadline()isnulland writes the current value to the variablechrome_count_S.So
chrome_count_Sis benullafter the loop and at the thewritecommand.=== UPDATE ===
Remove the
chrome_count_Srow in the loop and take the value from thechrome_count_Iduring writing: