【发布时间】:2013-11-18 01:56:42
【问题描述】:
我正在尝试从用户那里接受 5 个文本字段中的数字,以便使用不同的方法(bubbleSort、mergeSort、quickSort)对它们进行排序。不幸的是,我一直抛出“java.lang.NullPointerException”。我环顾四周,我能找到的最接近我的问题的是NumberFormatException when attempting to parse string as an integer,但这仅仅是由于一个空白空间。我投了一个 .trim() 只是为了很好的衡量,但无济于事。
public class SortWindow {
private JFrame frame;
private JTextField textFieldInput;
private String[] list;
private int[] numList;
private static JTextArea textAreaOutput;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
SortWindow window = new SortWindow();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public SortWindow() {
initialize();
}
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
//Buttons
JButton buttonBubble = new JButton("Bubble");
buttonBubble.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textAreaOutput.setText("");
list = textFieldInput.getText().split(" ");
numList[0] = Integer.parseInt(list[0].trim());
for (int i = 0; i < list.length; i++){
numList[i] = Integer.parseInt(list[i]);
}
bubbleSort(numList);
}
});
buttonBubble.setBounds(12, 13, 115, 40);
frame.getContentPane().add(buttonBubble);
//Text Fields
textFieldInput = new JTextField(5);
textFieldInput.setBounds(177, 13, 243, 40);
frame.getContentPane().add(textFieldInput);
textFieldInput.setColumns(10);
textAreaOutput = new JTextArea();
textAreaOutput.setLineWrap(true);
textAreaOutput.setText("In the box above, enter 5 numbers separated by spaces.");
textAreaOutput.setEditable(false);
textAreaOutput.setBounds(177, 66, 243, 176);
frame.getContentPane().add(textAreaOutput);
}
}
list 是一个字符串数组,当我输入 1 2 3 4 5 (或任何其他组合 - 我什至尝试只输入一个数字并使用我的第二行查看发生了什么,但它仍然抛出异常)到我的 GUI 中的 textField,它将它正确地存储在字符串数组中,并且我可以使用 System.out.print() 成功地完美地打印其中的各个字符串。我的问题只有在我尝试使用以下四行中的任何一行时才会出现(我只包括第二行以确保我的 for 循环没有问题)。
如果需要,我可以提供我的其余代码,但我想我会从这个开始,因为我想不出任何其他会影响它的东西。
编辑:将我班级的大部分内容添加到 OP。第60行抛出异常,即:
numList[0] = Integer.parseInt(list[0].trim());
EDIT2:根据要求,这是我得到的。
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at SortWindow$2.actionPerformed(SortWindow.java:60)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
【问题讨论】:
-
你能写下你的整个错误代码行吗?它在哪个行号失败 - 它在
numList[0] = Integer.parseInt(list[0].trim());处吗?如果您能分享更多的课程内容,也会有所帮助。 -
我怀疑你的问题是所有代码都被注释掉了。
-
是的,就是你提到的那部分。或者 for 循环,如果我尝试只使用它。将编辑我的 OP 以包括我的课程中以某种方式相关的所有部分。
-
@Hot Licks - 它只是被注释掉以显示我的错误在哪里。抱歉,如果这具有误导性。
-
OP 已更新。我还从错误行中删除了 cmets。
标签: java eclipse user-interface textfield parseint