Results 1 to 11 of 11
  1. #1
    s1l3nced is offline Member
    Join Date
    Feb 2012
    Posts
    6
    Rep Power
    0

    Default Stuck on homework problem (1st year Java programmer)

    I've done pretty much most of what the teacher has asked, and when I compile, i'm receiving an error message.
    run:
    Your wallet's full!
    Exception in thread "main" java.lang.NullPointerException
    at prog24178.sean.duchstein.assignment2.Wallet.displa yContents(Wallet.java:21)
    at prog24178.sean.duchstein.assignment2.TestWallet.ma in(TestWallet.java:42)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 1 second)

    Apparently, this line:
    Java Code:
    getBills()[i].getDescription();
    is causing issues. I'm not sure exactly how.. I've traced back and can't see anything wrong with the getDescription method, nor the getBills method.

    Moneyholder.java
    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package prog24178.sean.duchstein.assignment2;
    
    /**
     *
     * @author Sean
     */
    public abstract class MoneyHolder {
    
        private Bill[] bills;
        private int numBillsInside;
        int capacity;
    
        protected MoneyHolder(int capacity) {
            bills = new Bill[capacity];
            numBillsInside = 0;
            this.capacity = capacity;
        }
    
        public Bill[] getBills() {
            return bills;
        }
    
        public int getNumBillsInside() {
            return numBillsInside;
        }
    
        public void addBill(Bill bill) {
            if (bill instanceof Bill) {
                Bill billNum = (Bill) bill;
                if (getNumBillsInside() == capacity) {
                    System.out.println("Your wallet's full!");
                } else {
                    getBills()[numBillsInside] = billNum;
                    System.out.println("hi");
                    numBillsInside++;
                }
            }
        }
    
        public abstract void displayContents();
    }
    Bill.java
    Java Code:
    package prog24178.sean.duchstein.assignment2;
    
    /**
     *
     * @author Sean
     */
    public abstract class Bill {
        private int denomination;
        private String colour;
        
        protected Bill (int denomination, String colour) {
            this.denomination = denomination;
            this.colour = colour;
        }
    
        public int getDenomination() {
            return denomination;
        }
    
        public String getColour() {
            return colour;
        }
        
        public abstract String getDescription();
        
        
    }
    Wallet.java
    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package prog24178.sean.duchstein.assignment2;
    
    /**
     *
     * @author Sean
     */
    public class Wallet extends MoneyHolder implements Countable {
    
        Wallet(int capacity) {
            super(capacity);
        }
    
        @Override
        public void displayContents() {
    
            for (int i = 0; i < getBills().length; i++) {
                getBills()[i].getDescription();
            }
        }
        
        public int countMoney() {
    
            int totalAmountOfBills = 0;
    
            for (int i = 0; i < this.getBills().length; i++) {
                totalAmountOfBills += this.getBills()[i].getDenomination();
            }
            return totalAmountOfBills;
        }
    }
    TestWallet.java
    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package prog24178.sean.duchstein.assignment2;
    
    /**
     *
     * @author Sean
     */
    public class TestWallet {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
                    
            
            Wallet smallWallet = new Wallet(2);
            Wallet largeWallet = new Wallet(10);
            
            Bill bill0 = new FiveDollarBill(5, "Blue");
            Bill bill1 = new TenDollarBill(10, "purple");
            Bill bill2 = new TwentyDollarBill(20, "green");
            Bill bill3 = new FiveDollarBill(5, "black");
            Bill bill4 = new TenDollarBill(10, "black");
            Bill bill5 = new TwentyDollarBill(20, "black");
            
            smallWallet.addBill(bill0);
            smallWallet.addBill(bill5);
            smallWallet.addBill(bill1);
            
            largeWallet.addBill(bill1);
            largeWallet.addBill(bill2);
            largeWallet.addBill(bill3);
            largeWallet.addBill(bill4);
            largeWallet.addBill(bill0);
            largeWallet.addBill(bill0);
            largeWallet.addBill(bill5);
            
            smallWallet.displayContents();
            largeWallet.displayContents();
        }
    }
    FiveDollarBill.java
    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package prog24178.sean.duchstein.assignment2;
    
    /**
     *
     * @author Sean
     */
    public class FiveDollarBill extends Bill {
        public FiveDollarBill(int denomination, String colour){
            super(denomination, colour);   
        }
        
        @Override
        public String getDescription() {
            return this.getColour() + " five dollar bill";
        }
    }
    Countable.java
    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package prog24178.sean.duchstein.assignment2;
    
    /**
     *
     * @author Sean
     */
    public interface Countable {
       int countMoney();
    }
    TenDollarBill.java
    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package prog24178.sean.duchstein.assignment2;
    
    /**
     *
     * @author Sean
     */
    public class TenDollarBill extends Bill {
        public TenDollarBill(int denomination, String colour){
            super(denomination, colour);   
        }
        
        @Override
        public String getDescription() {
            return this.getColour() + " five dollar bill";
        }
    }
    TwentyDollarBill.java
    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package prog24178.sean.duchstein.assignment2;
    
    /**
     *
     * @author Sean
     */
    public class TwentyDollarBill extends Bill {
        public TwentyDollarBill(int denomination, String colour){
            super(denomination, colour);   
        }
        
        @Override
        public String getDescription() {
            return this.getColour() + " five dollar bill";
        }
    }






    Description:
    This assignment is based primarily on Chapter 14 of the textbook. Your task is to model a wallet and
    various monetary amounts. Here is a list of the classes and interfaces that you are required to
    implement:

    Money Model:
    Bill.java an abstract class which contains the following:
     A private data field of integer type called denomination represents the monetary amount of
    the bill
     A private String data field called colour represents the colour of the bill
     A constructor of protected visibility that has the following parameters:
    o An integer parameter that sets the denomination
    o A String parameter that sets the colour of the bill
     Accessor methods for each data field
     An abstract method called getDescription() that returns a String description of the bill
    FiveDollarBill.java
    TenDollarBill.java
    TwentyDollarBill.java
     Each of the above classes should inherit from the Bill class
     Each of the above classes should have a constructor with the following parameter:
    o A String parameter that sets the colour of the bill
     Each of the above classes must implement the getDescription() method by returning a String
    containing the colour and denomination of the bill

    Wallet Model:
    Countable.java an interface with one abstract method countMoney() which returns an integer
    MoneyHolder.java an abstract class which contains the following:
     A private array of Bills data field called bills holds the Bill objects that are inside this money
    holder
     A private integer data field called numBillsInside holds the number of bills that are currently
    inside this particular money holder
     A constructor of protected visibility that does the following:
    o Has an integer parameter that sets the capacity of the money holder (maximum number
    of bills that can fit inside the money holder)Justin Alerta PROG 24178 Assignment #2 Winter 2012
    o Sets the size of the bills array based on the capacity
    o Sets the numBillsInside variable to 0
     Accessor methods for each data field
     public void addBill(Bill bill) adds a bill to this money holder
    o If the wallet is full (numBillsInside = the wallet capacity), DO NOT add the bill and
    display a message stating that the wallet is full
    o If the wallet is not full, add the bill to the bills array (at index numBillsInside) and
    increment the number of bills inside the wallet by one
     An abstract void method called displayContents() used to display the contents of the wallet

    Wallet.java a class that inherits from MoneyHolder and implements the Countable interface which
    contains the following:
     A constructor with the following parameter:
    o An integer parameter that sets the capacity
     Implement all abstract methods:
    o countMoney() returns the total amount of all the bills inside the wallet
    o displayContents() calls the getDescription() method for each bill that is inside the
    wallet


    Create a test class called TestWallet.java which does the following:
    1. Create two wallets: a smallWallet of capacity 2 and a largeWallet of capacity 10.
    2. Create the following bills:
    a. A blue $5 bill
    b. A purple $10 bill
    c. A green $20 bill
    d. Black $5, $10, and $20 bills
    3. Add a blue $5, black $20, and purple $10 bill to the small wallet.
    4. Add a purple $10, green $20, black $5, black $10, blue $5, blue $5, and black $20 bill to the large
    wallet.
    5. Display the contents and amount of money inside each wallet.
    Bonus (optional):
    6. Display the number of $5 bills inside the small wallet.
    7. Display the number of $20 bills inside the large wallet.
    Hint: You must iterate the contents of each wallet to determine how many of the specified bill is
    inside each wallet
    Sample Output:

    Below is the expected output when you run TestWallet.java:
    Wallet is full!
    Wallet contents:
    blue five dollar bill
    black twenty dollar bill
    The small wallet has $25
    Wallet contents:
    purple ten dollar bill
    twenty twenty dollar bill
    black five dollar bill
    black ten dollar bill
    blue five dollar bill
    blue five dollar bill
    black twenty dollar bill
    The large wallet has $75
    -----------------------
    BONUS SECTION
    There are 1 $5 bills in the small wallet.
    There are 2 $20 bills in the large wallet.


    The problem lies in MoneyHolder.java
    here :
    Java Code:
    public void addBill(Bill bill) {
            if (bill instanceof Bill) {
                Bill billNum = (Bill) bill;
                if (numBillsInside == this.getBills().length) {
                    System.out.println("Wallet is full");
                } else {
                    this.getBills()[numBillsInside] = billNum;
                    numBillsInside++;
    
                }
            }
        }
    I cannot figure this one out. If anyone can, please let me know. I'm in desperate need. I'm sure you java programmers know my frustration.. I've been at this for 36 hours now. I keep getting upset with myself when I can't figure it out!! gah..
    Last edited by s1l3nced; 02-28-2012 at 12:35 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Stuck on homework problem (1st year Java programmer)

    The most important bit of information is to know which line throws the NPE. So please tell us which line is it? Then you'll need to check to see which variable on that line is null and work back through your code to see why.

    So again, which line is throwing the NPE?

  3. #3
    s1l3nced is offline Member
    Join Date
    Feb 2012
    Posts
    6
    Rep Power
    0

    Default Re: Stuck on homework problem (1st year Java programmer)

    Java Code:
      getBills()[numBillsInside] = billNum;
    Hey, Thanks for the quick response!! Wow I didn't think someone would reply like that :P

    run:
    Exception in thread "main" java.lang.NullPointerException
    at prog24178.sean.duchstein.assignment2.MoneyHolder.a ddBill(MoneyHolder.java:37)
    at prog24178.sean.duchstein.assignment2.TestWallet.ma in(TestWallet.java:29)
    Java Result: 1

    The line is in MoneyHolder.Java on Line 37.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Stuck on homework problem (1st year Java programmer)

    So only one thing can be null on that line and cause a NPE -- whatever array is returned by getBills(). Back track to that method and see why it's returning null, then get back to us.

  5. #5
    s1l3nced is offline Member
    Join Date
    Feb 2012
    Posts
    6
    Rep Power
    0

    Default Re: Stuck on homework problem (1st year Java programmer)

    Someone told me

    "the first line you're creating a new variable called bills local to the constructor
    the third line you're assigning the instance variable capacity to the value of capacity"

    Do you think the issue lies in this part of the code in MoneyHolder.Java
    Java Code:
    protected MoneyHolder(int capacity) {
            Bill[] bills = new Bill[capacity];
            numBillsInside = 0;
            this.capacity = capacity;
    ??

    The array is initialized, but perhaps not accessable outside this scope? Any idea how to have this happen outside??

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Stuck on homework problem (1st year Java programmer)

    Welcome to variable shadowing 101. This code:

    Java Code:
    protected MoneyHolder(int capacity) {
            Bill[] bills = new Bill[capacity];
            numBillsInside = 0;
            this.capacity = capacity;
    declares a bills variable inside of this constructor and will initialize it to some capacity, but as soon as the constructor ends, this local variable will go out of scope and will exist no more. See how to solve this?
    Last edited by Fubarable; 02-28-2012 at 12:13 AM.

  7. #7
    s1l3nced is offline Member
    Join Date
    Feb 2012
    Posts
    6
    Rep Power
    0

    Default Re: Stuck on homework problem (1st year Java programmer)

    if I take bills variable out of the constructor, it won't be in reference with capacity, though.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Stuck on homework problem (1st year Java programmer)

    Quote Originally Posted by s1l3nced View Post
    if I take bills variable out of the constructor, it won't be in reference with capacity, though.
    I'm not telling you to take bills out of the constructor -- just don't re-declare it in there. The bills variable in the constructor is completely distinct from the one in the class due to your re-declaration of it. Please read the link in my previous post on variable shadowing for more on this.

  9. #9
    s1l3nced is offline Member
    Join Date
    Feb 2012
    Posts
    6
    Rep Power
    0

    Default Re: Stuck on homework problem (1st year Java programmer)

    Java Code:
    private Bill[] bills;
        private int numBillsInside;
        int capacity;
    
        protected MoneyHolder(int capacity) {
            bills = new Bill[capacity];
    I did that. Perfect. this works! onto the next error...

    run:
    Your wallet's full!
    Exception in thread "main" java.lang.NullPointerException
    at prog24178.sean.duchstein.assignment2.Wallet.displa yContents(Wallet.java:21)
    at prog24178.sean.duchstein.assignment2.TestWallet.ma in(TestWallet.java:42)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 1 second)


    Method the error is happening in..

    Java Code:
    @Override
        public void displayContents() {
    
            for (int i = 0; i < getBills().length; i++) {
                getBills()[i].getDescription();
            }
        }
    from class Wallet.Java
    this specific line..
    Java Code:
    getBills()[i].getDescription();
    Any idea? I'm stumped again >_<

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Stuck on homework problem (1st year Java programmer)

    I don't see that line anywhere in the code you've posted above.

  11. #11
    s1l3nced is offline Member
    Join Date
    Feb 2012
    Posts
    6
    Rep Power
    0

    Default Re: Stuck on homework problem (1st year Java programmer)

    Sorry, I've added it in afterwards, a few trial and errors led me to that point. What I have most recently posted, should be where the original is.. I will update the original. I've looked through all classes to find the getDescription method, as well as getBills.. I still don't see why it's giving me compile errors..

Similar Threads

  1. Help with Homework Problem
    By Holy_Zombies in forum New To Java
    Replies: 6
    Last Post: 10-05-2010, 06:26 AM
  2. Having problem in calculating leap year
    By lclclc in forum New To Java
    Replies: 3
    Last Post: 09-25-2009, 08:50 PM
  3. Date of first day, given the week in the year and the year...
    By Lee.J.Baxter in forum Advanced Java
    Replies: 1
    Last Post: 08-26-2009, 08:48 AM
  4. Homework PREREQUISITE Problem
    By ChrisC in forum New To Java
    Replies: 7
    Last Post: 11-27-2007, 05:36 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
  •