0

I'm trying to use a single array across multiple methods; when I try to print the values of the array after I have defined them in another method I get the error Exception in thread "main" java.lang.NullPointerException at shop.main(shop.java:130)

public static int[] discount;
public static double[] price;
public static String[] name;

public static void setup(Scanner input, String[] name, double[] price, int[] discount) {
    System.out.print("Please enter the number of items to setup shop: ");
    do {
        CheeseNum = input.nextInt();
        if (CheeseNum < 0) {
            System.out.print("Invalid Input. Enter a value > 0: ");
        }
    } while (CheeseNum < 0);
    System.out.printf("\n");

    discount = new int[CheeseNum];
    price = new double[CheeseNum];
    name = new String[CheeseNum];

    for (int i = 0; i < CheeseNum; i++){
        System.out.print("Enter the name of the " + numSuffix(i + 1) + " product: ");
        name[i] = input.next();
        System.out.printf("Enter the per package price of " + name[i] + ": ");
        price[i] = input.nextDouble();
        System.out.printf("Enter the number of packages ('x') to qualify for Special Discount (buy 'x' get 1 free) for " + name[i] + ", or 0 if no Special Discount offered:");
        discount[i] = input.nextInt();
    }
}

public static void buy(Scanner input, String[] name, int[] purchased) {
    purchased = new int[CheeseNum];
    for (int i = 0; i < CheeseNum; i++){
        System.out.printf("\nEnter the number of " + name[i] + " packages to buy: ");
        purchased[i] = input.nextInt();
        shopBuyIns = shopBuyIns + purchased[i];
    }
}

So whats happening is the user inputs the values for the array in setup, and when I try to use those values in buy it tells me the arrays are null.

2 Answers2

0

Whenever you create an array or object in java, java will allocate memory for the object you created and the variable name of this object is a pointer to a location in the memory where it allocated. In your function you created new array, which means you allocated another place in the memory for a new array, but since you did it inside the function scope, the array will only exist in there and when you are out of the function's scope java's garbage collector will delete all local variables of the function - and your new array as well! So now your variable is a pointer to a place in the memory that does not exists and you'r getting NullPointerException

Omri Attiya
  • 3,917
  • 3
  • 19
  • 35
0

This does not work the way you expect it to:

public static void buy(Scanner input, String[] name, int[] purchased) {
    purchased = new int[CheeseNum];
    ...
}

You, probably invoke this method like:

int[] outerPurchased;

buy(scanner, name, outerPurchased);

and expect that after returning from the function outerPurchased is set to the value you assign in buy.

But this does not work this way. Firstly, outerPurchased and purchased are different variables that store references to the actual array stored in the heap memory. In buy method the local variable purchased initially points to the same array as the variable that is passed to buy (or in your case it most probably is null).

Then purchases is modified and began to point to newly created array but outerPurchased is not modified. So after buy is finished outerPurchased is still null so you get NullPointerException.

You can fix this by either:

  1. create the array outside of buy and pass it to this method
  2. return the array created in buy
int[] buy(Scanner input, String[] name) {
    int[] purchased = new int[CheeseNum];
    ...
    return purchased;
}


/// and invoke like this

int[] outerPurchased;


outerPurchased = buy(scanner, name);