0

I'm making a swing aplication, which works "fine" on windows, but it sends an error on the (*) line.

public static void main(String[] args) throws ParseException, java.lang.InstantiationException {

    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(MainView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(MainView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(MainView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }




    /* Create and display the dialog */
    java.awt.EventQueue.invokeLater(() -> {
        MainView dialog = null;
        try {
            dialog = new MainView(new javax.swing.JFrame(), true);
        } catch (ParseException | SQLException | ClassNotFoundException ex) {
            Logger.getLogger(MainView.class.getName()).log(Level.SEVERE, null, ex);
        }

        dialog.setVisible(true); /// (*) This line throws the error
    });
}

I already tried changing the code to what netbeans suggested, and it keeps showing the same error.

This is what netbeans changes:

    java.awt.EventQueue.invokeLater(new Runnable() {

        @Override
        public void run() {
            MainView dialog = null;
            try {
                dialog = new MainView(new javax.swing.JFrame(), true);
            } catch (ParseException | SQLException | ClassNotFoundException ex) {
                Logger.getLogger(MainView.class.getName()).log(Level.SEVERE, null, ex);
            }

            dialog.setVisible(true); /// (*) This line throws the error
        }
    });

Here is the error:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
        at view.MainView.lambda$main$0(MainView.java:2842)
        at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
        at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
        at java.awt.EventQueue.access$500(EventQueue.java:97)
        at java.awt.EventQueue$3.run(EventQueue.java:709)
        at java.awt.EventQueue$3.run(EventQueue.java:703)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
        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)

1 Answers1

3

Why would you ever want to set the dialog to visible if creating the dialog has failed?

try {
   dialog = new MainView(new javax.swing.JFrame(), true);
   dialog.setVisible(true); //  <--- move it here
} catch (ParseException | SQLException | ClassNotFoundException ex) {
   Logger.getLogger(MainView.class.getName()).log(Level.SEVERE, null, ex);
}

Additionally

You should use imports (import java.util.logging.*) in a way that stops you having to write extremely ugly lines like

} catch (ClassNotFoundException ex) {
    java.util.logging.Logger.getLogger(MainView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
    java.util.logging.Logger.getLogger(MainView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
    java.util.logging.Logger.getLogger(MainView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}

and simplify them to

} catch (ClassNotFoundException ex) {
    Logger.getLogger(MainView.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
    Logger.getLogger(MainView.class.getName()).log(Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
    Logger.getLogger(MainView.class.getName()).log(Level.SEVERE, null, ex);
}
Ross Drew
  • 8,163
  • 2
  • 41
  • 53