Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JFrame中的NullPointerException_Java_Swing_Nullpointerexception_Jframe - Fatal编程技术网

Java JFrame中的NullPointerException

Java JFrame中的NullPointerException,java,swing,nullpointerexception,jframe,Java,Swing,Nullpointerexception,Jframe,我有一个类目录来存储有关它的信息。在这里,我只使用了两种方法: public void setCataName(String n) {cataName = n;} public String getCataName() {return cataName;} 这是我的JFrame的代码: public class AddCataFrame extends JFrame { JLabel lname; JTextField tname; Catalogu

我有一个类
目录
来存储有关它的信息。在这里,我只使用了两种方法:

public void setCataName(String n)
    {cataName = n;}

public String getCataName()
    {return cataName;}
这是我的JFrame的代码:

public class AddCataFrame extends JFrame
{
    JLabel lname; 
    JTextField tname;
    Catalogue catalogue;

    AddCataFrame()
    {
        super("Add New Catalogue");
        setLayout(new FlowLayout());

        lname = new JLabel("Name:", SwingConstants.LEFT);

        tname = new JTextField(15);

        textListener t = new textListener();
        tname.addActionListener(t);

        add(lname);
        add(tname);
    }

    class textListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {   
            //get the name from the textField after entered by user
            //then set it to the name of catalogue.
            //This is the place give me NullPointerException error.
            catalogue.setCataName(tname.getText()); 

            JOptionPane.showMessageDialog(null,catalogue.getCataName());
        }
    }
}

我不明白为什么给我一个NullPointerException。请帮助我。

在哪里初始化目录变量?你没有。解决方案:在尝试使用它之前先初始化它

e、 g

这仅声明变量:

Catalogue catalogue;
这将声明并初始化它:

Catalogue catalogue = new Catalogue();
或者,如果要在创建时使用传递到类中的目录对象,则可以初始化变量:

AddCataFrame(Catalgogue catalogue)
{
    super("Add New Catalogue");
    this.catalogue = catalogue; // *************

    setLayout(new FlowLayout());

    lname = new JLabel("Name:", SwingConstants.LEFT);

    tname = new JTextField(15);

    textListener t = new textListener();
    tname.addActionListener(t);

    add(lname);
    add(tname);
}


更重要的是,您需要学习如何调试NPE(NullPointerException)的一般概念。您应该仔细检查抛出它的行,找出哪个变量为null,然后回溯到代码中以了解原因。相信我,您会一次又一次地遇到这些问题。

在哪里初始化目录变量?你没有。解决方案:在尝试使用它之前先初始化它

e、 g

这仅声明变量:

Catalogue catalogue;
这将声明并初始化它:

Catalogue catalogue = new Catalogue();
或者,如果要在创建时使用传递到类中的目录对象,则可以初始化变量:

AddCataFrame(Catalgogue catalogue)
{
    super("Add New Catalogue");
    this.catalogue = catalogue; // *************

    setLayout(new FlowLayout());

    lname = new JLabel("Name:", SwingConstants.LEFT);

    tname = new JTextField(15);

    textListener t = new textListener();
    tname.addActionListener(t);

    add(lname);
    add(tname);
}


更重要的是,您需要学习如何调试NPE(NullPointerException)的一般概念。您应该仔细检查抛出它的行,找出哪个变量为null,然后回溯到代码中以了解原因。相信我,您会一次又一次地遇到这些问题。

您的变量目录已定义,但从未初始化。这就是为什么你会有例外


您应该有一行,如
catalog=newcatalog()

您的变量目录已定义,但从未初始化。这就是为什么你会有例外


您应该有一行,如
catalog=newcatalog()
您没有初始化
catalog
。你应该改变

Catalogue catalogue;

默认情况下,非基本字段初始化为
null
,因此第行出现
NullPointerException

catalogue.setCataName(tname.getText());

您没有初始化
目录
。你应该改变

Catalogue catalogue;

默认情况下,非基本字段初始化为
null
,因此第行出现
NullPointerException

catalogue.setCataName(tname.getText());

你必须初始化目录

Cataloge  catalog= new Cataloge();

你必须初始化目录

Cataloge  catalog= new Cataloge();

您是否已初始化
目录
?请参阅。这可能是您将面临的最常见的异常,在大多数情况下,IME是最容易捕获(修复)的异常之一。所以,学会自己调试。我犯了多么大的错误!!很抱歉浪费您的时间。您是否已初始化
目录
?请参阅。这可能是您将面临的最常见的异常,在大多数情况下,IME是最容易捕获(修复)的异常之一。所以,学会自己调试。我犯了多么大的错误!!对不起,浪费了你的时间。我真是个笨蛋,忘了基本的事情,谢谢你。我会更加小心地进一步编码。我是这样的傻瓜,忘记基本的事情,谢谢你。在进一步编码时,我会更加小心。