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

class Calculator{
	JFrame frame;
	JPanel panel1, panel2;
	JTextField textField;
	JButton button[];
	Container contentPane;
	
	void launchFrame(){
		frame = new JFrame("My Calculator");
		panel1 = new JPanel();
		panel2 = new JPanel(new GridLayout(4,4));
		textField = new JTextField(10);
		button = new JButton[17];
		for (int i=0; i <10; i++)
			button[i] = new JButton(i + "");
		
		button[0] = new JButton("0");
		button[1] = new JButton("1");
		button[2] = new JButton("2");
		button[3] = new JButton("3");
		button[4] = new JButton("4");
		button[5] = new JButton("5");
		button[6] = new JButton("6");
		button[7] = new JButton("7");
		button[8] = new JButton("8");
		button[9] = new JButton("9");
		button[10] = new JButton("+");
		button[11] = new JButton("-");
		button[12] = new JButton("*");
		button[13] = new JButton("/");
		button[14] = new JButton("=");
		button[15] = new JButton("CLR");
		button[16] = new JButton(".");
		
		panel1.add(textField);
		panel1.add(button[15]);
		
		panel2.add(button[7]);
		panel2.add(button[8]);
		panel2.add(button[9]);
		panel2.add(button[10]);
		panel2.add(button[4]);
		panel2.add(button[5]);
		panel2.add(button[6]);
		panel2.add(button[11]);
		panel2.add(button[1]);
		panel2.add(button[2]);
		panel2.add(button[3]);
		panel2.add(button[12]);
		panel2.add(button[0]);
		panel2.add(button[16]);
		panel2.add(button[14]);
		panel2.add(button[13]);
		
		contentPane.add(panel1, BorderLayout.NORTH);
		contentPane.add(panel2, BorderLayout.CENTER);
		frame.pack();
		frame.setVisible(true);
		}
		
		public static void main(String args[]){
			Calculator calc = new Calculator ();
			calc.launchFrame ();
		}
}

Hello everyone. Could someone help me in fixing these codes. JCreator compiles the codes with no problem, but after I run it an error shows.

Configuration: <Default>--------------------
Exception in thread "main" java.lang.NullPointerException
at Calculator.launchFrame(Calculator.java:58)
at Calculator.main(Calculator.java:66)

Process completed.

Please help me with this. Thanks!! ^_^

You never assigned a value to contentPane.

EDIT: got it..

contentPane = frame.getContentPane();
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.