无法弄清楚如何处理这些错误
Can't figure out how to deal with these errors
我构建了一个小软件。该软件运行正常。在启动软件时,会弹出一个选项窗格询问名称,用户可以输入名称然后选择确定或只是取消并终止 运行 软件。我现在的问题是,如果用户单击取消选项,它会终止然后抛出一堆错误。这些错误我不明白,我几乎对我创建的每个方法都使用了适当的异常。有没有人对发生的事情有任何建议或想法。
这是 RequestAngentName Class
public class RequestAgentName{
// instance variable daclaration
private static String uName;
/*
recursive method set username to instance variable
*/
public void setUserName(String name) throws NullPointerException
{
name = JOptionPane.showInputDialog(null, "Insert First Name and Last Initial", "Agent Name",
JOptionPane.PLAIN_MESSAGE);
// if user input is not empty
(line 24)if(!name.isEmpty())
{
// verify input is acceptable, if yes assign input to global variable
if(JOptionPane.showConfirmDialog(null, "Is This Your Name?\n\n " + name, "WARNING" , JOptionPane.YES_NO_OPTION) == JOptionPane.YES_NO_OPTION)
{
uName = name;
} // end if
// if input is not acceptable prompt user to reenter name
else
// if username is wrong reenter until correct
this.setUserName(name);
} // end if
// if input is empty prompt user to reenter
else
this.setUserName(name);
} //end method set username
// method to return user input
public String getUserName(){
return uName;
} // end method get username
} // end of Request name class
这是我在main中调用它的方式,我觉得这可能是错误所在
RequestAgentName userName = new RequestAgentName();
try {
(line 52) userName.setUserName(userName.getUserName());
new FNAFrame();
(new Thread(new FnaComponents())).start();
}
catch (NullPointerException npe){
npe.printStackTrace();
}
报错如下;
java.lang.NullPointerException
at fna.comments.generator.RequestAgentName.setUserName(RequestAgentName.java:24)
at fna.comments.generator.FNAFrame.run(FNAFrame.java:52)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:749)
at java.awt.EventQueue.access0(EventQueue.java:97)
at java.awt.EventQueue.run(EventQueue.java:702)
at java.awt.EventQueue.run(EventQueue.java:696)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:719)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
构建成功(总时间:6 秒)
当你
name = JOptionPane(...);
并且当用户按下取消时,JOptionPane returns null
。所以,名字是 null
.
因此
if(!name.isEmpty())
变成
if(!null.isEmpty())
您不能在 null
上调用方法。因此 NullPointerException
被抛出。一个简单的修复方法是:
if(name!=null && !name.isEmpty())
编辑
糟糕,我没有看到对整个代码的影响。最好像这样修复它:
if (name == null){
// User hit cancel, so simply return/terminate
return;
} else if (!name.isEmpty()) {
// User pressed OK with a value
} else
// User pressed OK without value
我构建了一个小软件。该软件运行正常。在启动软件时,会弹出一个选项窗格询问名称,用户可以输入名称然后选择确定或只是取消并终止 运行 软件。我现在的问题是,如果用户单击取消选项,它会终止然后抛出一堆错误。这些错误我不明白,我几乎对我创建的每个方法都使用了适当的异常。有没有人对发生的事情有任何建议或想法。
这是 RequestAngentName Class
public class RequestAgentName{
// instance variable daclaration
private static String uName;
/*
recursive method set username to instance variable
*/
public void setUserName(String name) throws NullPointerException
{
name = JOptionPane.showInputDialog(null, "Insert First Name and Last Initial", "Agent Name",
JOptionPane.PLAIN_MESSAGE);
// if user input is not empty
(line 24)if(!name.isEmpty())
{
// verify input is acceptable, if yes assign input to global variable
if(JOptionPane.showConfirmDialog(null, "Is This Your Name?\n\n " + name, "WARNING" , JOptionPane.YES_NO_OPTION) == JOptionPane.YES_NO_OPTION)
{
uName = name;
} // end if
// if input is not acceptable prompt user to reenter name
else
// if username is wrong reenter until correct
this.setUserName(name);
} // end if
// if input is empty prompt user to reenter
else
this.setUserName(name);
} //end method set username
// method to return user input
public String getUserName(){
return uName;
} // end method get username
} // end of Request name class
这是我在main中调用它的方式,我觉得这可能是错误所在
RequestAgentName userName = new RequestAgentName();
try {
(line 52) userName.setUserName(userName.getUserName());
new FNAFrame();
(new Thread(new FnaComponents())).start();
}
catch (NullPointerException npe){
npe.printStackTrace();
}
报错如下;
java.lang.NullPointerException
at fna.comments.generator.RequestAgentName.setUserName(RequestAgentName.java:24)
at fna.comments.generator.FNAFrame.run(FNAFrame.java:52)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:749)
at java.awt.EventQueue.access0(EventQueue.java:97)
at java.awt.EventQueue.run(EventQueue.java:702)
at java.awt.EventQueue.run(EventQueue.java:696)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:719)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
构建成功(总时间:6 秒)
当你
name = JOptionPane(...);
并且当用户按下取消时,JOptionPane returns null
。所以,名字是 null
.
因此
if(!name.isEmpty())
变成
if(!null.isEmpty())
您不能在 null
上调用方法。因此 NullPointerException
被抛出。一个简单的修复方法是:
if(name!=null && !name.isEmpty())
编辑
糟糕,我没有看到对整个代码的影响。最好像这样修复它:
if (name == null){
// User hit cancel, so simply return/terminate
return;
} else if (!name.isEmpty()) {
// User pressed OK with a value
} else
// User pressed OK without value