0

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

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

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

  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);   
}
});
}

如果用户尝试单击提交(我创建的一个按钮)并且 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)

我做错什么了?

4

2 回答 2

2

其中一个变量为空。它可能是:

  • 选择器
  • 选择器.getSelectedFile()
  • 保存选择器
  • saveChooser.getSelectedFile()
  • 解析文件
  • 开始跨度文本
  • col2 到 col6

使用堆栈跟踪中的行号和代码中的一个或多个调试器来了解是哪一个。我的猜测是选定的文件之一,因为调用没有意义

chooser.getSelectedFile().toString() != null

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

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

于 2013-01-04T23:37:05.713 回答
0

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

@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");
}

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

于 2013-01-04T23:36:00.927 回答