Results 1 to 4 of 4
Thread: Trouble with arrays.......
- 03-08-2012, 09:26 PM #1
Member
- Join Date
- Mar 2012
- Location
- Vestal, NY
- Posts
- 36
- Rep Power
- 0
Trouble with arrays.......
Hey guys, I'm writing a program for class to show inheritance, and i think im doing it wrong. Could you help out? I'm not asking for a the answer, just a little debugging.
I'm getting a NullPointerException at sum += myCoin[counter].getValue(); Any ideas why?Java Code:// CLASS COIN: public class Coin { private final int HEADS = 0; private final int TAILS = 1; private int face; //-------------------------------------------------------------------------------------------------- // Sets up the coin by flipping it initially //-------------------------------------------------------------------------------------------------- public Coin() { flip(); } //-------------------------------------------------------------------------------------------------- // Flips the coin by randomly choosing a face value //-------------------------------------------------------------------------------------------------- public void flip() { face = (int) (Math.random() * 2); } //-------------------------------------------------------------------------------------------------- // Returns true if the current face of the coin is heads //-------------------------------------------------------------------------------------------------- public boolean isHeads() { return (face == HEADS); } //-------------------------------------------------------------------------------------------------- // Returns the current face of the coin as a string //-------------------------------------------------------------------------------------------------- public String toString() { String faceName; if (face == HEADS) faceName = "Heads"; else faceName = "Tails"; return faceName; } } // CLASS MONETARYCOIN: import java.util.Random; public class MonetaryCoin extends Coin { private int value; private int[] list = {1, 5, 10, 25}; private Random generator = new Random(); //-------------------------------------------------------------------------------------------------------------------- // Calls the Coin constructor and adds a value //-------------------------------------------------------------------------------------------------------------------- public MonetaryCoin() { super(); value = list[generator.nextInt(4)]; } //-------------------------------------------------------------------------------------------------------------------- // lets user input a value for the coin //-------------------------------------------------------------------------------------------------------------------- public void setValue(int newvalue) { value = newvalue; } //-------------------------------------------------------------------------------------------------------------------- // returns the value of the coin //-------------------------------------------------------------------------------------------------------------------- public int getValue() { return value; } } // DRIVER: import javax.swing.*; public class driver { public static void main(String[] args) { MonetaryCoin myCoin[] = new MonetaryCoin[34]; //makes an array of 34 monetary coins called MyCoin int sum = 0; //sets the base value of the sum counter for(int counter = 0; counter < 34; counter++) //runs through all 34 objects of myCoin { sum += myCoin[counter].getValue(); //adds current MonetaryCoins value to sum } JOptionPane.showMessageDialog(null, "The total of all 34 coins is: " + sum); //outputs the value of sum in a dialog box } }Last edited by jcarosella10; 03-08-2012 at 09:53 PM.
- 03-08-2012, 09:27 PM #2
Member
- Join Date
- Mar 2012
- Location
- Vestal, NY
- Posts
- 36
- Rep Power
- 0
Re: Trouble with arrays.......
oh by the way the Coin class is out of a book, not my code
- 03-08-2012, 09:50 PM #3
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
Re: Trouble with arrays.......
a)list[generator.nextInt(4) - 1];
nextInt(4) can generate zero. 0-1 = -1 -> ArrayIndexOutOfBoundsException
b) sum += myCoin[counter].getValue();
you never create myCoin objects! you have created a array, but myCoin[0] = null
add the line myCoin[counter] = new MonetaryCoin(); before
- 03-08-2012, 09:57 PM #4
Member
- Join Date
- Mar 2012
- Location
- Vestal, NY
- Posts
- 36
- Rep Power
- 0
Similar Threads
-
Trouble with arrays..
By ertopp in forum New To JavaReplies: 14Last Post: 11-11-2011, 05:58 AM -
Trouble with arrays Help
By mrjaeyun in forum New To JavaReplies: 3Last Post: 11-07-2011, 10:14 PM -
Trouble with arrays and classes
By CuddlyKittens11 in forum Advanced JavaReplies: 3Last Post: 04-25-2011, 12:42 AM -
Arrays trouble
By gto400no1 in forum New To JavaReplies: 1Last Post: 04-14-2010, 01:20 AM -
HELP: Still having trouble getting arrays :(
By Psyclone in forum New To JavaReplies: 4Last Post: 02-06-2010, 01:05 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks