Hello all. So I have a little problem with my school project. We are to create a snack machine with cookies and mints and interact with it using a GUI. We click the add Mints or Cookies button to add to the 'Snack Machine' and then click buy to purchase from the Snack Machine. A project.java file given to us(we MAY NOT change anything inside it) tests for a null list and if it exists it prints "Purchase fail". My problem is that the error box doesnt show up when I attempt to purchase without having added any Mints/Cookies, instead java shows a nullpointerexception error. Here are the codes:

package proj3;

import java.awt.Color;
import java.util.ArrayList;

public class SnackMachine {

    private ArrayList<Mints> mints;
    private ArrayList<Cookies> cookies;
    private Money money; 
    private Cookies c;
    private Mints m;

    public SnackMachine(){

    public void addCookies(CookieFlavors flavor, int nrcookies){
        for(int i = 0; i < nrcookies; i++){
            c = new Cookies(flavor);
    public void addMints(Color color, int nrmints){
        for(int i = 0; i < nrmints; i++){
            m = new Mints(color);
    public Cookies buyCookies(Money money){
        this.money = money;
        return cookies.remove(0);
    public Mints buyMints(Money money) {
    //  if(!mints.equals(null)){
        return mints.remove(0);
    public int getNrMints() {
        return mints.size();
    public int getNrCookies() {
        return cookies.size();
    public Money getCashOnHand() {
        return money;

The following is a code snippet from the provided project file:

private class BuyMints implements ActionListener
        public void actionPerformed(ActionEvent ae)
            // read the coins inserted
            int nrNickels, nrDimes, nrQrts;
            nrNickels = ( (Integer)spinNickels.getValue()).intValue();
            nrDimes = ( (Integer)spinDimes.getValue()).intValue();
            nrQrts = ( (Integer)spinQrts.getValue()).intValue();

            // buy the Mints, get Mints in return
            Mints m = snackMachine.buyMints(new Money(nrNickels, nrDimes, nrQrts));
            if ( m == null )    // sold out or not exact change         
                JOptionPane.showMessageDialog(mainFrame, "Mints purchase failed");

            else {
                // purchase successful
                RoundIcon mints = new RoundIcon(m.getColor(), 100);
                String msg = "Congratulation! You bought this package of Mints";
                JOptionPane.showMessageDialog(null, msg, 
                        "Mints Purchase", JOptionPane.INFORMATION_MESSAGE, mints);

            // reset coin spinners
            resetSpinners( );

My guesses are that there is supposed to be an array/arraylist in the Mint/Cookies file isntead of the SnackMachine file? If anyone wishes to view my other classes please just ask. Thank you all in advance and sorry if this is all long :P

Edited by tingwong

4 Years
Discussion Span
Last Post by tingwong

Never mind everyone! I managed to fix it. Thanks anyway everyone, until I stumble upon a nother error...LOL

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.