Grace
Grace

Reputation: 67

Java - Array outputing null

My program is supposed to output labels. All of the input works when I run it but the output is wrong and all that it outputs is null, for every part of the label except for the box number.

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): ");
    }

Upvotes: 1

Views: 90

Answers (2)

Paul Boddington
Paul Boddington

Reputation: 37655

The array nameAddressArray is declared twice. You have a static field

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

You also have a local variable with the same name in the main method.

String nameAddressArray[] = new String[7];

Your main method is putting values into the second array, whereas your getLabelData method is using the values from the static field, and these are all the initial value (null).

One way to solve this problem is to just get rid of the local variable. Then both parts of the code will use the same array.

Alternatively, you could get rid of the static field, and pass the array as a parameter to the getLabelData method. This is probably a better solution, as mutable static fields are generally not a good idea.

Upvotes: 2

Vishal Gajera
Vishal Gajera

Reputation: 4207

you just need to comment this line into Main method(),

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

Upvotes: 0

Related Questions