-1

I'm trying to run this code in java applet via appletviewer in Fedora 18. But I get this error

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)

As per my code 42nd line in my code is bMul.setBounds(100, 280, 50, 50); but this isn't wrong as-far-as i know. After searching on Google, I found that

NullPointerException is a runtime Exception thrown by the JVM when your application code, other referenced API(s) or middleware (Weblogic, WAS, JBoss...) encounters the following conditions:

  • Attempting to invoke an instance method of a null object
  • Attempting to access or modify a particular field of a null object
  • Attempting to obtain the length of such null object as an array

I've tried hard but failed in making it work. Please help me. Here I provide main.java file's code.

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");
  }
}
4

3 回答 3

6

Look at these lines:

bAdd = new Button("+"); bSub = new Button("-");
bSub = new Button("*"); bDiv = new Button("/");

You're assigning a value to bSub twice, but not assigning any value to bMul, so it's still null.

When you then dereference bMul at line 42:

bMul.setBounds(100, 280, 50, 50);

... that's throwing a NullPointerException. I suspect the start of the second line from above should be:

bMul = new Button("*");

I would strongly advise you to stick to one statement per line. I'd also advise you to break your code up into smaller chunks - for example, one method to initialize the digits, another for the operators etc.

于 2013-02-16T16:57:48.107 回答
0

您没有创建bMul对象。

只需添加这一行bMul=new Button("*");

于 2013-02-16T16:59:00.633 回答
0

你没有初始化 bMul!!因此,您正在引用一个空对象并抛出异常

于 2013-02-16T17:00:04.567 回答