Rilcon42
Rilcon42

Reputation: 9763

Java Display message issue

I have a java program that creates a JFrame like this:

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

I also have a class (gui.java) that implements 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);   
}
});
}

If the user tries to click Submit (a button I created) and the fields in the JFrame are not filled in then it throws an error. The code for the error message is:

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

});

//Note: f1 is a static version of the frame I initially received

The error I get is:

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)

What did I do wrong?

Upvotes: 0

Views: 252

Answers (2)

JB Nizet
JB Nizet

Reputation: 691755

One of the variables is null. It could be:

  • chooser
  • chooser.getSelectedFile()
  • saveChooser
  • saveChooser.getSelectedFile()
  • parseFile
  • startSpanText
  • col2 to col6

Use the line number in the stack trace, and a debugger or traces in the code to know which one. My guess would be one of the selected files, since it makes no sense to call

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

Either there is no selected file, and it throws an NPE because getSelectedFile() returns null, or there is one, and its toString() will never be null.

Also, you shouldn't use a mouse listener to do something when a button is pressed. That's what an ActionListener is for. It will be simpler, and also work when the user presses the button with its keyboard.

Upvotes: 2

Duncan Jones
Duncan Jones

Reputation: 69339

Your exception message should point you to the answer. One of the items you are using in your mouseReleased method is 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");
}

Use a debugger to inspect which variables are set. Use the line number in the exception message to help you.

Upvotes: 0

Related Questions