import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;public class Calculator extends JFrame implements ActionListener
{
JPanel tfPanel,btnPanel,southPanel;
JButton b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,divbtn,mulbtn,subbtn,addbtn,clearbtn,radixPointbtn,equalbtn;
        JTextField tf;
    GridLayout gl;
        double op1,result;
        String operation;
        
        public Calculator()
{
super("Calculator");
                tf = new JTextField(20);
                tf.setText("");
tf.setHorizontalAlignment(JTextField.RIGHT);
                tf.setEditable(false);
                tfPanel.add(tf);
                getContentPane().add(tfPanel,"North");
                
                b0 = new JButton("0");
b1 = new JButton("1");
                b2 = new JButton("2");
b3 = new JButton("3");
b4 = new JButton("4");
b5 = new JButton("5");
        b6 = new JButton("6");
b7 = new JButton("7");
b8 = new JButton("8");
b9 = new JButton("9");
                divbtn = new JButton("/");
                mulbtn = new JButton("x");
                subbtn = new JButton("-");
                addbtn = new JButton("+");
                radixPointbtn = new JButton(".");
equalbtn = new JButton("=");
                
                
                clearbtn = new JButton("C");
                clearbtn.addActionListener(new ActionListener()
                {
public void actionPerformed(ActionEvent e)
         {
tf.setText("");
                        }
                });
                
   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);
                divbtn.addActionListener(this);
                mulbtn.addActionListener(this);
subbtn.addActionListener(this);
addbtn.addActionListener(this);
                radixPointbtn.addActionListener(this);
        
          gl = new GridLayout(4,4,5,5);
btnPanel.setLayout(gl);
btnPanel.add(b7);
btnPanel.add(b8);
btnPanel.add(b9);
btnPanel.add(divbtn);
btnPanel.add(b4);
btnPanel.add(b5);
btnPanel.add(b6);
btnPanel.add(mulbtn);
btnPanel.add(b1);
btnPanel.add(b2);
btnPanel.add(b3);
btnPanel.add(subbtn);
btnPanel.add(b0);
btnPanel.add(radixPointbtn);
btnPanel.add(equalbtn);
                btnPanel.add(addbtn);
                getContentPane().add(btnPanel);
                
                southPanel.add(clearbtn);
                getContentPane().add(southPanel,"south");
                
                setSize(300,250);
     setVisible(true);
                setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}        public void actionPerformed(ActionEvent e)
      {
Object objKey = e.getSource();

                if(objKey == b0)
  {
tf.setText(tf.getText()+"0");
}
  else if(objKey == b1)
{
tf.setText(tf.getText()+"1");
}
else if(objKey == b2)
{
tf.setText(tf.getText()+"2");
   }
else if(objKey == b3)
{
tf.setText(tf.getText()+"3");
}
else if(objKey == b4)
{
tf.setText(tf.getText()+"4");
    }
else if(objKey == b5)
{
tf.setText(tf.getText()+"5");
                }
else if(objKey == b6)
{
tf.setText(tf.getText()+"6");
}
else if(objKey == b7)
         {
tf.setText(tf.getText()+"7");
}
else if(objKey == b8)
{
tf.setText(tf.getText()+"8");
   }
                else if(objKey == b9)
{
  tf.setText(tf.getText()+"9");
                }
else if(objKey == radixPointbtn)
{
tf.setText(tf.getText()+".");
    }
else if(objKey == addbtn)
{
op1 = Double.parseDouble(tf.getText());
operation ="add";
  tf.setText("");
                }
else if(objKey == subbtn)
{
op1 = Double.parseDouble(tf.getText());
operation = "sub";
tf.setText("");
   }
else if(objKey == mulbtn)
{
op1 = Double.parseDouble(tf.getText());
operation = "mul";
tf.setText("");
}
else if(objKey == divbtn)
{
op1 = Double.parseDouble(tf.getText());
operation = "div";
tf.setText("");
  }
    else if(objKey == equalbtn)
{
if(operation == "add")
     {
result = op1 + Double.parseDouble(tf.getText());
tf.setText(Double.toString(result));
}
  else if(operation == "sub")
{
result = op1 - Double.parseDouble(tf.getText());
  tf.setText(Double.toString(result));
                        }
else if(operation == "mul")
{
result = op1 * Double.parseDouble(tf.getText());
tf.setText(Double.toString(result));
}
else if(operation == "div")
{
result = op1 / Double.parseDouble(tf.getText());
tf.setText(Double.toString(result));
}
}
}

public static void main(String[] args)
{
new Calculator();
        }
}Exception in thread "main" java.lang.NullPointerException
        at Calculator.<init>(Calculator.java:22)
        at Calculator.main(Calculator.java:195)
运行的时候出现以上问题,不知错在哪里,高手指教

解决方案 »

  1.   

    老兄,拜托你在使用那些JPanel变量之前,先把他们声明实例化好后才使用。还有,别忘了给分哦,O(∩_∩)O~
      

  2.   

    布局的参数错了,改好了
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.io.*;public class Calculator extends JFrame implements ActionListener {
    JPanel tfPanel, btnPanel, southPanel;
    JButton b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, divbtn, mulbtn, subbtn,
    addbtn, clearbtn, radixPointbtn, equalbtn;
    JTextField tf;
    GridLayout gl;
    double op1, result;
    String operation; public Calculator() {
    super("Calculator");
    tf = new JTextField(20);
    tf.setText("");
    tf.setHorizontalAlignment(JTextField.RIGHT);
    tf.setEditable(false);
    tfPanel = new JPanel();
    tfPanel.add(tf);
    getContentPane().add(tfPanel, "North"); b0 = new JButton("0");
    b1 = new JButton("1");
    b2 = new JButton("2");
    b3 = new JButton("3");
    b4 = new JButton("4");
    b5 = new JButton("5");
    b6 = new JButton("6");
    b7 = new JButton("7");
    b8 = new JButton("8");
    b9 = new JButton("9");
    divbtn = new JButton("/");
    mulbtn = new JButton("x");
    subbtn = new JButton("-");
    addbtn = new JButton("+");
    radixPointbtn = new JButton(".");
    equalbtn = new JButton("="); clearbtn = new JButton("C");
    clearbtn.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    tf.setText("");
    }
    }); 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);
    divbtn.addActionListener(this);
    mulbtn.addActionListener(this);
    subbtn.addActionListener(this);
    addbtn.addActionListener(this);
    radixPointbtn.addActionListener(this); gl = new GridLayout(4, 4, 5, 5);
    btnPanel = new JPanel();
    btnPanel.setLayout(gl);
    btnPanel.add(b7);
    btnPanel.add(b8);
    btnPanel.add(b9);
    btnPanel.add(divbtn);
    btnPanel.add(b4);
    btnPanel.add(b5);
    btnPanel.add(b6);
    btnPanel.add(mulbtn);
    btnPanel.add(b1);
    btnPanel.add(b2);
    btnPanel.add(b3);
    btnPanel.add(subbtn);
    btnPanel.add(b0);
    btnPanel.add(radixPointbtn);
    btnPanel.add(equalbtn);
    btnPanel.add(addbtn);
    getContentPane().add(btnPanel);
    southPanel = new JPanel();
    southPanel.add(clearbtn);
    getContentPane().add(southPanel, BorderLayout.SOUTH); setSize(300, 250);
    setVisible(true);
    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    } public void actionPerformed(ActionEvent e) {
    Object objKey = e.getSource(); if (objKey == b0) {
    tf.setText(tf.getText() + "0");
    } else if (objKey == b1) {
    tf.setText(tf.getText() + "1");
    } else if (objKey == b2) {
    tf.setText(tf.getText() + "2");
    } else if (objKey == b3) {
    tf.setText(tf.getText() + "3");
    } else if (objKey == b4) {
    tf.setText(tf.getText() + "4");
    } else if (objKey == b5) {
    tf.setText(tf.getText() + "5");
    } else if (objKey == b6) {
    tf.setText(tf.getText() + "6");
    } else if (objKey == b7) {
    tf.setText(tf.getText() + "7");
    } else if (objKey == b8) {
    tf.setText(tf.getText() + "8");
    } else if (objKey == b9) {
    tf.setText(tf.getText() + "9");
    } else if (objKey == radixPointbtn) {
    tf.setText(tf.getText() + ".");
    } else if (objKey == addbtn) {
    op1 = Double.parseDouble(tf.getText());
    operation = "add";
    tf.setText("");
    } else if (objKey == subbtn) {
    op1 = Double.parseDouble(tf.getText());
    operation = "sub";
    tf.setText("");
    } else if (objKey == mulbtn) {
    op1 = Double.parseDouble(tf.getText());
    operation = "mul";
    tf.setText("");
    } else if (objKey == divbtn) {
    op1 = Double.parseDouble(tf.getText());
    operation = "div";
    tf.setText("");
    } else if (objKey == equalbtn) {
    if (operation == "add") {
    result = op1 + Double.parseDouble(tf.getText());
    tf.setText(Double.toString(result));
    } else if (operation == "sub") {
    result = op1 - Double.parseDouble(tf.getText());
    tf.setText(Double.toString(result));
    } else if (operation == "mul") {
    result = op1 * Double.parseDouble(tf.getText());
    tf.setText(Double.toString(result));
    } else if (operation == "div") {
    result = op1 / Double.parseDouble(tf.getText());
    tf.setText(Double.toString(result));
    }
    }
    } public static void main(String[] args) {
    new Calculator();
    }
    }