【问题标题】:Attempt to parseInt from a String throws "java.lang.NullPointerException"尝试从字符串 parseInt 抛出“java.lang.NullPointerException”
【发布时间】: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


【解决方案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));

【讨论】:

  • 现在我觉得很傻 - 添加 =new int[5] 就可以了。非常感谢!
【解决方案2】:

该异常意味着您列表中的项目为空(值为 null),请检查您的列表项目是否存在问题,而不是在解析部分! 试试看:

   System.out.println(list[0]);

并观察输出。

【讨论】:

  • 在我的 textField 中输入“1”(或 1 2 3... 等),然后打印 list[0] 打印 1,没有错误。
  • 尝试 split("\\s") 而不是 split(" ") / 或 split("\\s+") 如果你想获得额外的空间..
  • 原来我的问题是我忽略了一些小事。我会尝试你刚刚添加的内容。谢谢。
猜你喜欢
  • 1970-01-01
  • 2012-02-05
  • 1970-01-01
  • 2014-06-29
  • 2020-09-15
  • 1970-01-01
  • 2018-04-24
  • 1970-01-01
  • 2013-12-07
相关资源
最近更新 更多