JOptionPane 中的 NullpointerException
NullpointerException in JOptionPane
****它在 JOptionPane 代码中抛出 NullPointerException,如下所示。****
public static void main(String[]args){
String[] options={"Do it again?", "Exit"};
int response=0;
int indexOfZip;
do{
try{
String dataStr=JOptionPane.showInputDialog("Enter a zip code");
if(dataStr!=null){
while (k<i){
if (place[k].equals(dataStr)){
indexOfZip=k;
k++;
}
}
int data=Integer.parseInt(dataStr);
response=JOptionPane.showOptionDialog(null,
"You asked me to search for zip code:"+dataStr+"The zip code "+dataStr+" belongs to"+place[k].getTown()+","+place[k].getState()+"Do you want me to search again?",
"results",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
options,options[0]);
System.out.println("You asked me to search for zip code:"+dataStr);
System.out.println("The zip code "+dataStr+" belongs to"+place[k].getTown()+","+place[k].getState());
System.out.println("Do you want me to search again?");
}
}catch(NumberFormatException e){
JOptionPane.showMessageDialog(null,"Bad Numeric String, try again.", "Input Error",JOptionPane.ERROR_MESSAGE);
}
}while(response==0);
if (response!=0){
System.out.print("No");
System.out.println("Good Bye!");
}
}
终端说异常是针对这一行
响应=JOptionPane.showOptionDialog(空,
但是我看不出这里有什么问题,因为"response"之前已经初始化了。
看来place[k]
一定是null
,
place[k].getTown()
触发 NullPointerException
。
在调试器中观察 place[k]
的值(或添加打印语句)。
****它在 JOptionPane 代码中抛出 NullPointerException,如下所示。****
public static void main(String[]args){
String[] options={"Do it again?", "Exit"};
int response=0;
int indexOfZip;
do{
try{
String dataStr=JOptionPane.showInputDialog("Enter a zip code");
if(dataStr!=null){
while (k<i){
if (place[k].equals(dataStr)){
indexOfZip=k;
k++;
}
}
int data=Integer.parseInt(dataStr);
response=JOptionPane.showOptionDialog(null,
"You asked me to search for zip code:"+dataStr+"The zip code "+dataStr+" belongs to"+place[k].getTown()+","+place[k].getState()+"Do you want me to search again?",
"results",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
options,options[0]);
System.out.println("You asked me to search for zip code:"+dataStr);
System.out.println("The zip code "+dataStr+" belongs to"+place[k].getTown()+","+place[k].getState());
System.out.println("Do you want me to search again?");
}
}catch(NumberFormatException e){
JOptionPane.showMessageDialog(null,"Bad Numeric String, try again.", "Input Error",JOptionPane.ERROR_MESSAGE);
}
}while(response==0);
if (response!=0){
System.out.print("No");
System.out.println("Good Bye!");
}
}
终端说异常是针对这一行 响应=JOptionPane.showOptionDialog(空,
但是我看不出这里有什么问题,因为"response"之前已经初始化了。
看来place[k]
一定是null
,
place[k].getTown()
触发 NullPointerException
。
在调试器中观察 place[k]
的值(或添加打印语句)。