Results 1 to 4 of 4
  1. #1
    jcarosella10 is offline Member
    Join Date
    Mar 2012
    Location
    Vestal, NY
    Posts
    36
    Rep Power
    0

    Default 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.

    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
        }
    }
    I'm getting a NullPointerException at sum += myCoin[counter].getValue(); Any ideas why?
    Last edited by jcarosella10; 03-08-2012 at 10:53 PM.

  2. #2
    jcarosella10 is offline Member
    Join Date
    Mar 2012
    Location
    Vestal, NY
    Posts
    36
    Rep Power
    0

    Default Re: Trouble with arrays.......

    oh by the way the Coin class is out of a book, not my code

  3. #3
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default 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

  4. #4
    jcarosella10 is offline Member
    Join Date
    Mar 2012
    Location
    Vestal, NY
    Posts
    36
    Rep Power
    0

    Default Re: Trouble with arrays.......

    ohhh. I figured out the arrayoutofbounds but i knew i was forgetting something. Thanks!

Similar Threads

  1. Trouble with arrays..
    By ertopp in forum New To Java
    Replies: 14
    Last Post: 11-11-2011, 06:58 AM
  2. Trouble with arrays Help
    By mrjaeyun in forum New To Java
    Replies: 3
    Last Post: 11-07-2011, 11:14 PM
  3. Trouble with arrays and classes
    By CuddlyKittens11 in forum Advanced Java
    Replies: 3
    Last Post: 04-25-2011, 01:42 AM
  4. Arrays trouble
    By gto400no1 in forum New To Java
    Replies: 1
    Last Post: 04-14-2010, 02:20 AM
  5. HELP: Still having trouble getting arrays :(
    By Psyclone in forum New To Java
    Replies: 4
    Last Post: 02-06-2010, 02:05 AM

Tags for this Thread

Posting Permissions

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