0

I created a Java Application and get this Exception:

Exception in thread "main" java.lang.NullPointerException
    at java.awt.Container.addImpl(Unknown Source)
    at java.awt.Container.add(Unknown Source)
    at javax.swing.JFrame.addImpl(Unknown Source)
    at java.awt.Container.add(Unknown Source)
    at Executer.<init>(Executer.java:21)
    at Executer.main(Executer.java:14 

Here is the code:

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

public class Executer {

private JLabel lblCommand;
private JTextField txtEnter;
private JButton btNext, btPrevious;
private JPanel panel;

public static void main(String[] args) {
    new Executer();
}
public Executer() {
    JFrame frame = new JFrame("Execute Script");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(900,400);
    frame.setVisible(true);
    frame.add(panel); 
    frame.setVisible(true);
    MyPanel();
    Text();
    Buttons();
    Fields();
}
public void MyPanel() {
    panel = new JPanel();
    panel.setLayout(null);
}
public void Text(){
    lblCommand = new JLabel("Enter Here");
    lblCommand.setBounds(135, 50, 150, 20);
    Font styleOne = new Font("Arial", Font.BOLD, 13);
    lblCommand.setFont(styleOne);
    panel.add(lblCommand);
}

public void Fields () {
    txtEnter = new JTextField();
    txtEnter.setBounds(210, 50, 150, 20);
    panel.add(txtEnter);
}
public void Buttons() {
    btNext = new JButton ("Next");
    btNext.setBounds(380,325,100,20);
    panel.add(btNext);

    btPrevious = new JButton ("Previous");
    btPrevious.setBounds(260,325,100,20);
    panel.add(btPrevious);
}}

What is a NullPointerException? How would I find out?

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
Troll Junior
  • 19
  • 1
  • 1
  • 3
  • 1
    I don't know much about/care much for Swing applications, but I think that you have to instantiate `panel` in some way. You pass it in as `null`. – Makoto Jul 28 '13 at 22:38
  • To figure out the cause of a NPE, you should look at the stacktrace to find the first line that mentions a class and method from your own code. Starting from there work backwards to the last line that mentions a class and method from your code. This will give you a starting place to track down the cause of the problem. – Code-Apprentice Jul 28 '13 at 22:45
  • 1
    Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). – Andrew Thompson Jul 29 '13 at 02:03
  • @EricLeschinski No, I don't think so. While knowledge of that may have helped, the problem here was simply that a line of code was placed in the wrong spot. – Chris Bode Jul 31 '13 at 03:52

4 Answers4

6

You need to instantiate panel before adding it. If you use panel before calling MyPanel(), panel is still null, hence the NullPointerException.

While you're here, give this a glance. http://geosoft.no/development/javastyle.html

Method names in Java should be mixed case starting with a lower case letter, e.g. myPanel() instead of MyPanel(). To most of us, MyPanel() looks like a constructor at first glance because you improperly styled it.

Additionally, MyPanel, Text, Fields, and Buttons should all be private methods, as it would be improper for an external class to call them.

Chris Bode
  • 1,265
  • 7
  • 16
1

Problem in this line frame.add(panel); Panel is not initialized at that point, move this line MyPanel(); before adding to initialize it.

nkukhar
  • 1,975
  • 2
  • 18
  • 37
1

As others have said, you need to create the JPanel before you try to add it to the JFrame. In fact, you should typically create all components inside the JPanel as well. I suggest that you move the calls to

Text();
Buttons();
Fields();

from the Executer constructor to the MyPanel() method and call MyPanel() before calling frame.add(panel);.

In addition, you do not need to call frame.setVisible(true); twice. Also, you should use a LayoutManager rather than calling panel.setLayout(null);. See the Oracle tutorial for Using Layout Managers.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
0

Yes you are adding panel to the frame before creating object of JPanel. Anyway change your constructor with this:

public Executer() {
        JFrame frame = new JFrame("Execute Script");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(900, 400);

        MyPanel();
        Text();
        Buttons();
        Fields();

        frame.add(panel);
        frame.setVisible(true);

    }

thanks.

Golam Rabbi
  • 65
  • 2
  • 7