这是引发错误的代码。在 TextPad 中,当我尝试将数组的内容写入文本文件时,我得到了 NullPointerException。(它在数组中看不到任何东西。)注意:它在 Netbeans 中完美运行。我只在Textpad中得到这个。我已经搜索了谷歌,但我不知道它为什么这样做。
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();