0

Im a very much a beginner programmer and i stumbled across this error code, when i try to click on my second, third and fourth MenuItem. The program runs, i can click the first MenuItem and the action is performed (leading me to a second class view). But when i try to click on the rest of the MenuItems or the 2 buttons with same action code (CLASS.show(); + dispose ();) i get this error:

  • Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: Cannot invoke "numerologi.views.Klientliste.show()" because "this.this$0.KLside" is null at numerologi.views.Program$3.actionPerformed(Program.java:120).......

So i understand that/where the error is at - on line 120 (and at the other MenuItem action lines) - but i dont know how to fix it? Please help!

I have now tried this and the error code stopped, but the action does not occurs? :

menuKliste.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    if (e == null) {
    KLside.show();
    dispose(); } }
});

My code:

package numerologi.views;

import java.awt.*; 
import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.*;

public class Program extends JFrame {

    private JPanel contentPane, panel;
    private JMenuBar menuBar;
    private JMenu ForsideMenu;
    private JMenuItem menuopret, menuKliste, menuNF, menuNliste;
    private JLabel LabelVelkommen, ProgramMadeby;
    
    private Opret ONside; //classes/sites
    private Klientliste KLside;
    private NF NFside;
    private Navneliste NLside;
    
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {     
Program frame = new Program();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();}
}
});
}
    
public Program() {
    
    initComponents();
    createEvents(); } 

private void initComponents() { //This method contains all codes for creating and initializing components.  
    
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 1000, 600);
    
    contentPane = new JPanel();
    contentPane.setBackground(SystemColor.menu);
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);
    
    ProgramMadeby = new JLabel("Program made by Ellinoir");
    ProgramMadeby.setFont(new Font("Tahoma", Font.PLAIN, 10));
    ProgramMadeby.setBounds(10, 540, 192, 13);
    contentPane.add(ProgramMadeby);
    ONside = new Opret();
    
    menuBar = new JMenuBar();
    menuBar.setBounds(0, 0, 986, 22);
    contentPane.add(menuBar);
    
    ForsideMenu = new JMenu("Menu");
    ForsideMenu.setHorizontalAlignment(SwingConstants.CENTER);
    menuBar.add(ForsideMenu);
    
    menuopret = new JMenuItem("Opret ny klient");
    menuopret.setBackground(SystemColor.inactiveCaption);
    
    ForsideMenu.add(menuopret);
    
    menuKliste = new JMenuItem("Klientliste");
    menuKliste.setBackground(SystemColor.inactiveCaption);
    ForsideMenu.add(menuKliste);
    
    ForsideMenu.addSeparator();
    
    menuNF = new JMenuItem("Navne forandring");
    menuNF.setBackground(SystemColor.inactiveCaption);
    ForsideMenu.add(menuNF);
    
    menuNliste = new JMenuItem("Navneliste");
    menuNliste.setBackground(SystemColor.inactiveCaption);
    ForsideMenu.add(menuNliste);
    
    LabelVelkommen = new JLabel("Velkommen til numerologiens verden!");
    LabelVelkommen.setFont(new Font("Tahoma", Font.BOLD, 27));
    LabelVelkommen.setBackground(SystemColor.inactiveCaption);
    LabelVelkommen.setBounds(230, 80, 543, 65);
    contentPane.add(LabelVelkommen);
    
    panel = new JPanel();
    panel.setBackground(SystemColor.inactiveCaption);
    panel.setBounds(130, 150, 750, 300);
    contentPane.add(panel);
    setIconImage(Toolkit.getDefaultToolkit().getImage(Program.class.getResource("/numerologi/resources/Diamond_512.png")));
    setTitle("Numerologi program");
}

private void createEvents() { // This method contains all codes for creating events.    
    
    menuopret.addActionListener(new ActionListener() { //this one works
    public void actionPerformed(ActionEvent e) {
    ONside.show();
    dispose(); }
});
    menuKliste.addActionListener(new ActionListener() { //these don't
    public void actionPerformed(ActionEvent e) {
    KLside.show();
    dispose(); }
});
    menuNF.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    NFside.show();
    dispose(); }
});
    menuNliste.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    NLside.show();
    dispose(); }
});
    }
                    }

All error code:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: Cannot invoke "numerologi.views.Klientliste.show()" because "this.this$0.KLside" is null
    at numerologi.views.Program$3.actionPerformed(Program.java:120)
    at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1967)
    at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2308)
    at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405)
    at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262)
    at java.desktop/javax.swing.AbstractButton.doClick(AbstractButton.java:369)
    at java.desktop/javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:1012)
    at java.desktop/javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:1056)
    at java.desktop/java.awt.Component.processMouseEvent(Component.java:6614)
    at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3342)
    at java.desktop/java.awt.Component.processEvent(Component.java:6379)
    at java.desktop/java.awt.Container.processEvent(Container.java:2263)
    at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:4990)
    at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2321)
    at java.desktop/java.awt.Component.dispatchEvent(Component.java:4822)
    at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4919)
    at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4548)
    at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4489)
    at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2307)
    at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2769)
    at java.desktop/java.awt.Component.dispatchEvent(Component.java:4822)
    at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:772)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:95)
    at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:745)
    at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:743)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
    at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:742)
    at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
Ellinoir
  • 11
  • 4

0 Answers0