2

I am working on my first java GUI and already get some good help here, thank you all!

I have a primary class from which i want to control and handle my additional programs like tcp connections etc. So i created a JFrame, appended a JMenu and the main area should change its content depending on what JMenuItem is klicked, thats a simple explain. The content i want to load in this main area are JPanels which are running my additional applications. when i click ex. the menuitem "Neu" i want to load a specific JPanel in the main area depending on the clicked JMenuItem. how i will bring this to work?

Here is the error

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
        at MyFrame$1.actionPerformed(MyFrame.java:48)
        at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
        at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
        at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
        at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
        at javax.swing.AbstractButton.doClick(Unknown Source)
        at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)
        at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(Unknown
Source)
        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$400(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 Sour
ce)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Sour
ce)
        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 Sour
ce)
        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)

Here is the Code in one file

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

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

public class MyFrame extends JFrame { 

private static Container contentContainer;

    public static void main(String[] args) {

        new MyFrame();

    }

    public MyFrame() {

        setTitle("MyFrame");
        setSize(300, 200);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setJMenuBar(createMenu());

        // I GUESS HERE ARE MY TROUBLES //

        MyPanel panel = makePanel(new String("Test oO"));        
        contentContainer = this.getContentPane();

        // I GUESS HERE ARE MY TROUBLES //

        setVisible(true);

    }

    public static JMenuBar createMenu() {

        JMenuBar menuBar = new JMenuBar();
        JMenu menu = new JMenu("Menu");
        JMenuItem menuItem = new JMenuItem("Neu");
        menuItem.setMnemonic(KeyEvent.VK_E);
        menuItem.setToolTipText("Neu");
        menuItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent event) {
                MyPanel dynamicPanel = makePanel(new String("Test haha"));
                contentContainer.add(dynamicPanel);                
            }
        });
        menu.add(menuItem);
        menuBar.add(menu);
        return menuBar;

    }

    public static JDialog makeDialog(String title, String content) {

        JDialog meinJDialog = new JDialog();
        meinJDialog.setVisible(true);
        return meinJDialog;   

    }  

    public static MyPanel makePanel(String config) {

        MyPanel panel = new MyPanel(config);
        return panel;

    }  

}

class MyPanel extends JPanel {

    public MyPanel(String config) {

        setBackground(new Color(77,81,84));
        JButton testButton = new JButton(config);
        add(testButton);

    }

}

Thanks a lot and sorry for my poor language and coding skills :P

muni
  • 119
  • 12
  • 1) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). Demote the 'external class' to default access so it can be added to the same souce file as the other class. 2) See [What is a stack trace, and how can I use it to debug my application errors?](http://stackoverflow.com/q/3988788/418556) & [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/q/218384/418556) – Andrew Thompson Feb 05 '15 at 02:18
  • BTW - `// wir benötigen nur..` Comments that are not in English are noise on an English oriented site. Trim them out. – Andrew Thompson Feb 05 '15 at 02:19
  • Thanks a lot. german comments are unnecessary, theyre just for me and says (creating this or that) as i can see theres nothing important in any comments.. i try to do this MCVE stuff but i will need a while, sry – muni Feb 05 '15 at 02:19
  • *"comments are unnecessary, theyre just for me.."* And noise for most other people. **Leave them out!** – Andrew Thompson Feb 05 '15 at 02:25
  • oh ok sorry i thought you mean "translate it" – muni Feb 05 '15 at 02:28
  • `private static Container contentContainer;` – MadProgrammer Feb 05 '15 at 02:57
  • changed it to private Container contentContainer but without any effect. do you have mor suggestions? – muni Feb 05 '15 at 03:07

1 Answers1

4

Change:

Container contentContainer = this.getContentPane(); // Local variable!

To:

contentContainer = this.getContentPane(); // Class attribute!
Andrew Thompson
  • 163,965
  • 36
  • 203
  • 405
  • 2
    @muni Also take a look at [How to Use CardLayout](http://docs.oracle.com/javase/tutorial/uiswing/layout/card.html) – MadProgrammer Feb 05 '15 at 02:32
  • So, it seems the `NullPointerException` is solved. Please don't mistake SO for a help desk, it is a Q&A Site, where each question should have a dedicated thread. (Rides off into the sunset, whistling 'Happy Trails'..) – Andrew Thompson Feb 05 '15 at 02:32
  • 2
    @muni And `static` is a bad idea, this will hurt you in the long run – MadProgrammer Feb 05 '15 at 02:33
  • *"in which place?"* ..What? Who are you replying to? When in doubt, it is a good idea to add more words, rather than less. – Andrew Thompson Feb 05 '15 at 02:40
  • @Andrew Thompson: sorry i answered MadProgrammers comment about static.. please see my updated code. MadProgrammer i have a few static methods and variables, which one do you mean exactly? – muni Feb 05 '15 at 02:50
  • @MadProgrammer - see updated comment. OP - I've stopped thinking about this since the problem (the only one I am prepared to deal with in ***this*** question) is solved. – Andrew Thompson Feb 05 '15 at 02:56
  • Sorry dude but i know how to handle exceptions and now i also have read whats a nullpointerexception. the actual problem is i have a nullpointerexception but even if i initiate the object without Classname i didnt get a result, only the error doesnt appear anymore. how can i load the JPanel into my main JFrame? – muni Feb 05 '15 at 02:58
  • *"Sorry dude but i know how to handle exceptions.."* The existence of the question would suggest otherwise. And don't call me 'dude'. – Andrew Thompson Feb 05 '15 at 03:06
  • Sorry Mr Thompson. Ok, maybe i just think i can handle them the right way but now i know its a nullPointerException and i tried initiating the object otherwise but it still not work. can you please help me, Mr Thompson? – muni Feb 05 '15 at 03:11
  • *"..but it still not work."* But that is a separate problem, which should be asked and answered on a separate thread. I thought I'd explained that earlier.. – Andrew Thompson Feb 05 '15 at 03:14
  • i realised that the task is executing and the JPanel is added succesfully when i click the JMenuItem but it will not take effect until i resize the JFrame – muni Feb 05 '15 at 03:25
  • @muni [Accept this answer](http://meta.stackexchange.com/a/5235/236612) now. It solved your `NullPointerException`. You can ask another question later if necessary. – gknicker Feb 05 '15 at 04:51
  • sorry i forgot that. of course it solved my problem. i accepted and voted up too :D – muni Feb 05 '15 at 04:53