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.