Question

I'm attempting to accept 5 numbers in a textField from the user, in order to sort them using different methods (bubbleSort, mergeSort, quickSort). Unfortunately, I keep having "java.lang.NullPointerException" thrown. I've looked around a bit, and the closest I could find to my issue is NumberFormatException when attempting to parse string as an integer, but that was simply due to an empty space. I threw in a .trim() just for good measure though, to no avail.

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 is an array of Strings, and when I enter 1 2 3 4 5 (or any other combination - I even tried just entering a single digit and seeing what happened using my second line, but it still threw the exception) into the textField in my GUI, it stores it correctly within the String array, and I can successfully print the individual Strings within it perfectly fine using System.out.print(). My problem only arises when I attempt to use any of the following four lines (I only included the second line to make sure there wasn't something wrong with my for loop).

I can provide the rest of my code if needed, but I figured I'd start with this, since I can't think of anything else that would be affecting it.

EDIT: Added a much larger part of my class to the OP. The exception is thrown in line 60, which is:

numList[0] = Integer.parseInt(list[0].trim());

EDIT2: As requested, this is what I am given.

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)
Was it helpful?

Solution

your numList size might not been initialized. you can try like this and see if its okay:

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

OTHER TIPS

That Exception means that the Item in your list is Empty (has value of null), check your list items the problem is there, and not in the parsing part! try :

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

and watch the output.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top