The Below code does not fail to compile but however at runtime it says java.lang.NullPointerException at Line at line number 20 and 41. Also i am little bit curious to know what is Null Pointer Exception, what happens at runtime ?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Tool
{
private JToolBar toolbar1;
private JToolBar toolbar2;
private JPanel panel;
public Tool()
{
JFrame frame= new JFrame();
panel = new JPanel();
panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));
JButton one = new JButton("one");
JButton two = new JButton("two");
JButton three = new JButton("three");
JButton four = new JButton("four");
toolbar1 = new JToolBar();
toolbar2 = new JToolBar();
toolbar1.add(one);
toolbar1.add(two);
toolbar2.add(three);
toolbar2.add(four);
toolbar1.setAlignmentX(0);
toolbar2.setAlignmentX(0);
panel.add(toolbar1);
panel.add(toolbar2);
frame.add(panel,BorderLayout.NORTH);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.setSize(400,300);
frame.setTitle("ZOOP");
frame.setVisible(true);
}
public static void main (String args[])
{
Tool zoop = new Tool();
}
}
You are passing a
nullon the following methods….It’s because the following haven’t been initialized.
Definition of NullPointerException
Initialize it