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??