Please help me to sort out this java.awt.BoxLayout can't be shared or NullPointerException
<p>The below code "Boxlayout example complies well but throws me the below exception at runtime:</p>

<pre><code>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)
</code></pre>

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

<pre><code>//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();

}

}
</code></pre>
