Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-15-2012, 03:21 PM   PM User | #1
abell12
New Coder

 
Join Date: Mar 2012
Posts: 31
Thanks: 1
Thanked 1 Time in 1 Post
abell12 is an unknown quantity at this point
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);
    }
}
abell12 is offline   Reply With Quote
Old 11-15-2012, 05:38 PM   PM User | #2
alykins
Senior Coder

 
alykins's Avatar
 
Join Date: Apr 2011
Posts: 1,608
Thanks: 37
Thanked 183 Times in 182 Posts
alykins will become famous soon enough
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
__________________

I code C hash-tag .Net
Reference: W3C W3CWiki .Net Lib
Validate: html CSS
Debug: Chrome FireFox IE

Last edited by alykins; 11-15-2012 at 05:40 PM..
alykins is offline   Reply With Quote
Old 11-15-2012, 07:28 PM   PM User | #3
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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).
Fou-Lu is offline   Reply With Quote
Old 11-15-2012, 07:52 PM   PM User | #4
abell12
New Coder

 
Join Date: Mar 2012
Posts: 31
Thanks: 1
Thanked 1 Time in 1 Post
abell12 is an unknown quantity at this point
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);
    }
}

Last edited by abell12; 11-15-2012 at 07:56 PM..
abell12 is offline   Reply With Quote
Old 11-15-2012, 08:07 PM   PM User | #5
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Old 11-15-2012, 08:31 PM   PM User | #6
abell12
New Coder

 
Join Date: Mar 2012
Posts: 31
Thanks: 1
Thanked 1 Time in 1 Post
abell12 is an unknown quantity at this point
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.
abell12 is offline   Reply With Quote
Old 11-15-2012, 08:46 PM   PM User | #7
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Old 11-15-2012, 09:18 PM   PM User | #8
abell12
New Coder

 
Join Date: Mar 2012
Posts: 31
Thanks: 1
Thanked 1 Time in 1 Post
abell12 is an unknown quantity at this point
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.
abell12 is offline   Reply With Quote
Old 11-15-2012, 11:11 PM   PM User | #9
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:58 AM.


Advertisement
Log in to turn off these ads.