我试图从用户接受textField中的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,它将它正确地存储在String数组中,并且我可以使用System.out.print()在其中成功地打印单个字符串。我的问题只出现在我尝试使用以下四行中的任何一行时(我只包括第二行以确保我的for循环没有问题)。
如果需要,我可以提供其余的代码,但我想我会从这开始,因为我无法想到任何会影响它的其他内容。
编辑:我的课程中增加了一个更大的部分。第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)
答案 0 :(得分:1)
您的numList大小可能尚未初始化。你可以尝试这样,看看它没关系:
int[] numList=new int[5];
String[] list = textFieldInput.getText().split(" ");
for (int i = 0; i < list.length; i++){
numList[i] = Integer.parseInt(list[i]);
}
System.out.println(Arrays.toString(numList));
答案 1 :(得分:0)
该异常意味着列表中的Item为Empty(值为null),检查列表项是否存在问题,而不是解析部分! 试试:
System.out.println(list[0]);
并观看输出。