0

我目前正在尝试从 textArea 获取用户输入,将其放入数组中,然后将数组输出到 textField 中。我目前遇到了 nullPointerException。我猜这是因为我没有正确收集来自 textArea 的输入并将其放入数组中。请帮忙。最终,我将不得不使用 Bubble、Merge 和 Quick sort 对其进行排序,但我只想确保我正在获取输入并将其正确放入数组中。请不要给我关于排序的答案。谢谢你。

目前,我正在使用的区域在 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);


 }
}

目前,当我运行该程序并按下冒泡排序按钮时,它会冻结并且不执行任何操作。

所以我将此添加到我已更改的冒泡排序按钮区域中。

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

        }

    });

它目前仍然冻结,但我不确定它为什么会冻结。我需要做些什么才能从 inputArea 获取文本?

4

1 回答 1

0

在尝试访问它之前,您必须初始化数组。Java 中的数组是作为对象处理的,因此您必须array= new int [arrayLength]在访问它之前声明:(arrayLength 是您放入其中的变量)。

于 2013-11-10T17:55:49.757 回答