NullPointerException - THENIGHTMARE

时间:2018-05-17 13:16:32

标签: java nullpointerexception

你会想到我有多少时间来回答NullPointerException。虽然它有答案,但新手程序员很难找到原因并修复它。请帮忙。

这是代码 -

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.script.*;
public class JCalculator extends javax.swing.JFrame{
JButton n1,n2,n3,n4,n5,n6,n7,n8,n9,n10,n11,n12,n13,n14,n15,n16,n17,n18;
public void calc(){
    JFrame frame = new JFrame("JCalculator");
    frame.setExtendedState(Frame.MAXIMIZED_BOTH);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JTextField tf = new JTextField();
    JPanel panel = new JPanel();
    panel.setLayout(new BorderLayout());
    panel.setLayout(new GridLayout(4,4,4,4));
    //1.declare and instantiate buttons
    n1 = new JButton("1");
    n2 = new JButton("2");
    n3 = new JButton("3");
    n4 = new JButton("4");
    n5 = new JButton("5");
    n6 = new JButton("6");
    n7 = new JButton("7");
    n8 = new JButton("8");
    n9 = new JButton("9");
    n10= new JButton("0");
    n11= new JButton("+");
    n12= new JButton("-");
    n13= new JButton("*");
    n14= new JButton("/");
    n15= new JButton("=");
    n16= new JButton("C");
    n17= new JButton(".");
    n18= new JButton("Del");
    //2.add to frame
    panel.add(tf);
    panel.add(n1);
    panel.add(n2);
    panel.add(n3);
    panel.add(n4);
    panel.add(n5);
    panel.add(n6);
    panel.add(n7);
    panel.add(n8);
    panel.add(n9);
    panel.add(n10);
    panel.add(n11);
    panel.add(n12);
    panel.add(n13);
    panel.add(n14);
    panel.add(n15);
    panel.add(n16);
    panel.add(n17);
    panel.add(n18);
    frame.add(panel);

    //4.AddActionListener
    n1.addActionListener((ActionEvent) -> {
       String num1 = n1.getText();
       String num  = tf.getText();
       num = num.concat(num1);
       tf.setText(num);
    });
    n2.addActionListener((ActionEvent) -> {
        String num1 = n2.getText();
        String num = tf.getText();
        num = num.concat(num1);
        tf.setText(num);
    });
    n3.addActionListener((ActionEvent) -> {
        String num1 = n3.getText();
        String num = tf.getText();
        num = num.concat(num1);
        tf.setText(num);
    });
    n4.addActionListener((ActionEvent) -> {
        String num1 = n4.getText();
        String num = tf.getText();
        num = num.concat(num1);
        tf.setText(num);
    });
    n5.addActionListener((ActionEvent) -> {
        String num1 = n5.getText();
        String num = tf.getText();
        num = num.concat(num1);
        tf.setText(num);
    });
    n6.addActionListener((ActionEvent) -> {
        String num1 = n6.getText();
        String num = tf.getText();
        num = num.concat(num1);
        tf.setText(num);
    });
    n7.addActionListener((ActionEvent) -> {
        String num1 = n7.getText();
        String num = tf.getText();
        num = num.concat(num1);
        tf.setText(num);
    });
    n8.addActionListener((ActionEvent) -> {
        String num1 = n8.getText();
        String num = tf.getText();
        num = num.concat(num1);
        tf.setText(num);
    });
    n9.addActionListener((ActionEvent) -> {
        String num1 = n9.getText();
        String num = tf.getText();
        num = num.concat(num1);
        tf.setText(num);
    });
    n10.addActionListener((ActionEvent) -> {
        String num1 = n10.getText();
        String num = tf.getText();
        num = num.concat(num1);
        tf.setText(num);
    });
    n11.addActionListener((ActionEvent) -> {
        String num1 = n11.getText();
        String num = tf.getText();
        num = num.concat(num1);
        tf.setText(num);
    });
    n12.addActionListener((ActionEvent) -> {
        String num1 = n12.getText();
        String num = tf.getText();
        num = num.concat(num1);
        tf.setText(num);
    });
    n13.addActionListener((ActionEvent) -> {
        String num1 = n13.getText();
        String num = tf.getText();
        num = num.concat(num1);
        tf.setText(num);
    });
    n14.addActionListener((ActionEvent) -> {
        String num1 = n14.getText();
        String num = tf.getText();
        num = num.concat(num1);
        tf.setText(num);
    });
    n15.addActionListener((ActionEvent) -> {
       String num = tf.getText();
       tf.setText("");
       ScriptEngineManager mgr = new ScriptEngineManager();
       ScriptEngine engine = mgr.getEngineByName("JavScript");
       try {
           String s = engine.eval(num).toString();
           tf.setText(s);
       }catch (ScriptException e) {
           e.printStackTrace();
       }
    });
    n16.addActionListener((ActionEvent) -> {
        String num = n16.getText();
        num = null;
        tf.setText(num);
    });
    n17.addActionListener((ActionEvent) -> {
        String num1 = n17.getText();
        String num = tf.getText();
        num = num.concat(num1);
        tf.setText(num);
    });
    n18.addActionListener((ActionEvent) -> {
        String num = tf.getText();
        String num1 = "";
        for(int i = 0; i<num.length()-1; i++){
            tf.setText(num1+num.charAt(i));
        }
    });
    frame.setVisible(true);
    tf.setVisible(true);
    n1.setVisible(true);
    n2.setVisible(true);
    n3.setVisible(true);
    n4.setVisible(true);
    n5.setVisible(true);
    n6.setVisible(true);
    n7.setVisible(true);
    n8.setVisible(true);
    n9.setVisible(true);
    n10.setVisible(true);
    n11.setVisible(true);
    n12.setVisible(true);
    n13.setVisible(true);
    n14.setVisible(true);
    n15.setVisible(true);
    n16.setVisible(true);
    n17.setVisible(true);
}}

当我按下&#34; =&#34;按钮引发此异常 -

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at JCalculator.lambda$calc$14(JCalculator.java:147)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6533)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6298)
at java.awt.Container.processEvent(Container.java:2237)
at java.awt.Component.dispatchEventImpl(Component.java:4889)
at java.awt.Container.dispatchEventImpl(Container.java:2295)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4889)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4526)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4467)
at java.awt.Container.dispatchEventImpl(Container.java:2281)
at java.awt.Window.dispatchEventImpl(Window.java:2746)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
BUILD SUCCESSFUL (total time: 29 seconds)

为什么我收到此错误?根本原因是什么?请清楚地解释一下。

1 个答案:

答案 0 :(得分:1)

空指针异常是最容易修复的错误之一。

错误发生在

           String s = engine.eval(num).toString();

表示引擎未正确启动。好像脚本名称有拼写错误。应该是 javascript

包含任何空指针异常导航到行号,按顺序检查对象,即(左 - 右),检查可能的空值,将断点放在同一行并在调试模式下运行。