Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New Coder
    Join Date
    Mar 2012
    Posts
    33
    Thanks
    1
    Thanked 1 Time in 1 Post

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

  • #2
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,841
    Thanks
    42
    Thanked 200 Times in 199 Posts
    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
    Last edited by alykins; 11-15-2012 at 05:40 PM.

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

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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).

  • #4
    New Coder
    Join Date
    Mar 2012
    Posts
    33
    Thanks
    1
    Thanked 1 Time in 1 Post
    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.

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.

  • #6
    New Coder
    Join Date
    Mar 2012
    Posts
    33
    Thanks
    1
    Thanked 1 Time in 1 Post
    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.

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.

  • #8
    New Coder
    Join Date
    Mar 2012
    Posts
    33
    Thanks
    1
    Thanked 1 Time in 1 Post
    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.

  • #9
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.

  • #10
    New Coder
    Join Date
    Aug 2014
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts
    public class NullPointerException extends RuntimeException

    Thrown when an application attempts to use null in a case where an object is required. These include:

    Calling the instance method of a null object.
    Accessing or modifying the field of a null object.
    Taking the length of null as if it were an array.
    Accessing or modifying the slots of null as if it were an array.
    Throwing null as if it were a Throwable value.

    Applications should throw instances of this class to indicate other illegal uses of the null object. NullPointerException objects may be constructed by the virtual machine as if suppression were disabled and/or the stack trace was not writable.
    Last edited by soniavdavidson; 09-05-2014 at 10:47 AM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •