我想知道你是否可以告诉我我的动作听众在哪里出错了?我将发布下面的代码和错误,它一直让我疯狂......
btnEdit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
EditItem editFrame = new EditItem(sc, orderTextField, numofItemsField, totalCostField);
}
});
package Assesment;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class EditItem {
private ShoppingCart sc;
private JTextArea orderTextField;
private JTextField numItemsField;
private JTextField totalCostField;
private JTextField orderNumberJTextField;
private JButton buttonEdit;
private JComboBox comOrderNumber;
private JTextField txtFieldName;
private JTextField txtFieldQuantity;
private JTextField txtFieldPrice ;
public EditItem(ShoppingCart sc, JTextArea orderTextField,
JTextField numItemsField, JTextField totalCostField) {
this.sc = sc;
this.orderTextField = orderTextField;
this.numItemsField = numItemsField;
this.totalCostField = totalCostField;
build();
addActionListeners();
}
private void build() {
// creates a main frame called editMainFrame1
JFrame editMainFrame1 = new JFrame("Edit");
editMainFrame1.setVisible(true);
editMainFrame1.setLayout(new GridLayout(5, 1));
editMainFrame1.setResizable(false);
editMainFrame1.setSize(300, 300);
JPanel orderNumberPanel = new JPanel(new GridLayout(1, 2));
JPanel nameNumberPanel = new JPanel(new GridLayout(1, 2));
JPanel quantityNumberPanel = new JPanel(new GridLayout(1, 2));
JPanel priceNumberPanel = new JPanel(new GridLayout(1, 2));
JPanel buttonNumberPanel = new JPanel(new GridLayout(1, 1));
JLabel entOrderNumber = new JLabel("Select Order Number");
Integer[] orderNumbersList = sc.getOrderNumbersList();
JComboBox comOrderNumber = new JComboBox(orderNumbersList);
orderNumberPanel.add(entOrderNumber);
orderNumberPanel.add(comOrderNumber);
JLabel entName = new JLabel("Enter New Name");
JTextField txtFieldName = new JTextField();
txtFieldName.setSize(20, 20);
nameNumberPanel.add(entName);
nameNumberPanel.add(txtFieldName);
JLabel entQuantity = new JLabel("Enter New Quantity");
JTextField txtFieldQuantity = new JTextField();
txtFieldQuantity.setSize(20, 20);
quantityNumberPanel.add(entQuantity);
quantityNumberPanel.add(txtFieldQuantity);
JLabel entPrice = new JLabel("Enter New Price");
JTextField txtFieldPrice = new JTextField();
txtFieldPrice.setSize(20, 20);
priceNumberPanel.add(entPrice);
priceNumberPanel.add(txtFieldPrice);
JButton buttonEdit = new JButton("Edit!");
buttonNumberPanel.add(buttonEdit);
// adding components to separate panels
editMainFrame1.add(orderNumberPanel);
editMainFrame1.add(nameNumberPanel);
editMainFrame1.add(quantityNumberPanel);
editMainFrame1.add(priceNumberPanel);
editMainFrame1.add(buttonNumberPanel);
}
private void addActionListeners() {
buttonEdit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
OrderItem x = sc.findItem(Integer.parseInt((String) comOrderNumber.getSelectedItem())) ;
sc.removeItem(x);
String newNameEntered = txtFieldName.getText();
int newQuantityEntered = Integer.parseInt((String) txtFieldQuantity.getText());
double newPriceEntered = Double.parseDouble((String) txtFieldPrice.getText());
int z = Integer.parseInt((String) comOrderNumber.getSelectedItem()) ;
System.out.println(""+newQuantityEntered+newNameEntered);
sc.addItem(z,newNameEntered,newQuantityEntered,newPriceEntered);
orderTextField.setText("3" + sc.getBasketContents());
numItemsField.setText(""+sc.getSize());
totalCostField.setText(""+sc.getTotalPrice());
}
});
}
}
Lines giving the errors : buttonEdit.addActionListener(new ActionListener() {
Line 95
addActionListeners(); Line 39
EditItem editFrame = new EditItem(sc, orderTextField, numofItemsField, totalCostField);
Line 252.
我一直在下面的错误:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Assesment.EditItem.addActionListeners(EditItem.java:95)
at Assesment.EditItem.<init>(EditItem.java:39)
at Assesment.Store$3.actionPerformed(Store.java:252)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2028)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2351)
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.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:272)
at java.awt.Component.processMouseEvent(Component.java:6382)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3275)
at java.awt.Component.processEvent(Component.java:6147)
at java.awt.Container.processEvent(Container.java:2083)
at java.awt.Component.dispatchEventImpl(Component.java:4744)
at java.awt.Container.dispatchEventImpl(Container.java:2141)
at java.awt.Component.dispatchEvent(Component.java:4572)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4619)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4280)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4210)
at java.awt.Container.dispatchEventImpl(Container.java:2127)
at java.awt.Window.dispatchEventImpl(Window.java:2489)
at java.awt.Component.dispatchEvent(Component.java:4572)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:710)
at java.awt.EventQueue.access$400(EventQueue.java:82)
at java.awt.EventQueue$2.run(EventQueue.java:669)
at java.awt.EventQueue$2.run(EventQueue.java:667)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
at java.awt.EventQueue$3.run(EventQueue.java:683)
at java.awt.EventQueue$3.run(EventQueue.java:681)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:680)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
它正在驱使我疯狂,因为我有其他行动Listerners似乎以同样的方式工作,而不是给我任何错误
答案 0 :(得分:1)
JButton buttonEdit = new JButton("Edit!");
应该是:
buttonEdit = new JButton("Edit!");
通过添加JButton
前缀,它实际上变成了一个局部变量,“隐藏”真实的前缀,它总是null
。
答案 1 :(得分:1)
使用build()
方法
JButton buttonEdit = new JButton("Edit!");
不应该
buttonEdit = new JButton("Edit!");
初始化私有字段?
答案 2 :(得分:0)
buttonEdit.addActionListener(new ActionListener() {
buttonEdit为null,因此您无法使用任何方法为null。看看这张图片(在Eclipse中调试): https://dl.dropboxusercontent.com/u/1987930/screenshot.png
为什么?
JButton buttonEdit = new JButton("Edit!"); //You set a local varaiable, not to class, so
// remove first word