gpt4 book ai didi

java - 我在面板上有一个用于按钮的 actionListener,获取空指针

转载 作者:行者123 更新时间:2023-12-01 14:41:07 27 4
gpt4 key购买 nike

我已经查看并测试了各种方法,但是这个面板(通过另一个面板组件调用)将看不到 actionListener 已设置。我正在 Netbeans 7.0 中工作。这是面板代码:

public class PanelAddClient extends javax.swing.JPanel {

private AppEventListener appEventListener;



public PanelAddClient() {
initComponents();
//paneClientDBTable = new PanelClientDBTable(sqlModule);


}

/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
java.awt.GridBagConstraints gridBagConstraints;

jToolBar1 = new javax.swing.JToolBar();
filler1 = new javax.swing.Box.Filler(new java.awt.Dimension(500, 0), new java.awt.Dimension(500, 0), new java.awt.Dimension(500, 32767));
jLabel11 = new javax.swing.JLabel();
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
textClientNo = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
textFName = new javax.swing.JTextField();
jLabel3 = new javax.swing.JLabel();
textLName = new javax.swing.JTextField();
jLabel4 = new javax.swing.JLabel();
textStreetAddress = new javax.swing.JTextField();
jLabel5 = new javax.swing.JLabel();
textCity = new javax.swing.JTextField();
jLabel6 = new javax.swing.JLabel();
comboStatebox = new javax.swing.JComboBox();
jLabel7 = new javax.swing.JLabel();
textZipCode = new javax.swing.JTextField();
jPanel2 = new javax.swing.JPanel();
jLabel8 = new javax.swing.JLabel();
textHomePhone = new javax.swing.JTextField();
jLabel9 = new javax.swing.JLabel();
textMobilePhone = new javax.swing.JTextField();
jLabel10 = new javax.swing.JLabel();
textEmail = new javax.swing.JTextField();
buttonAddClient = new javax.swing.JButton();

setLayout(new java.awt.BorderLayout());

jToolBar1.setBackground(new java.awt.Color(102, 0, 102));
jToolBar1.setFloatable(false);
jToolBar1.setRollover(true);
jToolBar1.add(filler1);

jLabel11.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
jLabel11.setForeground(new java.awt.Color(51, 255, 51));
jLabel11.setText("Add New Client Panel");
jToolBar1.add(jLabel11);

add(jToolBar1, java.awt.BorderLayout.PAGE_START);

jPanel1.setBackground(new java.awt.Color(204, 0, 204));

jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
jLabel1.setForeground(new java.awt.Color(255, 255, 255));
jLabel1.setText("Client Number:");

textClientNo.setToolTipText("");
textClientNo.setPreferredSize(new java.awt.Dimension(50, 15));

jLabel2.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
jLabel2.setForeground(new java.awt.Color(255, 255, 255));
jLabel2.setText("First Name :");
jLabel2.setToolTipText("");

textFName.setToolTipText("");

jLabel3.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
jLabel3.setForeground(new java.awt.Color(255, 255, 255));
jLabel3.setText("Last Name :");
jLabel3.setToolTipText("");

textLName.setToolTipText("");

jLabel4.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
jLabel4.setForeground(new java.awt.Color(255, 255, 255));
jLabel4.setText("Stree Address :");

textStreetAddress.setToolTipText("");

jLabel5.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
jLabel5.setForeground(new java.awt.Color(255, 255, 255));
jLabel5.setText("City :");

textCity.setToolTipText("");

jLabel6.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
jLabel6.setForeground(new java.awt.Color(255, 255, 255));
jLabel6.setText("State :");

comboStatebox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "AL", "AK", "MS", "CA", "TX", "OK", "GA", "SC", "TN", "NC", "NY", "IL", "LA", "IN", "WA", "ND", "SD", "MD", "CO", "MA", "RI" }));

jLabel7.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
jLabel7.setForeground(new java.awt.Color(255, 255, 255));
jLabel7.setText("Zip Code :");

textZipCode.setToolTipText("");

jPanel2.setBackground(new java.awt.Color(204, 0, 204));
jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Contact Information", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 12), java.awt.Color.white)); // NOI18N
jPanel2.setForeground(new java.awt.Color(255, 255, 255));
jPanel2.setLayout(new java.awt.GridBagLayout());

jLabel8.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
jLabel8.setForeground(new java.awt.Color(255, 255, 255));
jLabel8.setText("Phone Number :");
jPanel2.add(jLabel8, new java.awt.GridBagConstraints());

textHomePhone.setPreferredSize(new java.awt.Dimension(100, 20));
jPanel2.add(textHomePhone, new java.awt.GridBagConstraints());

jLabel9.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
jLabel9.setForeground(new java.awt.Color(255, 255, 255));
jLabel9.setText("Mobile Phone :");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
jPanel2.add(jLabel9, gridBagConstraints);

textMobilePhone.setPreferredSize(new java.awt.Dimension(100, 20));
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 1;
jPanel2.add(textMobilePhone, gridBagConstraints);

jLabel10.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
jLabel10.setForeground(new java.awt.Color(255, 255, 255));
jLabel10.setText("Email :");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 2;
jPanel2.add(jLabel10, gridBagConstraints);

textEmail.setToolTipText("");
textEmail.setMaximumSize(new java.awt.Dimension(100, 27));
textEmail.setMinimumSize(new java.awt.Dimension(100, 20));
textEmail.setPreferredSize(new java.awt.Dimension(100, 20));
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 2;
jPanel2.add(textEmail, gridBagConstraints);

buttonAddClient.setBackground(new java.awt.Color(204, 153, 0));
buttonAddClient.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
buttonAddClient.setForeground(new java.awt.Color(153, 0, 0));
buttonAddClient.setText("Add Client Now");
buttonAddClient.setFocusable(false);
buttonAddClient.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
buttonAddClient.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
buttonAddClient.setInheritsPopupMenu(true);
buttonAddClient.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
buttonAddClient.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
buttonAddClientActionPerformed(evt);
}
});

javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(textClientNo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jLabel4)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(textStreetAddress, javax.swing.GroupLayout.PREFERRED_SIZE, 352, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(textFName, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(31, 31, 31)
.addComponent(jLabel3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(textLName))
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jLabel5)
.addGap(18, 18, 18)
.addComponent(textCity, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(40, 40, 40)
.addComponent(jLabel6)
.addGap(18, 18, 18)
.addComponent(comboStatebox, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jLabel7)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(textZipCode, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(267, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(buttonAddClient)
.addGap(279, 279, 279))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(40, 40, 40)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(textClientNo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1))
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(textFName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel3)
.addComponent(textLName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4)
.addComponent(textStreetAddress, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel5)
.addComponent(textCity, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel6)
.addComponent(comboStatebox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel7)
.addComponent(textZipCode, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(30, 30, 30)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(buttonAddClient)
.addContainerGap())
);

add(jPanel1, java.awt.BorderLayout.CENTER);
}// </editor-fold>

private void buttonAddClientActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
//card.show(panelMain, PANEL_SQLDB_RESULT);

//SQLModule sqlModule = new SQLModule();
//this.appEventListener = appEventListener;

//JOptionPane.showMessageDialog(null, "Button Add Client Clicked On", "Client ADD Button Click", JOptionPane.ERROR_MESSAGE);

String clientNo = textClientNo.getText();
String clientFName = textFName.getText();
String clientLName = textLName.getText();

if (clientNo.equals("")) {
JOptionPane.showMessageDialog(this, "You must enter a Client Number (Client ID).", "Error", JOptionPane.ERROR_MESSAGE);
textClientNo.requestFocus();
return;
}


System.out.print("JUST BEFORE APP-EVENT " + clientNo + " was HIT!");

//appEventListener.buttonAddClientActionPerformed(clientNo, clientFName, clientLName);

if (appEventListener != null) {

System.out.print(" appEventListener " + clientNo + " was Secondly HIT!");
appEventListener.buttonAddClientActionPerformed(clientNo, clientFName, clientLName);
}

}
<小时/>

这就是我得到空指针(或缺少空指针)的地方..,由于 appEventListener 为空,因此该代码永远不会被执行。

<小时/>
if (appEventListener != null) {

System.out.print(" appEventListener " + clientNo + " was Secondly HIT!");
appEventListener.buttonAddClientActionPerformed(clientNo, clientFName, clientLName);
}

异常跟踪:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at com.zflex.cims.panels.PanelAddClient.buttonAddClientActionPerformed(PanelAddClient.java:297)
at com.zflex.cims.panels.PanelAddClient.access$000(PanelAddClient.java:16)
at com.zflex.cims.panels.PanelAddClient$1.actionPerformed(PanelAddClient.java:181)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6038)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3260)
at java.awt.Component.processEvent(Component.java:5803)
at java.awt.Container.processEvent(Container.java:2058)
at java.awt.Component.dispatchEventImpl(Component.java:4410)
at java.awt.Container.dispatchEventImpl(Container.java:2116)
at java.awt.Component.dispatchEvent(Component.java:4240)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)
at java.awt.Container.dispatchEventImpl(Container.java:2102)
at java.awt.Window.dispatchEventImpl(Window.java:2429)
at java.awt.Component.dispatchEvent(Component.java:4240)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)

就在 APP-EVENT 6565 即将结束之前

<小时/>

这来自抽象的App Listener模块

public interface AppEventListener {

public void buttonAddClientActionPerformed(String clientNo, String clientFName, String clientLName);

//void buttonDisplayRawLDAPStatsActionPerformed();
}

最佳答案

您声明了事件监听器,但从未像这样初始化它:

private AppEventListener appEventListener = new AppEventListener();

在使用之前声明它(例如在 initComponet() 中)

关于java - 我在面板上有一个用于按钮的 actionListener,获取空指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15948499/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com