繁体   English   中英

JButton ActionListener出错

[英]Error for 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);
    }


}
}

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

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

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

public Gui() {

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

btnSearch = new JButton("Search");

旁白: static字段被认为是设计不佳,而是使用实例变量

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

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

Class Gui {

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

}

Class searchDB{

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

我会设计我的课程有点不同。 我同意@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);
    }

  }
}

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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