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);
}
}
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).
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);
}
}
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.
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
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.
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.