How to solve this exception ????
Hello every body I'm a new member in this very good website
I have just started to write java calculator and i face this run time exception
Exception in thread "main" java.lang.NullPointerException
at Calculater.<init>(Calculater.java:34)
at Calculater.main(Calculater.java:81)
and here is my code
Code :
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JTextArea;
import java.awt.FlowLayout;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Color;
public class Calculater extends JFrame
{
public static final int WIDTH = 450;
public static final int HIGHT = 250;
public Calculater()
{
super("TextArea Calculater");
setSize(WIDTH,HIGHT);
setLocation(200, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setResizable(false);
setLayout(new BorderLayout(5, 5));
//Three Panel to orgenize the frame
//leftPanel for numbers and operations
JPanel leftPanel = new JPanel();
leftPanel.setLayout(new GridLayout(4, 4, 5, 5));
//rightPanel for the text area and the rest clear buttons
JPanel rightPanel = new JPanel();
rightPanel.setLayout(new BorderLayout(5, 5));
//textAreaPanel for textArea
JPanel textAreaPanel = new JPanel();
textAreaPanel.setLayout(new GridLayout(1, 1, 5, 5));
//clearsPanel for the all kind of clear buttons
JPanel clearsPanel = new JPanel();
clearsPanel.setLayout(new GridLayout(1, 3, 5, 5));
//decleare buttons and add them to leftPanel
JButton b1 = new JButton("7");
leftPanel.add(b1);
JButton b2 = new JButton("8");
leftPanel.add(b2);
JButton b3 = new JButton("9");
leftPanel.add(b3);
JButton b4 = new JButton("/");
leftPanel.add(b4);
JButton b5 = new JButton("4");
leftPanel.add(b5);
JButton b6 = new JButton("5");
leftPanel.add(b6);
JButton b7 = new JButton("6");
leftPanel.add(b7);
JButton b8 = new JButton("x");
leftPanel.add(b8);
JButton b9 = new JButton("1");
leftPanel.add(b9);
JButton b10 = new JButton("2");
leftPanel.add(b10);
JButton b11 = new JButton("3");
leftPanel.add(b11);
JButton b12 = new JButton("-");
leftPanel.add(b12);
JButton b13 = new JButton("0");
leftPanel.add(b13);
JButton b14 = new JButton(".");
leftPanel.add(b14);
JButton b15 = new JButton("=");
leftPanel.add(b15);
JButton b16 = new JButton("+");
leftPanel.add(b16);
//add leftPanel to the frame
add(leftPanel, BorderLayout.CENTER);
//decleare clears buttons and add them to clearPanel
JButton b17 = new JButton("C");
clearsPanel.add(b17);
JButton b18 = new JButton("CE");
clearsPanel.add(b18);
JButton b19 = new JButton("Clear");
clearsPanel.add(b19);
//add clearPanel to rightPanel
rightPanel.add(clearsPanel, BorderLayout.SOUTH);
//declear text area and add it to textAreaPanel
JTextArea myArea = new JTextArea();
textAreaPanel.add(myArea);
//add textAreaPanel to rightPanel
rightPanel.add(textAreaPanel, BorderLayout.CENTER);
//add rightPanel to the frame
add(rightPanel, BorderLayout.EAST);
}
public static void main(String[] args)
{
Calculater myCalc = new Calculater();
}
}
could any one help :)
System.out.println("Thank you.");
Re: How to solve this exception ????
When posting code, please use the highlight tags. Unformatted code is very hard to read.
What line is that Exception occurring on? What are you trying to dereference? What is its value?
1 Attachment(s)
Re: How to solve this exception ????
thank you KevinWorkman
the exception in this line
content.setLayout(new BorderLayout(5, 5));
since i want to put content JPanel in to JFrame
and i want to pot it in the right side of the fame this is the calculator i want to do
Attachment 1133
Re: How to solve this exception ????
When posting code, please use the highlight tags. You can edit your original post to add them.
If that's the line, then it looks like content must be null. When do you declare and instantiate it?
Re: How to solve this exception ????
what do you mean by please use the highlight tags. ????
Re: How to solve this exception ????
There's a link in my signature explaining it, or you could look at the many other posts here that use them. But the point is, when posting code, you should surround it with highlight tags to preserve formatting. Unformatted code makes programmers' eyes bleed.
Code java:
System.out.println("see how pretty this is?");