-2

The below code "Boxlayout example complies well but throws me the below exception at runtime:

Exception in thread "main" java.lang.NullPointerException

at java.awt.Container.addImpl(Container.java: 1027)
at java.awt.Container.add(Container.java: 935)
at javax.swing.JFrame.addImpl(JFrame.java: 545)
at java.awt.Container.add(Container.java: 352)
at BoxExample.launchFrame(BoxExample.java:26)
at BoxExample.main(BoxExample.java:40)

Please help me out to sort out this as it wasted nearly 1 day of my life..

//Boxlayout example

import java.awt.*;

import javax.swing.*;

public class BoxExample
{

public JFrame f;

public JButton b1, b2,b3,b4,b5;

public BoxExample()

{

f=new JFrame("Box example");

f.setTitle("Box Layout Example");

f.setSize(150, 150);

JButton b1=new JButton("Button 1");

JButton b2=new JButton("Button 2");

JButton b3=new JButton("Button 3");

JButton b4=new JButton("Button 4");

JButton b5=new JButton("Button 5");

}


public void launchFrame()

{

System.out.println("inside lf");

f.setLayout(new BoxLayout(f,BoxLayout.Y_AXIS));

System.out.println("after layset");

f.add(b1);

f.add(b2);

f.add(b3);

f.add(b4);

f.add(b5);

f.pack();

f.setVisible(true);

}


public static void main(String args[])

{

BoxExample guiWindow=new BoxExample();

System.out.println("main");

guiWindow.launchFrame();

}

}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373

1 Answers1

1

You are shadowing some variables, mainly the JButton variables b1, b2, b3, b4, and b5, by declaring them in the class, and then re-declaring and initializing them in the constructor. The newly declared variables in the constructor are not the same ones that are declared in the class, and so the class variables will remain null.

Solution: don't re-declare the variables in the constructor. So instead of this:

class Foo {
  private Bar bar;

  public Foo() {
    Bar bar = new Bar(); // bar is re-declared here!
  }
}

do this:

class Foo {
  private Bar bar;

  public Foo() {
    bar = new Bar(); // notice the difference!
  }
}

Also, whenever you have a NullPointerException (NPE) look carefully at the line that throws the exception, here line 26 of the BoxExample class:

at BoxExample.launchFrame(BoxExample.java:26)

You'll find on that line that one of the variables is null. If you find out which variable, you can often backtrack through your code and see why it is null.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Thanks for your input but if I remove all those re-declarations (removing JButton keywords in constructor) am getting the runtime error of java.awt.AWTError: BoxLayout can't be shared at ... checkContainer... invalidateLayout... addLayoutComponent... addImpl... and all those stuff.. could you please correct then?? – DKB - user1543439 Jul 22 '12 at 03:00
  • @user1543439: hm, that doesn't make sense. Consider editing your post above and adding below your current code, your new attempt that causes the error. If you do this, please post only well-formatted code. Your current code is all left justified making it very hard for those who are unfamiliar with it (us!) to be able to read and understand it. – Hovercraft Full Of Eels Jul 22 '12 at 03:04
  • oh many thanks dude.. I have edited this and made a new post titled "Boxlayout can't be shared AWTError… recorrecting my previous quest which had Nullpointerexception am getting this error.. Please help me out" for this.. am getting the answers.. thanks for your feedback!! – DKB - user1543439 Jul 22 '12 at 04:03
  • @user1543439: why did you start a new thread for this? If you consider this current problem solved, OK, but if not then you're unnecessarily dividing the discussion of the problem which is not fair to those of us who volunteer to help here. – Hovercraft Full Of Eels Jul 22 '12 at 04:15