This question already has answers here:
What is a NullPointerException, and how do I fix it?

(12个答案)


4年前关闭。




我正在做一个简单的Javax.swing程序,但是确实有一些错误。这是完整的代码:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Color;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class JFrameColore extends JFrame
{
public JFrameColore()
{
    this.setSize(600, 300);

    label_R = new JLabel();
    label_R.setText("R");

    label_G = new JLabel();
    label_G.setText("G");

    label_B = new JLabel();
    label_B.setText("B");

    text_R = new JTextField();
    text_G = new JTextField();
    text_B = new JTextField();

    button_COLORA = new JButton();

    Azusa azunyan = new Azusa();

    button_COLORA.addActionListener(azunyan);

    panel_COLORE = new JPanel();


    label_COLORA = new JLabel();

}

public final JLabel label_R, label_G, label_B;

public final JTextField text_R, text_G, text_B;

public final JButton button_COLORA;

public final JPanel panel_COLORE;

public final JLabel label_COLORA;

private class Azusa implements ActionListener
{

    @Override
    public void actionPerformed(ActionEvent e)
    {
        boolean colorable = true;
        if (!isNumber(text_R.getText()))
        {
            colorable = false;
            text_R.setText("ERR");
        }
        else if (Integer.getInteger(text_R.getText()) < 0
                || Integer.getInteger(text_R.getText()) > 255)
        {
            colorable = false;
            text_R.setText("ERR");
        }
        if (!isNumber(text_G.getText()))
        {
            colorable = false;
            text_G.setText("ERR");
        }
        else if (Integer.getInteger(text_G.getText()) < 0
                || Integer.getInteger(text_G.getText()) > 255)
        {
            colorable = false;
            text_G.setText("ERR");
        }
        if (!isNumber(text_B.getText()))
        {
            colorable = false;
            text_B.setText("ERR");
        }
        else if (Integer.getInteger(text_B.getText()) < 0
                || Integer.getInteger(text_B.getText()) > 255)
        {
            colorable = false;
            text_B.setText("ERR");
        }



        if (colorable)
        {
            panel_COLORE.setOpaque(true);
            panel_COLORE.setBackground(new Color(

                    Integer.getInteger(text_R.getText()),
                    Integer.getInteger(text_G.getText()),
                    Integer.getInteger(text_B.getText())
            ));
        }

    }

}

protected boolean isNumber(String gringo)
{
    for (int i = 0; i < gringo.length(); i++)
    {
        if (!Character.isDigit(gringo.charAt(i)))
        {
            return false;
        }
    }
    return true;
}

}


例如,当我做
else if (Integer.getInteger(text_R.getText()) < 0 || Integer.getInteger(text_R.getText()) > 255)

我收到一个NullPointerException,我真的不知道为什么,因为我检查了值是否为整数。我究竟做错了什么?对于那些在Swing方面更有经验的人来说,这很好。

最佳答案

您要执行Integer.parseInt()而不是Integer.getInteger()

else if (Integer.parseInt(text_R.getText())


oracle docs

这是一条SO帖子,解释getInteger()的作用-link

10-07 22:53