0

我在为 JButton 添加 ActionListener 时遇到问题,请帮助解决这个问题。

错误:

Exception in thread "main" java.lang.NullPointerException
at searchDB.searchDB(searchDB.java:11)
at Ramses.main(Ramses.java:35)

主类:Ramses.java

import javax.swing.JFrame;
import ...........;

public class Ramses {

public static void main(String[] args) {
    // TODO Auto-generated method stub

    Gui GuiObject = new Gui();
    GuiObject.Gui();

    searchDB searchObject = new searchDB();
    searchObject.searchDB(); //error here

}
}

声明 JButton 的类:Gui.java

import javax.swing.*;
import........;

public class Gui {
public static JButton btnUpdate;
public void Gui() {
JButton btnSearch = new JButton("Search");
    btnSearch.setBounds(463, 112, 91, 23);
    btnSearch.setVisible(true);
    pnUpper.add(btnSearch);

 }
}

实现JButton ActionListener的类:searchDB.java

import javax.swing.*;

public class searchDB{

public void searchDB(){

    HandlerClass handler = new HandlerClass();
    Gui.btnSearch.addActionListener(handler); //error in this line

}
private class HandlerClass implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent event) {
        // TODO Auto-generated method stub
        String cstmrSearch = (String) Gui.cbCustomer.getSelectedItem();
        System.out.println("Selected Customer is :"+cstmrSearch);
    }


}
}

任何想法这段代码有什么问题?

4

3 回答 3

2

堆栈跟踪是问题的一个很好的指标

从构造函数中删除void关键字Gui

public Gui() {

允许分配组件变量。另外删除JButton构造函数中的类型以确保变量没有被遮蔽

btnSearch = new JButton("Search");

另外:static字段被认为是糟糕的设计,请改用实例变量

于 2013-10-23T20:24:10.300 回答
0

如果您打算向Class Guiand添加构造函数Class searchDB,则不应添加 void 作为返回类型。

添加 void 将使其成为一种正常的方法。构造函数将没有返回类型。您也可以在构造函数中包含初始化代码,并且在创建类的对象时会进行此初始化。无需进行任何显式调用。

Class Gui {

public Gui(){
 //initialization code goes here
}

}

Class searchDB{

public searchDB(){
 //initialization code goes here
}
}
于 2013-10-23T20:47:10.530 回答
0

我会设计我的课程有点不同。我同意@Reimeus,您应该避免滥用静态字段,并且应该避免“伪”构造函数,即具有 void 返回类型的构造函数,因为构造函数应该没有返回类型。

具体来说,我会改变这个:

public class Ramses {
  public static void main(String[] args) {
    Gui GuiObject = new Gui();
    GuiObject.Gui();
    searchDB searchObject = new searchDB();
    searchObject.searchDB(); //error here
  }
}

对此:

public class Ramses {
  public static void main(String[] args) {
    Gui gui = new Gui();  // variables should begin with lower-case letter
    // GuiObject.Gui();  // not needed
    SearchDB searchObject = new SearchDB(gui); // pass GUI object in
     // searchObject.searchDB(); // don't need this
  }
}

从此更改 Gui:

public class Gui {
public static JButton btnUpdate;
public void Gui() {
JButton btnSearch = new JButton("Search");
    btnSearch.setBounds(463, 112, 91, 23);
    btnSearch.setVisible(true);
    pnUpper.add(btnSearch);

 }
}

对此:

public class Gui {
  private JButton btnUpdate; // make private and non-static
  public Gui() {
    // JButton btnSearch = new JButton("Search");
    //  btnSearch.setBounds(463, 112, 91, 23); // don't use absolute positioning
    //  btnSearch.setVisible(true);  // not needed

    btnUpdate = new btnUpdate("Search");
    pnUpper.add(btnSearch);
 }

 public AbstractButton getBtnUpdate() {
    return btnUpdate;
 }
}

并从以下位置更改 searchDB:

public class searchDB{

public void searchDB(){

    HandlerClass handler = new HandlerClass();
    Gui.btnSearch.addActionListener(handler); //error in this line

}
private class HandlerClass implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent event) {
        // TODO Auto-generated method stub
        String cstmrSearch = (String) Gui.cbCustomer.getSelectedItem();
        System.out.println("Selected Customer is :"+cstmrSearch);
    }


}
}

到:

public class SearchDB{  // class names begin with upper-case

  public SearchDB(Gui gui){  // no more pseudo-constructors! And pass in Gui object
    HandlerClass handler = new HandlerClass();
    gui.getBtnUpdate().addActionListener(handler); // call method
  }

  private class HandlerClass implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent event) {
        // String cstmrSearch = (String) Gui.cbCustomer.getSelectedItem();
        String cstmrSearch = gui.getSelectedCustomer(); // give Gui this method
        System.out.println("Selected Customer is :"+cstmrSearch);
    }

  }
}

(请参阅代码中的注释以获取解释)

于 2013-10-23T20:54:43.537 回答