Here is the code throwing the error. In TextPad, I get a NullPointerException when I try to write the contents of the array to the text file. (It doesn't see anything in the array.) Note: it works perfectly in Netbeans. I only get this in Textpad. I have scoured Google and i have no idea why it is doing this.
void enterContact(){
// test contact
contactName = nameField.getText();
if (contactName == null || contactName.equals(""))
{
JOptionPane.showMessageDialog(null, "Please enter a name.");
return;
}
//test age betweeen 0 and 120
contactAge = ageField.getText();
try{
Integer.parseInt(contactAge);
}
catch(NumberFormatException e){
JOptionPane.showMessageDialog(null, "Please enter a valid age.");
}
finally{
if (Integer.parseInt(contactAge) <= 0 || Integer.parseInt(contactAge) >= 121){
JOptionPane.showMessageDialog(null, "Please enter a valid age.");
return;
}
}
// test email
contactEmail = emailField.getText();
if ( contactEmail == null || contactEmail.equals(""))
{
JOptionPane.showMessageDialog(null, "Please Enter an Email Address.");
return;
}
//test cell number
contactPhone = phoneField.getText();
try
{
Integer.parseInt(contactPhone);
}
catch(Exception e) {
JOptionPane.showMessageDialog(null, "Please Enter a valid Phone Number.");
return;
}
String columns2[] = { contactName, contactAge, contactEmail, contactPhone };
//write data to file
try{
for (int i = 0; i < columns2.length; i++){
fw.write(columns2[i].toString() + ", ");
}
fw.write("\r\n");
fw.flush();
fw.close();
If you get NullPointerException
during writing in file, there is two possibilities:
You may not open the file. For example because of forgetting calling the constructor of FileWriter
.
You don't have any permission to open the file and write into it.
If you got the exception only when you are run it using TextPad, I think the second problems occurred. You don't have permission.