0

I am currently trying to take user input from a textArea, place it into an array and then output the array in a textField. I am running into a nullPointerExeception currently. I am guessing it is because I am not gathering the input from the textArea correctly and placing it into the array. Please help. Eventually I am going to have to sort it out with Bubble, Merge, and Quick sort but I just want to make sure that I am taking the input and placing it into the array correctly. Please do NOT give me the answer on the sorting. Thank you.

Currently the area that I am working with is within the Bubble Sort JButton.

import java.awt.EventQueue;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;

public class Sorting {

private JFrame frame;
private JTextArea inputArea;
private JTextField outputArea;
List<String> list = new ArrayList<String>();
Scanner input = new Scanner(System.in);
int array[];
int inputNumber = 0;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Sorting window = new Sorting();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public Sorting() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 450, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setTitle("Sorting");
    frame.getContentPane().setLayout(null);



    JButton bubbleButton = new JButton("Bubble Sort");
    bubbleButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            inputArea.getText();
            array[inputNumber] = input.nextInt();

            printArray(array);
        }

        private void printArray(int[] array) {
            int n = inputNumber;
            for(int i = 0; i < n; i++){
                outputArea.setText(array[i] + " ");
            }

        }
    });
    bubbleButton.setBounds(10, 211, 114, 23);
    frame.getContentPane().add(bubbleButton);

    JButton mergeButton = new JButton("Merge Sort");
    mergeButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

        }
    });
    mergeButton.setBounds(305, 211, 114, 23);
    frame.getContentPane().add(mergeButton);

    JButton quickButton = new JButton("Quick Sort");
    quickButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

        }
    });
    quickButton.setBounds(163, 211, 114, 23);
    frame.getContentPane().add(quickButton);

    inputArea = new JTextArea();
    inputArea.setBounds(10, 36, 414, 51);
    frame.getContentPane().add(inputArea);

    outputArea = new JTextField();
    outputArea.setEditable(false);
    outputArea.setBounds(10, 98, 414, 59);
    frame.getContentPane().add(outputArea);
    outputArea.setColumns(10);

    JLabel label = new JLabel("Please Enter 5 Numbers");
    label.setHorizontalAlignment(SwingConstants.CENTER);
    label.setBounds(10, 11, 414, 14);
    frame.getContentPane().add(label);


 }
}

Currently when I run this program and press the Bubble Sort button it freezes and doesn't do anything.

So I added this into the Bubble Sort button area to which I have changed.

 JButton bubbleButton = new JButton("Bubble Sort");
    bubbleButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            ArrayList<Integer> list = new ArrayList<Integer>();
            while (input.hasNext()) {
                list.add(input.nextInt());
            }

            for (int i = 0; i < 5; i++) {
                outputArea.setText(list.toString());
            }

        }

    });

It is still currently freezing but I am unsure as to why it would be freezing. Is there something I need to do to getText from inputArea??

  • you never initialize your array, or do you? – kadhonn Nov 10 '13 at 17:16
  • 1
    The stack trace of the exception tells you at which line it happens, which should tell you which variable is null. – JB Nizet Nov 10 '13 at 17:21
  • So I am guessing that I am not even adding the input into the array? for(int i = 0; i < array.length; i++){ array[i] = input.nextInt(); } In the for loop when it tries to run that line with array.length is when it throws the exception. – user2929005 Nov 10 '13 at 17:24
  • 1
    Don't guess. Read the stack trace. And post it so that we can do the same. – JB Nizet Nov 10 '13 at 17:25
  • Jep, I'm pretty sure `array.length` throws the Exception... initialize your array! – kadhonn Nov 10 '13 at 17:32
  • OK. So I'm assuming that line 52 of Sorting.java (first line of the stack trace) is `for(int i = 0; i < array.length; i++){`. What could possibly be null in this line? Do you see anywhere in the code where you assign a value to `array`? – JB Nizet Nov 10 '13 at 17:35
  • No I do not am currently working on trying to fix that problem. Now for placing the input into the array is it better to place the input outside of the ActionListener or should it stay within the ActionListener? Considering that I have to create the three buttons that will sort the array using the Bubble, Merge, and Quick sorting methods. – user2929005 Nov 10 '13 at 17:41
  • if "placing the input" is the same with every sorting algorithm, it should only be implemented one time -> place it into your Sorting-class – kadhonn Nov 10 '13 at 17:51
  • @user2929005 wherever you're getting the user input for the size, just initialize the array in there `array = new int[parsedUserInput];` – Paul Samsotha Nov 10 '13 at 20:05
  • so I am guessing I do not have a clue as to where I would be getting the user input size. – user2929005 Nov 10 '13 at 20:13

1 Answers1

0

You have to initialize the array, before you try to access it. Arrays in Java are handled as objects so you have to state: array= new int [arrayLength] (arrayLength is a variable you put in there) before you access it.

G_J
  • 314
  • 3
  • 6
  • But I don't want to set the arrayLength. I want the user to be able to put in how ever many integers they want and the program is going to determine how long the array is. – user2929005 Nov 10 '13 at 18:00
  • Then you have to use some kind of dynamic growing data structure (arrays are of static size after intitializing them). An [ArrayList](http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html) is exactly what you need. (But be aware that you can't store primitive types in there, so you have to use the wrapper objects ([Integer](http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html) instead of int) for them.) This should definitely help you and get the task done. – G_J Nov 10 '13 at 18:33
  • So should I do List list = new ArrayList(); – user2929005 Nov 10 '13 at 19:39
  • 1
    Correct, but you have to initialize the ArrayList outside of the listener. In the code added above everytime the actionPerformed method is triggered a new ArrayList is instantiated. You have to change this and the line, where you add the user input to: `ArrayList list = new ArrayList(); JButton bubbleButton = new JButton("Bubble Sort"); bubbleButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { list.add(Integer.valueOf(input.nextInt())); }});` Please accept my answer, if this was helpful. – G_J Nov 10 '13 at 20:29
  • Ok understand that but now trying to output the integers in the outputArea. Where do I actually setText or append the output. I have tried outputArea.setText(list) inside a for loop but that doesn't do anything correctly. I am still unable to determine the length that the user inputs to set the length of the ArrayList – user2929005 Nov 10 '13 at 20:50
  • 1
    You have to use: `list.get(index).toString()` So you are getting the element at the specified position from the list as a String. – G_J Nov 10 '13 at 21:00
  • Can I use for(int I = 0; I < list.indexOf(inputArea); I++){ NOT SURE WHAT TO PLACE HERE} if I type outputArea.setText or append I know that doesn't work. – user2929005 Nov 10 '13 at 21:06
  • 1
    You have to use i – G_J Nov 10 '13 at 21:09
  • yes it helps but also how can I set the text correctly because I cant figure that out. – user2929005 Nov 10 '13 at 21:10
  • like say the user inputs 5 7 3 9 10 in the JTextArea what you have told me to do should set the length of the array to 5. But now how do I get the text from the textArea and output it again in the outputArea. I will worry about the sorting myself I am just unsure as to what I need to do to gather the text that was inputed. – user2929005 Nov 10 '13 at 21:13
  • 1
    What do you want to output? The sequence of numbers? If so you can loop over the list and append each list entry to a String and set this String as text for you output TextView. – G_J Nov 10 '13 at 21:14
  • Currently what I want to display is just the numbers that the user inputs. I want to eventually sort them and then output them to the outputArea. – user2929005 Nov 10 '13 at 21:28