JButton ActionListener出错

时间:2013-10-23 20:22:58

标签: java swing jbutton actionlistener

我在为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);
    }


}
}

任何想法这个代码中的问题是什么?

3 个答案:

答案 0 :(得分:2)

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

void

的构造函数中删除Gui关键字
public Gui() {

允许分配组件变量。另外,删除构造函数中的JButton类型以确保变量不是shadowed

btnSearch = new JButton("Search");

除此之外:static字段被认为是设计不佳,而是使用实例变量

答案 1 :(得分:0)

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

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

Class Gui {

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

}

Class searchDB{

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

答案 2 :(得分: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);
    }

  }
}

(见代码中的注释以供解释)