Note that there are some explanatory texts on larger screens.

plurals
  1. POTrying to print Array
    primarykey
    data
    text
    <p>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.</p> <p>Currently the area that I am working with is within the Bubble Sort JButton.</p> <pre><code>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&lt;String&gt; list = new ArrayList&lt;String&gt;(); 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 &lt; 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); } } </code></pre> <p>Currently when I run this program and press the Bubble Sort button it freezes and doesn't do anything.</p> <p>So I added this into the Bubble Sort button area to which I have changed.</p> <pre><code> JButton bubbleButton = new JButton("Bubble Sort"); bubbleButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { ArrayList&lt;Integer&gt; list = new ArrayList&lt;Integer&gt;(); while (input.hasNext()) { list.add(input.nextInt()); } for (int i = 0; i &lt; 5; i++) { outputArea.setText(list.toString()); } } }); </code></pre> <p>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??</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload