Java - 输出null的数组

时间:2015-11-28 23:22:41

标签: java arrays swing user-interface debugging

我的程序应该输出标签。当我运行它时,所有输入都有效,但输出错误,输出的所有内容都是null,标签的每个部分除了盒号。

import javax.swing.JOptionPane;

public class MailOrderpractice {

    static String nameAddressArray[] = new String[7];

    public static void main(String[] args) {
        // declare variables
        String nameAddressArray[] = new String[7];
        String numBoxesInput;
        int numBoxes;
        String enterAnother = "Y";
        int counter;

        getLabelData();

        numBoxesInput = JOptionPane.showInputDialog("Enter number of boxes in the order:");
        numBoxes = Integer.parseInt(numBoxesInput);

        // begin outer loop logic that determines when user is finished entering mail orders
        while (enterAnother.equalsIgnoreCase("Y")) {
            counter = 1;
            // begin the inner loop to display a label and increment the counter
            while (counter <= numBoxes) {
                System.out.println(nameAddressArray[0] + " " + nameAddressArray[1] + " " + nameAddressArray[2]);
                System.out.println(nameAddressArray[3]);
                System.out.println(nameAddressArray[4] + ", " + nameAddressArray[5] + " " + nameAddressArray[6]);
                System.out.println("Box " + counter + " of " + numBoxes);
                System.out.println();
                counter = counter + 1;
            }

            enterAnother = " "; // initialize the variable to something other than "Y" before sending the prompt
            enterAnother = JOptionPane.showInputDialog("Do you want to produce more labels? Y or N");

            while (!enterAnother.equalsIgnoreCase("Y") && !enterAnother.equalsIgnoreCase("N")) {
                enterAnother = JOptionPane.showInputDialog(null, "Invalid Response. Please enter Y or N.",
                    "DATA ENTRY ERROR", JOptionPane.ERROR_MESSAGE);
            } // end while

            if (enterAnother.equalsIgnoreCase("Y")) {
                getLabelData();
                numBoxesInput = JOptionPane.showInputDialog("Enter number of boxes in the order:");
                numBoxes = Integer.parseInt(numBoxesInput);
            } // end if
        } // end while

        System.exit(0);
    }

    public static void getLabelData() {
        nameAddressArray[0] = JOptionPane.showInputDialog("Enter title (Mr., Ms., Dr., etc.): ");
        nameAddressArray[1] = JOptionPane.showInputDialog("Enter first name: ");
        nameAddressArray[2] = JOptionPane.showInputDialog("Enter lastname: ");
        nameAddressArray[3] = JOptionPane.showInputDialog("Enter street address: ");
        nameAddressArray[4] = JOptionPane.showInputDialog("Enter city: ");
        nameAddressArray[5] = JOptionPane.showInputDialog("Enter state (IL, MO, etc.): ");
        nameAddressArray[6] = JOptionPane.showInputDialog("Enter zip (e.g., 62025): ");
    }

2 个答案:

答案 0 :(得分:2)

数组nameAddressArray被声明两次。您有一个static字段

static String nameAddressArray[] = new String[7];

您在main方法中也有一个同名的局部变量。

String nameAddressArray[] = new String[7];

您的main方法正在将值放入第二个数组,而您的getLabelData方法正在使用static字段中的值,这些都是初始值({{1 }})。

解决这个问题的一种方法是摆脱局部变量。然后代码的两个部分将使用相同的数组。

或者,您可以删除静态字段,并将数组作为参数传递给null方法。这可能是一个更好的解决方案,因为可变静态字段通常不是一个好主意。

答案 1 :(得分:0)

您只需要将此行注释到Main方法(),

Malformed XML: no element found