下面是一个“计算器”的程序,不过因为遇到困难,所以还没有编写完毕!这个程序在编译的时候能够通过,但当我点击“=”的时候,就会出现下列报错:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Counter$1.actionPerformed(ceshi.java:85)
at java.awt.Button.processActionEvent(Unknown Source)
at java.awt.Button.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
请问一下,我的程序哪里出错了?请大家赐教!!
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Counter extends JFrame
{
Double x,y;
boolean z;
//实例化2个显示屏
JTextField Jtextfield1=new JTextField(20);
TextField textfield2=new TextField(8);
//实例化5个面板
JPanel Jpanel1=new JPanel();
JPanel Jpanel2=new JPanel();
JPanel Jpanel3=new JPanel();
JPanel Jpanel4=new JPanel();
JPanel Jpanel5=new JPanel();
//实例化一个空字符缓冲区
StringBuffer str=new StringBuffer();
//实例化计算器上的按钮
Button[][] b=new Button[][]{{new Button("MC"),
new Button("MR"),
new Button("MS"),
new Button("M+")},
{new Button("Backspace"),
new Button("CE"),
new Button("C")},
{new Button("7"),
new Button("8"),
new Button("9"),
new Button("/"),
new Button("sqrt"),
new Button("4"),
new Button("5"),
new Button("6"),
new Button("*"),
new Button("%"),
new Button("1"),
new Button("2"),
new Button("3"),
new Button("-"),
new Button("1/x"),
new Button("0"),
new Button("+/-"),
new Button("."),
new Button("+"),
new Button("=")}
};
ActionListener action=new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b[1][0])
{
if(str.length()>1) //"Backspace"
{
str.setLength((str.length())-1);
Jtextfield1.setText(str.toString());
}
else
Jtextfield1.setText("0.");
}
else if(e.getSource()==b[1][1]) //"CE"
{
Jtextfield1.setText("0.");
str.setLength(0);
}
else if(e.getSource()==b[1][2]) //"C"
{
Jtextfield1.setText("0.");
str.setLength(0);
}
else if(e.getSource()==b[2][18]) //"+"
{
str.setLength(0);
z=false;
}
else if(e.getSource()==b[2][19]) //"="
{
Jtextfield1.setText(Double.toString(x+y));
z=true;
}
else if(str.length()<=10) //"数字键",“10”表示有限位数为10位
{
Jtextfield1.setText(str.append(e.getActionCommand()).toString());
if(z=true)
x=Double.parseDouble(str.toString());
else
y=Double.parseDouble(str.toString());
}
}
};
public Counter(String str)
{
super(str);
Jtextfield1.setBackground(Color.white);
Jtextfield1.setEditable(false);
Jtextfield1.setText("0.");
setLayout(null);
setJPanel1();
setJPanel2();
setJPanel3();
setJPanel4();
setJPanel5();
add(Jpanel1);
add(Jpanel2);
add(Jpanel3);
add(Jpanel4);
add(Jpanel5);
for(int i=0;i<b.length;i++)
{
for(int j=0;j<b[i].length;j++)
{
b[i][j].addActionListener(action);
}
}
}
public void setJPanel1()
{
GridLayout GLayout1=new GridLayout(1,1);
Jpanel1.setLayout(GLayout1);
Jpanel1.add(Jtextfield1);
Jtextfield1.setHorizontalAlignment(JTextField.RIGHT );
Jpanel1.setBounds(10,10,252,22);
}
public void setJPanel2()
{
GridLayout GLayout2=new GridLayout(5,1,10,6);
Jpanel2.setLayout(GLayout2);
for(int i=0;i<4;i++)
b[0][i].setForeground(Color.red);
Jpanel2.add(b[0][0]);
Jpanel2.add(b[0][1]);
Jpanel2.add(b[0][2]);
Jpanel2.add(b[0][3]);
Jpanel2.setBounds(10,74,40,165);
}
public void setJPanel3()
{
GridLayout GLayout3=new GridLayout(1,3,5,0);
Jpanel3.setLayout(GLayout3);
for(int i=0;i<3;i++)
b[1][i].setForeground(Color.red);
Jpanel3.add(b[1][0]);
Jpanel3.add(b[1][1]);
Jpanel3.add(b[1][2]);
Jpanel3.setBounds(57,39,205,25);
}
public void setJPanel4()
{
GridLayout GLayout4=new GridLayout(4,5,5,6);
Jpanel4.setLayout(GLayout4);
for(int i=0;i<b[2].length;i++)
{
Jpanel4.add(b[2][i]);
b[2][i].setForeground(Color.blue);
}
b[2][4].setForeground(Color.red);
b[2][9].setForeground(Color.red);
b[2][14].setForeground(Color.red);
Jpanel4.setBounds(57,74,205,132);
}
public void setJPanel5()
{
GridLayout GLayout5=new GridLayout(1,1);
Jpanel5.setLayout(GLayout5);
textfield2.setEditable(false);
Jpanel5.add(textfield2);
Jpanel5.setBounds(15,40,30,25);
}
public static void main(String[] args)
{
Counter counter=new Counter("计算器");
counter.setSize(280,245);
counter.setLocation(300,250);
counter.setVisible(true);
counter.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
[此贴子已经被作者于2007-11-19 1:19:04编辑过]
----------------解决方案--------------------------------------------------------
java.lang.NullPointerException
java代码中的错误。
主要有以下几种原因:
1、使用了未初始化的变量(虽然已经声明)
2、使用了未初始化的对象(虽然已经声明)
3,使用了关键字或已存在的类名作变量对象方法或类名。
----------------解决方案--------------------------------------------------------