我正试图通过Fedora 18中的appletviewer在java applet中运行此代码。但是我收到此错误
java.lang.NullPointerException
at main.init(main.java:42)
at sun.applet.AppletPanel.run(AppletPanel.java:436)
at java.lang.Thread.run(Thread.java:722)
根据我的代码,我的代码中的第42行是bMul.setBounds(100, 280, 50, 50);
,但这并没有错,因为据我所知。在Google上搜索后,我发现了
NullPointerException是JVM抛出的运行时异常 您的应用程序代码,其他引用的API或中间件 (Weblogic,WAS,JBoss ......)遇到以下情况:
- 尝试调用null对象的实例方法
- 试图访问或修改空对象的特定字段
- 尝试获取此类空对象的长度作为数组
我努力尝试但未能成功。请帮我。这里我提供main.java文件的代码。
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class main extends Applet implements ActionListener {
Button b0, b1, b2, b3, b4, b5, b6, b7, b8, b9;
Button bAdd, bSub, bMul, bDiv, bEqu, bCls, bDec, bSqrt, bSin, bCos, bTan;
TextField tf1;
int add, sub, mul, div, temp = 0;
double sqr, s, c, ta;
String t = "";
public void init() {
setLayout(null);
setFont(new Font("cantarell", Font.BOLD, 12));
b0 = new Button("0"); b1 = new Button("1");
b2 = new Button("2"); b3 = new Button("3");
b4 = new Button("4"); b5 = new Button("5");
b6 = new Button("6"); b7 = new Button("7");
b8 = new Button("8"); b9 = new Button("9");
bAdd = new Button("+"); bSub = new Button("-");
bSub = new Button("*"); bDiv = new Button("/");
bEqu = new Button("="); bSqrt = new Button("sqrt");
bSin = new Button("sin"); bCos = new Button("cos");
bTan = new Button("tan"); bCls = new Button("cls");
tf1 = new TextField("0");
tf1.setEditable(false);
tf1.setColumns(8);
tf1.setBounds(100, 100, 250, 50);
b0.setBounds(100, 130, 50, 50);
b1.setBounds(150, 130, 50, 50);
b2.setBounds(200, 130, 50, 50);
b3.setBounds(250, 130, 50, 50);
b4.setBounds(100, 180, 50, 50);
b5.setBounds(150, 180, 50, 50);
b6.setBounds(200, 180, 50, 50);
b7.setBounds(250, 180, 50, 50);
b8.setBounds(100, 230, 50, 50);
b3.setBounds(150, 230, 50, 50);
bAdd.setBounds(200, 230, 50, 50);
bSub.setBounds(250, 230, 50, 50);
bMul.setBounds(100, 280, 50, 50);
bDiv.setBounds(150, 280, 50, 50);
bEqu.setBounds(200, 280, 50, 50);
bSin.setBounds(300, 130, 50, 50);
bCos.setBounds(300, 180, 50, 50);
bTan.setBounds(300, 230, 50, 50);
bSqrt.setBounds(250, 280, 50, 50);
bCls.setBounds(300, 280, 50, 50);
add(b0); add(b1); add(b2);
add(b3); add(b4); add(b5);
add(b6); add(b7); add(b8); add(b9);
add(bAdd); add(bSub); add(bMul); add(bDiv);
add(bEqu); add(bSin); add(bCos); add(bTan);
add(bSqrt); add(bCls); add(tf1);
b0.addActionListener(this);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
bAdd.addActionListener(this);
bSub.addActionListener(this);
bMul.addActionListener(this);
bDiv.addActionListener(this);
bEqu.addActionListener(this);
bSqrt.addActionListener(this);
bSin.addActionListener(this);
bCos.addActionListener(this);
bTan.addActionListener(this);
bCls.addActionListener(this);
}
public void actionPerformed(ActionEvent ae) {
if(ae.getSource() == b0) {
zero();
if(tf1.getText().equals("0"))
tf1.setText(b0.getLabel());
else
tf1.setText(tf1.getText()+b0.getLabel());
temp = 0;
}
if(ae.getSource() == b1) {
zero();
if(tf1.getText().equals("0"))
tf1.setText(b1.getLabel());
else
tf1.setText(tf1.getText()+b1.getLabel());
temp = 0;
}
if(ae.getSource() == b2) {
zero();
if(tf1.getText().equals("0"))
tf1.setText(b2.getLabel());
else
tf1.setText(tf1.getText()+b2.getLabel());
temp = 0;
}
if(ae.getSource() == b3) {
zero();
if(tf1.getText().equals("0"))
tf1.setText(b3.getLabel());
else
tf1.setText(tf1.getText()+b3.getLabel());
temp = 0;
}
if(ae.getSource() == b4) {
zero();
if(tf1.getText().equals("0"))
tf1.setText(b4.getLabel());
else
tf1.setText(tf1.getText()+b4.getLabel());
temp = 0;
}
if(ae.getSource() == b5) {
zero();
if(tf1.getText().equals("0"))
tf1.setText(b5.getLabel());
else
tf1.setText(tf1.getText()+b5.getLabel());
temp = 0;
}
if(ae.getSource() == b6) {
zero();
if(tf1.getText().equals("0"))
tf1.setText(b6.getLabel());
else
tf1.setText(tf1.getText()+b6.getLabel());
temp = 0;
}
if(ae.getSource() == b7) {
zero();
if(tf1.getText().equals("0"))
tf1.setText(b7.getLabel());
else
tf1.setText(tf1.getText()+b7.getLabel());
temp = 0;
}
if(ae.getSource() == b8) {
zero();
if(tf1.getText().equals("0"))
tf1.setText(b8.getLabel());
else
tf1.setText(tf1.getText()+b8.getLabel());
temp = 0;
}
if(ae.getSource() == b9) {
zero();
if(tf1.getText().equals("0"))
tf1.setText(b9.getLabel());
else
tf1.setText(tf1.getText()+b9.getLabel());
temp = 0;
}
if(ae.getSource() == bSqrt) {
sqr = Double.parseDouble(tf1.getText());
tf1.setText(Double.toString(Math.sqrt(sqr)));
}
if(ae.getSource() == bSin) {
s = Double.parseDouble(tf1.getText());
tf1.setText(Double.toString(Math.sin(s)));
}
if(ae.getSource() == bCos) {
c = Double.parseDouble(tf1.getText());
tf1.setText(Double.toString(Math.cos(c)));
}
if(ae.getSource() == bTan) {
ta = Double.parseDouble(tf1.getText());
tf1.setText(Double.toString(Math.tan(ta)));
}
if(ae.getSource() == bAdd) {
add = Integer.parseInt(tf1.getText());
tf1.setText("");
t = "+";
}
if(ae.getSource() == bSub) {
sub = Integer.parseInt(tf1.getText());
tf1.setText("");
t = "-";
}
if(ae.getSource() == bMul) {
mul = Integer.parseInt(tf1.getText());
tf1.setText("");
t = "*";
}
if(ae.getSource() == bDiv) {
div = Integer.parseInt(tf1.getText());
tf1.setText("");
t = "/";
}
if(ae.getSource() == bEqu) {
if(t == "+") {
int add1 = Integer.parseInt(tf1.getText());
int add2 = add + add1;
tf1.setText(String.valueOf(add2));
}
else if(t == "-") {
int sub1 = Integer.parseInt(tf1.getText());
int sub2 = sub - sub1;
tf1.setText(String.valueOf(sub2));
}
else if(t == "*") {
int mul1 = Integer.parseInt(tf1.getText());
int mul2 = mul * mul1;
tf1.setText(String.valueOf(mul2));
}
else if(t == "/") {
int div1 = Integer.parseInt(tf1.getText());
int div2 = div / div1;
tf1.setText(String.valueOf(div2));
}
if(temp == 0) temp = 1;
}
if(ae.getSource() == bCls)
tf1.setText("0");
if(ae.getSource() == bDec) {
String s = tf1.getText();
for(int i = 0; i < s.length(); i++) {
if((s.charAt(i)) == '.') break;
else tf1.setText(tf1.getText()+bDec.getLabel());
}
}
}
void zero() {
if(temp == 1) tf1.setText("0");
}
}
答案 0 :(得分:6)
看看以下几行:
bAdd = new Button("+"); bSub = new Button("-");
bSub = new Button("*"); bDiv = new Button("/");
您将值分配给bSub
两次,但没有为bMul
分配任何值,因此它仍然为空。
然后在第42行取消引用bMul
:
bMul.setBounds(100, 280, 50, 50);
......那是NullPointerException
。我怀疑上面第二行的开头应该是:
bMul = new Button("*");
我强烈建议你坚持每行一个语句。我还建议你将代码分成更小的块 - 例如,一种初始化数字的方法,另一种用于运算符的方法等。
答案 1 :(得分:0)
您没有创建bMul
对象。
只需添加此行bMul=new Button("*");
答案 2 :(得分:0)
你没有初始化bMul !!因此,您正在引用一个null对象并抛出异常