简体   繁体   English

线程“ AWT-EventQueue-0”中的异常java.lang.NumberFormatException:对于输入字符串:“ null”

[英]Exception in thread “AWT-EventQueue-0” java.lang.NumberFormatException: For input string: “null”

package Unit2Exam;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Unit2Exam {

    static int r;
    static int g;
    static int b;
    static int rb;
    static int rg;
    static int on;
    static int rr;
    static int i;
    public static JTextField field1;
    public static JTextField field2;
    public static JTextField field3;

    public static void main(String[] args) {
        //This is my constructor for the Math class, it allows me to import methods from that class
        //final Math a = new Math();
        final Unit2Exam Calculator = new Unit2Exam();

        //Makes a font
        Font font = new Font("Verdana", Font.PLAIN,  12);

        GridBagConstraints grid = new GridBagConstraints();
        //This creates the JFRAMES
        JFrame frame = new JFrame("Free Fall Application");
        //This sets the title
        JPanel panel = new JPanel(new GridBagLayout());
        //makes and adds the panel
        frame.add(panel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        grid.fill = GridBagConstraints.BOTH;

        JFrame colors = new JFrame("Colors");
        JPanel colorPanel = new JPanel(new GridBagLayout());
        colors.add(colorPanel);
        colors.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        grid.fill = GridBagConstraints.BOTH;

        //causes the jbuttons to fill the borders (to make it look better)
        grid.weightx = 1;
        grid.weighty = 1;
        // frame.setSize(new Dimension(200, 200));
        frame.setResizable(false);
        colors.setResizable(false);
        colors.setVisible(true);
        colorPanel.setVisible(true);
        frame.setVisible(true);
        panel.setVisible(true);

        //This is where I implement all of the J stuff (i.e. Buttons, Textfields, and JLabels)
        JLabel title = new JLabel("Jonathan Kipper");
        JLabel label1 = new JLabel("Initial Velocity (m/s):");
        JLabel label2 = new JLabel("Time (Seconds):");
        JLabel label3 = new JLabel("Distance (meters):");
        JTextField field1 = new JTextField(6);
        JTextField field2 = new JTextField(6);
        JTextField field3 = new JTextField(6);
        JButton button1 = new JButton("Calculate");
        JButton button2 = new JButton("Clear");
        JButton colorButton = new JButton();

        grid.gridx = 0;
        grid.gridy = 0;
        panel.add(title, grid);
        title.setVisible(true);
        title.setFont(font);

        grid.gridx = 0;
        grid.gridy = 1;
        panel.add(label1, grid);
        label1.setVisible(true);
        label1.setFont(font);

        grid.gridx = 0;
        grid.gridy = 2;
        panel.add(label2, grid);
        label2.setVisible(true);
        label2.setFont(font);

        grid.gridx = 0;
        grid.gridy = 3;
        panel.add(label3, grid);
        label3.setVisible(true);
        label3.setFont(font);

        grid.gridx = 1;
        grid.gridy = 1;
        panel.add(field1, grid);
        field1.setVisible(true);

        grid.gridx = 1;
        grid.gridy = 2;
        panel.add(field2, grid);
        field2.setVisible(true);

        grid.gridx = 1;
        grid.gridy = 3;
        panel.add(field3, grid);
        field3.setText("");
        field3.setVisible(true);

        grid.gridx = 0;
        grid.gridy = 4;
        panel.add(button1, grid);
        button1.setVisible(true);

        grid.gridx = 1;
        grid.gridy = 4;
        panel.add(button2, grid);
        button2.setVisible(true);
        button2.setFont(font);

        frame.pack();
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        frame.setLocation(dim.width / 2 - frame.getSize().width / 2, dim.height / 2 - frame.getSize().height / 2);
        frame.setSize(250, 150);

        grid.gridx=0;
        grid.gridy=0;
        colorPanel.add(colorButton, grid);
        colorButton.setText("Click to stop colors");
        colorButton.setVisible(true);

        colors.pack();
        Dimension dimz = Toolkit.getDefaultToolkit().getScreenSize();
        colors.setLocation(dimz.width / 3 - frame.getSize().width / 3, dimz.height / 2 - frame.getSize().height / 2);
        colors.setSize(200, 150);

        i=1;
        //Infinite for loop

        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                double zzz = Calculator.distance();
                String za = String.valueOf(zzz);
                field3.setText(za);
            }
        });

        button2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                field1.setText("");
                field2.setText("");
                field3.setText("");

            }
        });



        colorButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                if (i == 1) {
                    colorButton.setText("Click here to start");
                    i = 0;
                } else if (i == 0) {
                    colorButton.setText("Click here to stop");
                    i = 1;
                }

            }
        });

        on = 0;
        r = 0;
        g = 0;
        b = 0;

        rr = 255;
        rg = 255;
        rb = 255;
        //This is the loop for the color changing
        while (i != 0) {
            //Colors
            Color color = new Color(r, g, b);
            Color rcolor = new Color(rr, rg, rb);

            //Panels
            panel.setBackground(color);
            frame.setBackground(color);

            //Buttons
            button1.setBackground(rcolor);
            button2.setBackground(rcolor);
            colorButton.setBackground(rcolor);

            //Labels
            label1.setForeground(rcolor);
            label2.setForeground(rcolor);
            label3.setForeground(rcolor);
            title.setForeground(rcolor);

            //Text fields
            field1.setForeground(rcolor);
            field2.setForeground(rcolor);
            field3.setForeground(rcolor);
            field1.setBackground(color);
            field2.setBackground(color);
            field3.setBackground(color);

            //Sets a slight delay to avoid seizures
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            //Goes through the different colors
            if (on == 0) {
                if (r == 255) {
                    on = 1;
                } else {
                    r++;
                    rr--;
                }
            } else if (on == 1) {
                if (g == 255) {
                    on = 2;
                } else {
                    g++;
                    rg--;
                }
            } else if (on == 2) {
                if (b == 255) {
                    on = 3;
                } else {
                    b++;
                    rb--;
                }
            } else if (on == 3) {
                if (r == 0) {
                    on = 4;
                } else {
                    r--;
                    rr++;
                }
            } else if (on == 4) {
                if (g == 0) {
                    on = 5;
                } else {
                    g--;
                    rg++;
                }
            } else if (on == 5) {
                if (b == 0) {
                    on = 0;
                } else {
                    b--;
                    rb++;
                }
            }
        }
    }

    public static double distance(){

        String d = field1.getText();
        double velocity = Double.parseDouble(d);
        String dd = field2.getText();
        double time = Double.valueOf(dd);

        double totalDistance = (velocity * time) + (4.9 * (time * time));

        return totalDistance;
    }
}

UPDATE: I can't leave the method as a public static void because it errors out when trying to call it to return a value. 更新:我不能将方法保留为公共静态void,因为在尝试调用它返回值时会出错。 I've changed the strings to field.getText(); 我已经将字符串更改为field.getText(); instead of the String.valueOf(field); 而不是String.valueOf(field); It keeps giving me nullpoint errors in the method and when I call the method earlier in the program. 当我在程序中更早地调用该方法时,它会不断给我带来nullpoint错误。

Uhm, you are trying to get the String by 嗯,您正在尝试通过

String.valueOf(JTextField)

Thats doing something waaay different from what you want. 那就是做一些与您想要的不同的事情。 You can get the Text of the Field by: 您可以通过以下方式获取字段文本:

e.g. String d = field1.getText();

Edit: 编辑:

I dont think, Unit2Exam has a purpose, but on your button, you are still using it. 我不认为Unit2Exam有目的,但是在您的按钮上,您仍在使用它。 First of all, declare distance as static Method: 首先,将距离声明为静态方法:

public static void distance()

Then, replace in your ActionListener 然后,在您的ActionListener中替换

aa.distance()

with

Calculator.distance()

Edit 2: 编辑2:

So, another answer. 因此,另一个答案。

First of all, your code is quite messed up, i assume you are new to java. 首先,您的代码很混乱,我认为您是Java新手。 Let me tell you your mistakes: 让我告诉你你的错误:

First of all: 首先:

  final Unit2Exam Calculator = new Unit2Exam();

Since you have no constructor, you dont need this. 由于没有构造函数,因此不需要。 Cut it out. 剪掉

Second: 第二:

The reason, why youre field returns null, is because: 您的字段为何返回null的原因是:

 JTextField field1 = new JTextField(6);
 JTextField field2 = new JTextField(6);
 JTextField field3 = new JTextField(6);

creates a local Textfield called field1, which has nothing to do with your global JTextField field1, which stays empty for the rest of its life. 创建一个名为field1的本地 Textfield,它与全局 JTextField field1无关,该字段在余下的时间里都保持为空。

Just remove the "JTextField" before each declaration and it should work: 只需在每个声明之前删除“ JTextField”,它就可以工作:

 field1 = new JTextField(6);
 field2 = new JTextField(6);
 field3 = new JTextField(6);

Final result: 最后结果:

  package Unit2Exam;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Unit2Exam {

    static int r;
    static int g;
    static int b;
    static int rb;
    static int rg;
    static int on;
    static int rr;
    static int i;
    public static JTextField field1;
    public static JTextField field2;
    public static JTextField field3;

    public static void main(String[] args) {

        //Makes a font
        Font font = new Font("Verdana", Font.PLAIN,  12);

        GridBagConstraints grid = new GridBagConstraints();
        //This creates the JFRAMES
        JFrame frame = new JFrame("Free Fall Application");
        //This sets the title
        JPanel panel = new JPanel(new GridBagLayout());
        //makes and adds the panel
        frame.add(panel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        grid.fill = GridBagConstraints.BOTH;

        JFrame colors = new JFrame("Colors");
        JPanel colorPanel = new JPanel(new GridBagLayout());
        colors.add(colorPanel);
        colors.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        grid.fill = GridBagConstraints.BOTH;

        //causes the jbuttons to fill the borders (to make it look better)
        grid.weightx = 1;
        grid.weighty = 1;
        // frame.setSize(new Dimension(200, 200));
        frame.setResizable(false);
        colors.setResizable(false);
        colors.setVisible(true);
        colorPanel.setVisible(true);
        frame.setVisible(true);
        panel.setVisible(true);

        //This is where I implement all of the J stuff (i.e. Buttons, Textfields, and JLabels)
        JLabel title = new JLabel("Jonathan Kipper");
        JLabel label1 = new JLabel("Initial Velocity (m/s):");
        JLabel label2 = new JLabel("Time (Seconds):");
        JLabel label3 = new JLabel("Distance (meters):");
        field1 = new JTextField(6);
       field2 = new JTextField(6);
         field3 = new JTextField(6);
        JButton button1 = new JButton("Calculate");
        JButton button2 = new JButton("Clear");
        JButton colorButton = new JButton();

        grid.gridx = 0;
        grid.gridy = 0;
        panel.add(title, grid);
        title.setVisible(true);
        title.setFont(font);

        grid.gridx = 0;
        grid.gridy = 1;
        panel.add(label1, grid);
        label1.setVisible(true);
        label1.setFont(font);

        grid.gridx = 0;
        grid.gridy = 2;
        panel.add(label2, grid);
        label2.setVisible(true);
        label2.setFont(font);

        grid.gridx = 0;
        grid.gridy = 3;
        panel.add(label3, grid);
        label3.setVisible(true);
        label3.setFont(font);

        grid.gridx = 1;
        grid.gridy = 1;
        panel.add(field1, grid);
        field1.setVisible(true);

        grid.gridx = 1;
        grid.gridy = 2;
        panel.add(field2, grid);
        field2.setVisible(true);

        grid.gridx = 1;
        grid.gridy = 3;
        panel.add(field3, grid);
        field3.setText("");
        field3.setVisible(true);

        grid.gridx = 0;
        grid.gridy = 4;
        panel.add(button1, grid);
        button1.setVisible(true);

        grid.gridx = 1;
        grid.gridy = 4;
        panel.add(button2, grid);
        button2.setVisible(true);
        button2.setFont(font);

        frame.pack();
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        frame.setLocation(dim.width / 2 - frame.getSize().width / 2, dim.height / 2 - frame.getSize().height / 2);
        frame.setSize(250, 150);

        grid.gridx=0;
        grid.gridy=0;
        colorPanel.add(colorButton, grid);
        colorButton.setText("Click to stop colors");
        colorButton.setVisible(true);

        colors.pack();
        Dimension dimz = Toolkit.getDefaultToolkit().getScreenSize();
        colors.setLocation(dimz.width / 3 - frame.getSize().width / 3, dimz.height / 2 - frame.getSize().height / 2);
        colors.setSize(200, 150);

        i=1;
        //Infinite for loop

        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                double zzz = Unit2Exam.distance();
                String za = String.valueOf(zzz);
                field3.setText(za);
            }
        });

        button2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                field1.setText("");
                field2.setText("");
                field3.setText("");

            }
        });



        colorButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                if (i == 1) {
                    colorButton.setText("Click here to start");
                    i = 0;
                } else if (i == 0) {
                    colorButton.setText("Click here to stop");
                    i = 1;
                }

            }
        });

        on = 0;
        r = 0;
        g = 0;
        b = 0;

        rr = 255;
        rg = 255;
        rb = 255;
        //This is the loop for the color changing
        while (i != 0) {
            //Colors
            Color color = new Color(r, g, b);
            Color rcolor = new Color(rr, rg, rb);

            //Panels
            panel.setBackground(color);
            frame.setBackground(color);

            //Buttons
            button1.setBackground(rcolor);
            button2.setBackground(rcolor);
            colorButton.setBackground(rcolor);

            //Labels
            label1.setForeground(rcolor);
            label2.setForeground(rcolor);
            label3.setForeground(rcolor);
            title.setForeground(rcolor);

            //Text fields
            field1.setForeground(rcolor);
            field2.setForeground(rcolor);
            field3.setForeground(rcolor);
            field1.setBackground(color);
            field2.setBackground(color);
            field3.setBackground(color);

            //Sets a slight delay to avoid seizures
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            //Goes through the different colors
            if (on == 0) {
                if (r == 255) {
                    on = 1;
                } else {
                    r++;
                    rr--;
                }
            } else if (on == 1) {
                if (g == 255) {
                    on = 2;
                } else {
                    g++;
                    rg--;
                }
            } else if (on == 2) {
                if (b == 255) {
                    on = 3;
                } else {
                    b++;
                    rb--;
                }
            } else if (on == 3) {
                if (r == 0) {
                    on = 4;
                } else {
                    r--;
                    rr++;
                }
            } else if (on == 4) {
                if (g == 0) {
                    on = 5;
                } else {
                    g--;
                    rg++;
                }
            } else if (on == 5) {
                if (b == 0) {
                    on = 0;
                } else {
                    b--;
                    rb++;
                }
            }
        }
    }

    public static double distance(){

        String d = field1.getText();
        double velocity = Double.parseDouble(d);
        String dd = field2.getText();
        double time = Double.valueOf(dd);

        double totalDistance = (velocity * time) + (4.9 * (time * time));

        return totalDistance;
    }

I change it to 我将其更改为

public double distance() {

    String d = field1.getText();
    double velocity = Double.parseDouble(d);
    String dd = field2.getText();
    double time = Double.valueOf(dd);

    double totalDistance = (velocity * time) + (4.9 * (time * time));

    return totalDistance;
}

However I'm still getting a null pointer exception. 但是我仍然收到一个空指针异常。 It changed the error slightly however. 但是它稍微改变了错误。 It's saying on line 273, which is the: String d = field1.getText(); 在第273行说,这是:String d = field1.getText(); is an issue. 是一个问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java parseint-线程“ AWT-EventQueue-0”中的异常java.lang.NumberFormatException:对于输入字符串:“” - java parseint - Exception in thread “AWT-EventQueue-0” java.lang.NumberFormatException: For input string: “” 线程“ AWT-EventQueue-0”中的异常java.lang.NumberFormatException:对于输入字符串:“ java中的78错误 - Exception in thread “AWT-EventQueue-0” java.lang.NumberFormatException: For input string: "78 error in java 字符串数据仍然存在? (线程“AWT-EventQueue-0”java.lang.NumberFormatException 中的异常:对于输入字符串) - String data still remains? (Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string) 线程“ AWT-EventQueue-0”中的异常java.lang.NumberFormatException:对于输入字符串:“ 11101110110100011111110111011010001” - Exception in thread “AWT-EventQueue-0” java.lang.NumberFormatException: For input string: “11101110110100011110111011010001” 线程“ AWT-EventQueue-0”中的异常java.lang.NumberFormatException:对于输入字符串:“” - Exception in thread “AWT-EventQueue-0” java.lang.NumberFormatException: For input string: “” 线程“ AWT-EventQueue-0”中的异常java.lang.NumberFormatException:对于输入字符串“” - Exception in thread “AWT-EventQueue-0” java.lang.NumberFormatException: For input string “” 线程“AWT-EventQueue-0”中的异常java.lang.NumberFormatException:对于输入字符串:“1” - Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "1 " 线程“AWT-EventQueue-0”java.lang.NumberFormatException 中的异常:对于输入字符串:“选择月份” - Exception in thread “AWT-EventQueue-0” java.lang.NumberFormatException: For input string: “Select Month” 线程“AWT-EventQueue-0”中的异常java.lang.NumberFormatException:对于输入字符串:“” - Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string:" " 线程“ AWT-EventQueue-0”中的异常java.lang.NumberFormatException:对于输入字符串: - Exception in thread “AWT-EventQueue-0” java.lang.NumberFormatException: For input string:
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM