-1

我构建了一个接口,当我调用 actionPerformed 方法时,它给了我错误。

我的方法:

public void actionPerformed(ActionEvent evento){
    Button active;
    active = (Button) evento.getSource(); //line 144
    if(active==botonSalir) 
        mainF.dispose();
    ...
}

主要方法:

public static void main(String [] args){
    InterfaceE objetoM = new InterfaceE();
    objetoM.actionPerformed(); //line 195
}

我得到的错误是:

Exception in thread "main" java.lang.NullPointerException
    at InterfaceE.actionPerformed(InterfazceE.java:144)
    at InterfaceE.main(InterfaceE.java:195)

中的参数应该是什么InterfaceE()

4

1 回答 1

-1

您必须在Interfaz未分配 Interfaz 对象时调用方法,或者它必须在actionPerformed()方法内为 null。objectM不是null可以确定的,因为我可以看到代码。但是您还没有粘贴actionPerformed()方法的代码。让我们看看方法中的代码,actionPerformed()以便我们可以给你确切的答案。

有问题的编辑后更新。

你没有得到compile time错误,因为你没有调用无参数acionPerformed方法吗?actionPerformed必须采用实现方法的类anonymous 对象。您没有在方法中发送匿名对象作为参数。您的代码在第 1 行吗?195的样子,ActionEventgetSource()ActionEventactionPerformed

objetoM.actionPerformed(new ActionEvent(){ //this is your line 195 for anonymous object.
     public ReturnType getSource(){
           // some code
     }
}
);
于 2013-03-17T09:10:13.503 回答