处理ActionListeners&时的NullPointer异常纽扣

时间:2017-04-15 17:34:57

标签: java swing model-view-controller nullpointerexception jbutton

我正在与MVC作战,我正在制作一个简单的计算器。我有4个班级,主要,模型,视图和控制器。问题是我在尝试将一个Listener与我的Controller类连接时得到NullPointer,它有一个嵌套类。我在互联网上看到了类似的解决方案,但我无法让它发挥作用。无论如何,这是代码:

public class CView {

public JPanel panel;
private JButton b1, b2, b3, b4, b5, b6, b7 , b8, b9, b0, bDel, bDiv, bMul, bAdd, bSub, bRes;
public JTextField t;
private String value;
public int tValue1 = 0;
public int tValue2 = 0;

public JPanel makePanel() {
    panel = new JPanel();
    panel.setLayout(null);
    t = new JTextField();

    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");
    b0 = new JButton("0");
    bDel = new JButton("Del");
    bDiv = new JButton("/");
    bMul = new JButton("*");
    bAdd = new JButton("+");
    bSub = new JButton("-");
    //bClr = new JButton("CLC");
    bRes = new JButton("=");

    b1.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            value = ((JButton)e.getSource()).getText();
            t.setText(t.getText()+value);
        }
    });
    b2.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            value = ((JButton)e.getSource()).getText();
            t.setText(t.getText()+value);
        }
    });
    b3.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            value = ((JButton)e.getSource()).getText();
            t.setText(t.getText()+value);
        }
    });
    b4.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            value = ((JButton)e.getSource()).getText();
            t.setText(t.getText()+value);
        }
    });
    b5.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            value = ((JButton)e.getSource()).getText();
            t.setText(t.getText()+value);
        }
    });
    b6.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            value = ((JButton)e.getSource()).getText();
            t.setText(t.getText()+value);
        }
    });
    b7.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            value = ((JButton)e.getSource()).getText();
            t.setText(t.getText()+value);
        }
    });
    b8.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            value = ((JButton)e.getSource()).getText();
            t.setText(t.getText()+value);
        }
    });
    b9.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            value = ((JButton)e.getSource()).getText();
            t.setText(t.getText()+value);
        }
    });
    b0.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            value = ((JButton)e.getSource()).getText();
            t.setText(t.getText()+value);
        }
    });
    bDel.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            t.setText(t.getText().substring(0, t.getText().length()-1));                
        }
    });     

    bDiv.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            getValue1();

        }
    });


    panel.add(b7);
    panel.add(b8);
    panel.add(b9);
    panel.add(b4);
    panel.add(b5);
    panel.add(b6);
    panel.add(b1);
    panel.add(b2);
    panel.add(b3);
    panel.add(b0);
    panel.add(bRes);
    panel.add(bDel);
    panel.add(bDiv);
    panel.add(bMul);
    panel.add(bAdd);
    panel.add(bSub);
    panel.add(t);
    t.setBounds(30,40,280,30);
    b7.setBounds(40,100,50,40);
    b8.setBounds(110,100,50,40);
    b9.setBounds(180,100,50,40);
    bDiv.setBounds(250,100,50,40);


    b4.setBounds(40,170,50,40);
    b5.setBounds(110,170,50,40);
    b6.setBounds(180,170,50,40);
    bMul.setBounds(250,170,50,40);

    b1.setBounds(40,240,50,40);
    b2.setBounds(110,240,50,40);
    b3.setBounds(180,240,50,40);
    bSub.setBounds(250,240,50,40);

    bDel.setBounds(40,310,60,40);
    b0.setBounds(110,310,50,40);
    bRes.setBounds(180,310,60,40);
    bAdd.setBounds(250,310,50,40);

    panel.setVisible(true);

    return panel;

}

void divCalculateListener(ActionListener l){
    bRes.addActionListener(l); //here is the problem

}

public int getValue1() {
    tValue1 = Integer.parseInt(t.getText());
    t.setText("");
    return tValue1;
}

public int getValue2() {
    tValue2 = Integer.parseInt(t.getText());
    t.setText("");
    return tValue2;
}

public void setResult(int res) {
    t.setText(Integer.toString(res));
    System.out.println("Ispis");
}
}

这是Controller类:

public class Controller  {

private Model model;
private CView view;

public Controller(Model model, CView view) {
    this.model = model;
    this.view = view;

    this.view.divCalculateListener(new CalculateListener());
}

class CalculateListener implements ActionListener {

    public void actionPerformed(ActionEvent e) {
        view.getValue2();
        try {
            model.divide(view.getValue1(), view.getValue2());
            view.setResult(model.getResult());
        } catch (NumberFormatException ne) {

        }

    }
}
}

正如您所看到的here,当他们尝试连接按钮时,它们使用与我完全相同的格式。提前谢谢。

0 个答案:

没有答案