CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   Java and JSP (http://www.codingforums.com/forumdisplay.php?f=54)
-   -   java.lang.NullPointerException - Help (http://www.codingforums.com/showthread.php?t=282258)

abell12 11-15-2012 03:21 PM

java.lang.NullPointerException - Help
 
Having problems with the goToCheckout method at the bottom.
I am getting this error:
Code:

java.lang.NullPointerException
        at Member.goToCheckout(Member.java:86)

I know name has got a value because when i inspect the object the names has my name in there.
The payment must have a value because you have to type an amount in.
Can anyone help me out here.

Code:

public class Member
{
    // instance variables
    private String name;
    private String id;
    private String pinNumber;
    private Store store;
    private Item item;
    private int money;

    /**
    * Constructor for objects of class Member
    */
    public Member(String newName, String newId, String newPinNumber, Store store)
    {
        if(newId.length() <= 10)
        {
            name = newName;
            id = newId;
           
            if(newPinNumber.length() == 4)
            {
                pinNumber = newPinNumber;
            }
            else
            {
                pinNumber = "0";
                System.out.println("Your pin number must be 4 characters");
            }
        }
        else
        {
            name = newName;
            id = null;
            pinNumber = newPinNumber;
            money = 500;
            System.out.println("Your ID must be less than 10 characters");
        }
    }
   
    /**
    * Get the members name
    */
    public String getName()
    {
        return name;
    }
   
    /**
    * Get the members id
    */
    public String getId()
    {
        return id;
    }
   
    /**
    * Get the members pin number
    */
    public String getPinNumber()
    {
        return pinNumber;
    }
   
    /**
    * Get the item
    */
    public Item selectItem(String itemName, int itemPrice)
    {
        Item item = new Item(itemName, itemPrice);
       
        System.out.println(name + " : I've selected " + itemName + " at " + itemPrice + "p");
       
        return item;
    }
   
    /**
    * Go to checkout
    * Enter money in pounds
    */
    public void goToCheckout(double payment)
    {
        store.checkout(payment, name);
    }
}


alykins 11-15-2012 05:38 PM

You never substantiate store, you just declare it. It's not a datatype so it needs to be substantiated- you've only created a singleton
Code:

private Store store; //singleton
.......
//elsewhere
private someFxn()
{
  store = new Store();
  // now store is usable
}

which let's you do things like
Code:

private Store store;
.....
private someFxn()
{
  for(int i=0; i<10; i++)
  {
    store = new Store();
    using (store)
    {
        /// do something
    }
  }
}

without throwing an error (note idk what the java equivalent is to using in C#)

because if you did this
Code:

private Store store = new store();
.....
private someFxn()
{
  for(int i=0; i<10; i++)
  {
    using (store)
    {
        /// do something
    }
  }
}

it would go through the loop once, destroy it and then throw a null exception

Fou-Lu 11-15-2012 07:28 PM

Simple problem. Store is null, you don't assign it anywhere in your constructor, so therefore you cannot call store.checkout in member.goToCheckout.
If you ran that through a debugger, you'd see that it is null when requested (as the error also specifies).

abell12 11-15-2012 07:52 PM

Ok. Its a null value, but when I inspect the store object it has got a name.
So how can I get the store to contain the name of the store?

Heres my store class code, if you need it.

Code:

public class Store
{
    // instance variables
    private String storeName;
    private int total;
    private Member member;

    /**
    * Constructor for objects of class Store
    */
    public Store(String newStoreName)
    {
        storeName = newStoreName;
        total = 0;
    }
   
    /**
    * Register a member
    */
    public Member memberRegister(String newName, String newId, String newPinNumber)
    {
        String name = newName;
        String id = newId;
        String pinNumber = newPinNumber;
       
        Member member = new Member(name, id, pinNumber, this);
       
        System.out.println(storeName + ": Welcome " + name + " (id: " + id + ")");
       
        return member;
    }
   
    /**
    * Checkout
    */
    public void checkout(double payment, String member)
    {
        System.out.println(payment + " & " + member);
    }
}


Fou-Lu 11-15-2012 08:07 PM

Where are you attempting to inspect the store variable? If its in the constructor, store is valid, but this.store is not so you therefore cannot access this.store in goToCheckout. Java uses variable masking in its methods, so dereferencing from "this" is only required if another variable either local or passed via parameter is using the same name.

abell12 11-15-2012 08:31 PM

I think you are talking about this line:
Code:

Member member = new Member(name, id, pinNumber, this);
If yes then I changed this to storeName, but now get an error compliling.

Saying:
Code:

constructor Member in class Member cannot be applied to given types:
required: java.lang.String,java.lang.String,java.lang.String,Store found:java.lang.String,java.lang.String,java.lang.String

Even changing this in the Member class:
Code:

private Store store;
To this:
Code:

private String store;
Gives me an error in the Member class now.

Fou-Lu 11-15-2012 08:46 PM

That's an entirely different problem.
Code:

    public Member(String newName, String newId, String newPinNumber, Store store);
Is the signature.
You have given it:
Code:

public Member(String, String, String);
Which has no overload on the constructor to accept that.

According to the error: required: java.lang.String,java.lang.String,java.lang.String,Store found:java.lang.String,java.lang.String,java.lang.String, you have constructed an object without providing it with a fourth parameter at all, so I haven't a clue where you did that. The error will tell you where it is. So this cannot be the construction you are using Member member = new Member(name, id, pinNumber, this);.

As for changing the datatype, I'm not sure what you expected. You cannot just change a Store object to a String object and expect that it will work without greatly modifying the other code to accommodate it.

As for your original problem, its simply that you haven't assigned this.store a value. All you have is a block of datatype Store, but you haven't put anything into it so therefore you cannot operate on it.

abell12 11-15-2012 09:18 PM

Ok, Ignore them errors and help me with this one please.

In the class Member I have these two fields.
Code:

private Item itemName;
private Item itemPrice;

In the class Item I have these two fields.
Code:

private String itemName;
private int itemPrice;

When I inspect the object in the Item class, the fields have values.
But when I inspect the object in the Member class, the fields show a null value.

Fou-Lu 11-15-2012 11:11 PM

The names in Member class don't make any sense. An item includes both its name and price, so calling itemPrice of type Item doesn't really jive. Not that its wrong, but it is a little unusual of a name to give it when the value is only of type Item and not a price.

Where have you set the value of these in Member? itemName and itemPrice in Item have no impact on itemName and itemPrice in Member, and they will also have no impact on any other instance of Item, so changes in one item's name doesn't affect another item's name. Only static variables are class level, non-static are instance specific.


All times are GMT +1. The time now is 04:50 AM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.