Results 1 to 11 of 11
- 02-27-2012, 11:47 PM #1
Member
- Join Date
- Feb 2012
- Posts
- 6
- Rep Power
- 0
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: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.Java Code:getBills()[i].getDescription();
Moneyholder.java
Bill.javaJava 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(); }
Wallet.javaJava 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(); }
TestWallet.javaJava 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; } }
FiveDollarBill.javaJava 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(); } }
Countable.javaJava 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"; } }
TenDollarBill.javaJava 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(); }
TwentyDollarBill.javaJava 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"; } }
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 :
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..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++; } } }Last edited by s1l3nced; 02-28-2012 at 12:35 AM.
-
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?
- 02-28-2012, 12:03 AM #3
Member
- Join Date
- Feb 2012
- Posts
- 6
- Rep Power
- 0
Re: Stuck on homework problem (1st year Java programmer)
Hey, Thanks for the quick response!! Wow I didn't think someone would reply like that :PJava Code:getBills()[numBillsInside] = billNum;
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.
-
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.
- 02-28-2012, 12:07 AM #5
Member
- Join Date
- Feb 2012
- Posts
- 6
- Rep Power
- 0
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??
-
Re: Stuck on homework problem (1st year Java programmer)
Welcome to variable shadowing 101. This code:
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?Java Code:protected MoneyHolder(int capacity) { Bill[] bills = new Bill[capacity]; numBillsInside = 0; this.capacity = capacity;Last edited by Fubarable; 02-28-2012 at 12:13 AM.
- 02-28-2012, 12:18 AM #7
Member
- Join Date
- Feb 2012
- Posts
- 6
- Rep Power
- 0
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.
-
Re: Stuck on homework problem (1st year Java programmer)
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.
- 02-28-2012, 12:22 AM #9
Member
- Join Date
- Feb 2012
- Posts
- 6
- Rep Power
- 0
Re: Stuck on homework problem (1st year Java programmer)
I did that. Perfect. this works! onto the next error...Java Code:private Bill[] bills; private int numBillsInside; int capacity; protected MoneyHolder(int capacity) { bills = new Bill[capacity];
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..
from class Wallet.JavaJava Code:@Override public void displayContents() { for (int i = 0; i < getBills().length; i++) { getBills()[i].getDescription(); } }
this specific line..
Any idea? I'm stumped again >_<Java Code:getBills()[i].getDescription();
-
Re: Stuck on homework problem (1st year Java programmer)
I don't see that line anywhere in the code you've posted above.
- 02-28-2012, 12:27 AM #11
Member
- Join Date
- Feb 2012
- Posts
- 6
- Rep Power
- 0
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
-
Help with Homework Problem
By Holy_Zombies in forum New To JavaReplies: 6Last Post: 10-05-2010, 06:26 AM -
Having problem in calculating leap year
By lclclc in forum New To JavaReplies: 3Last Post: 09-25-2009, 08:50 PM -
Date of first day, given the week in the year and the year...
By Lee.J.Baxter in forum Advanced JavaReplies: 1Last Post: 08-26-2009, 08:48 AM -
Homework PREREQUISITE Problem
By ChrisC in forum New To JavaReplies: 7Last Post: 11-27-2007, 05:36 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks