1

I'm using greedy algorithm to calculate change, I want to save the set of the coins i've used to a new aray, however I :

Exception in thread "main" java.lang.NullPointerException
    at lacy_lab_7b_2015.Lacy_Lab_7b_2015.main(Lacy_Lab_7b_2015.java:36)
Java Result: 1

from the complier.

    public class Lacy_Lab_7b_2015 {

public static Scanner input = new Scanner(System.in);// scanner object to allow user input
public static int amount;// global variable defined by user to be used to have certain value of coins added so that they wll equal this value
public static int count = 0;
final public static int[] FACE_VALUES_OF_COINS = {100, 50, 25, 10, 5, 1};
public static int[] coinsUsed;

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // TODO code application logic here
    amount = userAmount();
   // coinsUsed = coinsUsed(FACE_VALUES_OF_COINS,amount);

    for (int i = 0; i < FACE_VALUES_OF_COINS.length; i++) {
        int j = 0;
        if (FACE_VALUES_OF_COINS[i] < amount) {
            int count = amount / FACE_VALUES_OF_COINS[i];
           coinsUsed[j] = FACE_VALUES_OF_COINS[i];
            amount -= count * FACE_VALUES_OF_COINS[i];
        }//end if
    }//end for
    for (int i = 0; i < coinsUsed.length; i++) {
        System.out.println(coinsUsed[i]);
    }
}//end main
peterchen
  • 40,917
  • 20
  • 104
  • 186

1 Answers1

1

I whould suggest you to write

public static int[] coinsUsed;

as

public static int[] coinsUsed = new int[5];

Naz
  • 390
  • 1
  • 4
  • 15