gpt4 book ai didi

java - 尝试从字符串中解析Int会抛出 "java.lang.NullPointerException"

转载 作者:行者123 更新时间:2023-12-01 13:50:00 25 4
gpt4 key购买 nike

我试图在文本字段中接受用户的 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 中的文本字段将其正确存储在字符串数组中,并且我可以使用 System.out.print() 完美地成功打印其中的各个字符串。仅当我尝试使用以下四行中的任何一行时,我的问题才会出现(我只包含第二行以确保我的 for 循环没有问题)。

如果需要,我可以提供其余的代码,但我想我应该从这个开始,因为我想不出还有什么会影响它。

编辑:将我类(class)的大部分内容添加到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 大小可能尚未初始化。你可以尝试这样看看是否可以:

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));

关于java - 尝试从字符串中解析Int会抛出 "java.lang.NullPointerException",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20038809/

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