![]() |
java.lang.NullPointerException - Help
Having problems with the goToCheckout method at the bottom.
I am getting this error: Code:
java.lang.NullPointerExceptionThe payment must have a value because you have to type an amount in. Can anyone help me out here. Code:
public class Member |
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; //singletonCode:
private Store store;because if you did this Code:
private Store store = new store(); |
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 |
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.
|
I think you are talking about this line:
Code:
Member member = new Member(name, id, pinNumber, this);Saying: Code:
constructor Member in class Member cannot be applied to given types:Code:
private Store store;Code:
private String store; |
That's an entirely different problem.
Code:
public Member(String newName, String newId, String newPinNumber, Store store);You have given it: Code:
public Member(String, String, String);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. |
Ok, Ignore them errors and help me with this one please.
In the class Member I have these two fields. Code:
private Item itemName;Code:
private String itemName;But when I inspect the object in the Member class, the fields show a null value. |
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.