显示消息问题



我有一个创建JFrame的java程序,像这样:

        javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            gui.setFrame(new gui(), 1000, 300);
        }
    });

我也有一个类(gui.java)实现setFrame:

  public static void setFrame(final JFrame frame, final int width, final int height) {
 SwingUtilities.invokeLater(new Runnable() {
 public void run() {
   f1=frame;
 frame.setTitle("Testing");
 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 frame.setSize(width, height);
 frame.setVisible(true);   
}
});
}

如果用户试图单击Submit(我创建的一个按钮),而JFrame中的字段没有被填充,那么它会抛出一个错误。错误消息的代码是:

     submit.addMouseListener(new MouseListener(){
@Override
public void mouseClicked(MouseEvent e) {}
@Override
public void mouseEntered(MouseEvent e) {}
@Override
public void mouseExited(MouseEvent e) {}
@Override
public void mousePressed(MouseEvent e) {}
@Override
public void mouseReleased(MouseEvent e) {
    //check to make sure all values filled in
          if(chooser.getSelectedFile().toString()!=null&&saveChooser.getSelectedFile().toString()!=null)
        parseFile.readFile(chooser.getSelectedFile(),saveChooser.getSelectedFile(),startSpanText.getText(),(String)col2.getSelectedItem(),(String)col3.getSelectedItem(),(String)col4.getSelectedItem(),(String)col5.getSelectedItem(),(String)col6.getSelectedItem());     
    else
        JOptionPane.showMessageDialog(f1,"Bad");
}

});

//注意:f1是初始接收帧的静态版本

我得到的错误是:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at gui$3.mouseReleased(gui.java:133)
at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

我做错了什么?

其中一个变量为null。可以是:

  • 选择者
  • chooser.getSelectedFile ()
  • saveChooser
  • saveChooser.getSelectedFile ()
  • parseFile
  • startSpanText
  • col2到col6

使用堆栈跟踪中的行号,并在代码中使用调试器或跟踪来知道是哪一个。我猜应该是选中的文件之一,因为调用

没有意义。
chooser.getSelectedFile().toString() != null

要么没有选择的文件,因为getSelectedFile()返回null而抛出一个NPE,要么有一个,它的toString()永远不会为null。

同样,当按钮被按下时,您不应该使用鼠标侦听器来执行某些操作。这就是ActionListener的作用。它将更简单,并且当用户用键盘按下按钮时也可以工作。

您的异常消息应该指向答案。您在mouseReleased方法中使用的项目之一是null:

@Override
public void mouseReleased(MouseEvent e) {
  //check to make sure all values filled in
  if(chooser.getSelectedFile().toString()!=null && 
      saveChooser.getSelectedFile().toString()!=null)
    parseFile.readFile(chooser.getSelectedFile(), saveChooser.getSelectedFile(),
        startSpanText.getText(), (String)col2.getSelectedItem(), 
        (String)col3.getSelectedItem(), (String)col4.getSelectedItem(),
        (String)col5.getSelectedItem(), (String)col6.getSelectedItem());     
  else
    JOptionPane.showMessageDialog(f1,"Bad");
}

使用调试器检查设置了哪些变量。使用异常消息中的行号来帮助您。

相关内容

  • 没有找到相关文章

最新更新