0

I got null pointer exception during content adding in JFrame in Swing.

    public static void createAndShowGUI() {
    //Create and set up the window.
    frame = new JFrame("Data Entry Application");
    //Set up the content pane.
    // frame = new JFrame();
    addComponentsToPane(frame.getContentPane());  // null pointer exception in this line
     }

 public static void addComponentsToPane(Container pane) {
    if (RIGHT_TO_LEFT) {
        pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
    }

    Border blackline, raisedetched = null, loweredetched,
            raisedbevel = null, loweredbevel, empty, orangeline, redline, greenline, blueline;
    blackline = BorderFactory.createLineBorder(Color.black);
    orangeline = BorderFactory.createLineBorder(Color.ORANGE);
    redline = BorderFactory.createLineBorder(Color.RED);
    greenline = BorderFactory.createLineBorder(Color.GREEN);
    blueline = BorderFactory.createLineBorder(Color.blue);

    JButton button;
    pane.setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    if (shouldFill) {
        //natural height, maximum width
        c.fill = GridBagConstraints.HORIZONTAL;
    }

    /* Header Section start*/
    /* Username and designation starts */
    JPanel p_username = new JPanel();
    p_username.setMinimumSize(new Dimension(140, 30));
    l_username = new JLabel(Login.login_username);
    l_designation = new JLabel("Data Entry User");

    JPanel t9 = new JPanel(new GridLayout(0, 1));
    t9.add(l_username);
    t9.add(l_designation);
    t9.setPreferredSize(new Dimension(140, 30));
    p_username.add(t9);
   c.fill = GridBagConstraints.NORTH;
    c.gridx = 0;
    c.gridy = 0;
    c.ipady = 25;
    c.insets = new Insets(0, 10, 0, 0);
    pane.add(p_username, c);
    /* Username and designation end */
  }

Suggest some idea.

Dhinakar
  • 4,061
  • 6
  • 36
  • 68
  • 4
    Where is your `addComponentsToPane` method code ? – Suresh Atta Nov 03 '14 at 07:00
  • Null pointer means that you are trying to use something that is null. WE can't know what that is without seeing the addComponentsToPane method. – DonyorM Nov 03 '14 at 07:17
  • 1) 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) 2) Always copy/paste error and exception output! 3) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example). – Andrew Thompson Nov 03 '14 at 07:20

1 Answers1

1

I mocked your addComponentsToPane() method to show you what the problem is:

private static void addComponentsToPane(Container container)
{
    System.out.println("Is container null? " + container == null);
    JPanel panel = null;
    container.add(panel);
}

Calling this method results in the following output:

false
Exception in thread "main" java.lang.NullPointerException
    at java.awt.Container.addImpl(Container.java:1091)
    at java.awt.Container.add(Container.java:415)
    at FrameDemo.addComponentsToPane(FrameDemo.java:33)
    at FrameDemo.createAndShowGUI(FrameDemo.java:25)
    at FrameDemo.main(FrameDemo.java:14)

If you read the Javadoc, the getContentPane() method does not return null as you can see from the resulting output. My second line declares a JPanel but does not instantiates the object, which I then try to add to the contents pane. This is causing the NullPointerException in my case.

My conclusion: You are adding a component to the container that has not been properly instantiated. In fact, if you read the Javadoc for the Container.add(comp) method, it states that this method throws a NullPointerException if comp is null. Check all of the components you are trying to add to it and you can figure out the rest.

hfontanez
  • 5,774
  • 2
  • 25
  • 37