On 1/26/2010 8:31 PM, Wally Kolcz wrote:

I am perplexed. I have a Singleton called ShoppingCart. In it there is a single public variable called cart which is an ArrayCollection. I am trying to use it as the shopping cart.

Here is my Singleton:
/package com.earth.model
{
    import mx.collections.ArrayCollection;

    [Bindable]
    public final class ShoppingCart {

    private static var instance:ShoppingCart;

    public var cart:ArrayCollection;

    public function ShoppingCart(singletonEnforcer:MySingletonEnforcer) {
        if (singletonEnforcer == null) {
throw new Error ("ShoppingCart is a singleton class, use getInstance() instead");
        }
    }

        public static function getInstance():ShoppingCart {
            if (instance == null)
            instance = new ShoppingCart(new MySingletonEnforcer());
            return instance;
        }
    }
}

class MySingletonEnforcer {}/


I am trying to create a function to check to see if the cart has a product in it by productID. If not, then add a product Object. If it is there, update the record with the new quantity.

Here is the function.
/import com.earth.model.ShoppingCart;
public var cursor:IViewCursor;

public function addToCart(product:Object, qty:Number):void {
var cursor = ShoppingCart.getInstance().cart.createCursor();
                if (cursor.findAny(product.productID)){
                    cursor.current.qty += qty;
                }else{
                    var prod:Object = new Object;
                    prod.productName = product.productName;
                    prod.qty = qty;
                    prod.productID = product.productID;
                    ShoppingCart.getInstance().cart.addItem(prod);
                 }
                updateTotal();
            }/


When I try to invoke the function, it throws an error: *TypeError: Error #1009: Cannot access a property or method of a null object reference.*
Nevermind, figured it out. :-)

I have used this Singleton before and it worked fine storing a user's information. Please help.



Reply via email to