简体   繁体   中英

Null pointer when calling a method in another class

I have an input panel and want to call a method in another class when the button is pressed but am getting a null pointer exception. Was hoping someone might be able to tell me what I'm doing wrong?

Here is the code causing it:

public void actionPerformed(ActionEvent ae)
    {
        if (ae.getSource() == resultsButton)
        {
            jbTour.processAdditionalResult();
        }

    }


public void processAdditionalResult()
    {
        System.out.println("button pressed");
    }

您需要初始化jbTour: jbTour = new JBTourObject() (或其他),以便在调用actionPerformed方法之前它不为null。

I think I can tell even without the stack trace: jbTour is null. You've declared a name of that reference type in the class that implements the ActionListener interface, but you never initialized it to point a new reference.

If this throws a NullPointerException

jbTour.processAdditionalResult();

that means jbTour has not been set or is null .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM