0

Can somebody help me with this error ?

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at oopw4.FormManager.searchForm(FormManager.java:51)
    at oopw4.FormManager.unregisterForm(FormManager.java:40)

FormManager.class

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package oopw4;

import java.util.ArrayList;
import javax.swing.JInternalFrame;

public class FormManager {

    private JInternalFrame openedForm = null;
    private ArrayList<JInternalFrame> frameList = new ArrayList<>();

    public boolean isFormOpened(String namaForm) {
        boolean isFound = false;
        JInternalFrame jif = searchForm(namaForm);
        if (jif != null) {
            isFound = true;
            openedForm = jif;
        }
        return isFound;
    }

    public JInternalFrame getOpenedForm() {
        return openedForm;
    }

    public void registerForm(JInternalFrame frame) {
        frameList.add(frame);
    }

    public void unregisterForm(JInternalFrame frame) {
        JInternalFrame jif = (JInternalFrame)searchForm(frame.getName());
        if (jif != null) {
            frameList.remove(frame);
        }
    }

    private JInternalFrame searchForm(String namaForm) {
        JInternalFrame frame = null;
        int idx = 0;

        while (idx < frameList.size()) {
            if (frameList.get(idx).getName().equals(namaForm)) {
                frame = frameList.get(idx);
                break;
            } else {
                idx++;
            }
        }
        return frame;
    }
}

UserCrud.class

private void closeButtonActionPerformed(java.awt.event.ActionEvent evt) {                                            
        // TODO add your handling code here:
        AppLib.formManager.unregisterForm(this);
    }

MainFormDemo.class

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package oopw4;

import javax.swing.*;
import simbengkel.AppLib;

public class MainFormDemo extends javax.swing.JFrame {

    /**
     * Creates new form Frame   
     */
    public MainFormDemo() {
        initComponents();
        setExtendedState(JFrame.MAXIMIZED_BOTH);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        DesktopPane = new javax.swing.JDesktopPane();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        menuOpenedForm = new javax.swing.JMenuItem();
        openFormTableUser = new javax.swing.JMenuItem();
        menuClosedForm = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        menuHelp = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setLocation(new java.awt.Point(0, 0));
        getContentPane().setLayout(new java.awt.GridLayout(1, 0));

        javax.swing.GroupLayout DesktopPaneLayout = new javax.swing.GroupLayout(DesktopPane);
        DesktopPane.setLayout(DesktopPaneLayout);
        DesktopPaneLayout.setHorizontalGroup(
            DesktopPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 507, Short.MAX_VALUE)
        );
        DesktopPaneLayout.setVerticalGroup(
            DesktopPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 319, Short.MAX_VALUE)
        );

        getContentPane().add(DesktopPane);

        jMenu1.setText("File");

        menuOpenedForm.setText("Tic Tac Toe");
        menuOpenedForm.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuOpenedFormActionPerformed(evt);
            }
        });
        jMenu1.add(menuOpenedForm);

        openFormTableUser.setText("Table User");
        openFormTableUser.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                openFormTableUserActionPerformed(evt);
            }
        });
        jMenu1.add(openFormTableUser);

        menuClosedForm.setText("Logout");
        menuClosedForm.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuClosedFormActionPerformed(evt);
            }
        });
        jMenu1.add(menuClosedForm);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Edit");

        menuHelp.setText("Help");
        jMenu2.add(menuHelp);

        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>                        

    private void menuOpenedFormActionPerformed(java.awt.event.ActionEvent evt) {                                               
        // TODO add your handling code here:
        this.dispose();
        TicTacToe main = new TicTacToe();
        main.show();
    }                                              

    private void menuClosedFormActionPerformed(java.awt.event.ActionEvent evt) {                                               
        // TODO add your handling code here:
        this.dispose();
        LoginUser login = new LoginUser();
        login.show();
    }                                              

    private void openFormTableUserActionPerformed(java.awt.event.ActionEvent evt) {                                                  
        // TODO add your handling code here:
        if (AppLib.formManager.isFormOpened("UserCrud")) {
            JOptionPane.showMessageDialog(this, "Form Already Opened");
            JInternalFrame jif = AppLib.formManager.getOpenedForm();
            jif.setVisible(true);
        } else {
            UserCrud tableUser = new UserCrud();
            DesktopPane.add(tableUser);
            tableUser.setVisible(true);
            AppLib.formManager.registerForm(tableUser);
        }
    }                                                 

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        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(MainFormDemo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MainFormDemo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MainFormDemo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MainFormDemo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new MainFormDemo().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JDesktopPane DesktopPane;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem menuClosedForm;
    private javax.swing.JMenu menuHelp;
    private javax.swing.JMenuItem menuOpenedForm;
    private javax.swing.JMenuItem openFormTableUser;
    // End of variables declaration                   
}

Error :

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at oopw4.FormManager.searchForm(FormManager.java:51)
    at oopw4.FormManager.unregisterForm(FormManager.java:40)
    at oopw4.UserCrud.closeButtonActionPerformed(UserCrud.java:263)
    at oopw4.UserCrud.access$500(UserCrud.java:24)
    at oopw4.UserCrud$6.actionPerformed(UserCrud.java:187)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6533)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
    at java.awt.Component.processEvent(Component.java:6298)
    at java.awt.Container.processEvent(Container.java:2236)
    at java.awt.Component.dispatchEventImpl(Component.java:4889)
    at java.awt.Container.dispatchEventImpl(Container.java:2294)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
    at java.awt.Container.dispatchEventImpl(Container.java:2280)
    at java.awt.Window.dispatchEventImpl(Window.java:2746)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
    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.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.awt.EventQueue$4.run(EventQueue.java:731)
    at java.awt.EventQueue$4.run(EventQueue.java:729)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
    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)

Can someone help me ?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433

1 Answers1

0

Remember Swing UI operates on its own thread. And components in Swing are not designed to be thread safe. So, welcome to Swing. You will likely find that your search function is breaking here frameList.get(idx).getName().equals(namaForm) ... because the frame isn't created yet. Read up on "InvokeLater" and use that to respond to UX events. That makes sure that the Swing thread that does window management is all caught up before you try and do anything with components.

Micromuncher
  • 903
  • 7
  • 19