Results 1 to 6 of 6
  1. #1
    Return 0 is offline Member
    Join Date
    Feb 2011
    Posts
    12
    Rep Power
    0

    Default Default value of String upon construction

    Hello everyone, I'm new to this forum and to Java, but not programming. I have the below code. What I am trying to do is create a bag to store items in. I have an abstract class called Object, which right now is just a shell. My Item class is derived from Object. So what I want to do is create an Item called bag with say 10 slots, or whatever number, to serve as a player's inventory. I then want to add any item to the bag where there is an open slot. Right now I'm just trying to display the names of each item in a for loop which bombs when it reaches index 2 where i have not added an item.

    I'm not quite sure I understand why. Do I need to create an empty Item of some sort and go ahead and fill in the remaining bag slots for this to work? For some reason I was under the impression that the array setup 10 objects and defaulted their members to something. Any help would be appreciated.

    Java Code:
    public class Driver
    {
        public static void main( String[] args )
        {
            Item bag[]  = new Item[10];
            Item potion = new Item( "Potion", "Consumable" );
            Item sword  = new Item( "Short Sword", "Sword" );
    
            for ( int i = 0; i < bag.length; i++ )
            {
                System.out.println( bag[i].getItemName() );
            }
        }
    }
    and...

    Java Code:
    public class Item extends Object
    {
        private String itemName;
        private String itemType;
    
        // Empty constructor
        Item()
        {
        }
    
        // Overloaded constructor
        Item( String n, String t )
        {
            itemName = n;
            itemType = t;
        }
    
        public String getItemName()
        {
            if ( this.itemName.isEmpty() )
            {
                return "Empty Slot";
            }
            else
            {
                return this.itemName;
            }       
        }
    }

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    An Object[] (any type of object) is initialised with a null reference in all slots until you put something else there.

  3. #3
    Return 0 is offline Member
    Join Date
    Feb 2011
    Posts
    12
    Rep Power
    0

    Default

    I see. So what would be the best way to output that a slot is empty?

    EDIT: Figured it out, just check for null... originally I was checking for NULL which didn't work. Thanks.
    Last edited by Return 0; 03-03-2011 at 06:42 PM.

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Java Code:
    if (array[index] == null)

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default

    I have an abstract class called Object ...
    It's bad practice to duplicate a class name that already exists in the standard JDK and doubly bad practice to duplicate a class name that exists in the java.lang package.

    But it's triply (or worse) bad practice to duplicate the class name of the class that sits at the top of the Java class hierarchy: Object.

    db

  6. #6
    Return 0 is offline Member
    Join Date
    Feb 2011
    Posts
    12
    Rep Power
    0

    Default

    Quote Originally Posted by Darryl.Burke View Post
    It's bad practice to duplicate a class name that already exists in the standard JDK and doubly bad practice to duplicate a class name that exists in the java.lang package.

    But it's triply (or worse) bad practice to duplicate the class name of the class that sits at the top of the Java class hierarchy: Object.

    db
    Ha, I figured it probably already existed. Thanks for pointing that out, I'll rename it.

Similar Threads

  1. Object Construction ends Program in GridWorld
    By BJ1110 in forum Advanced Java
    Replies: 9
    Last Post: 02-04-2011, 04:02 PM
  2. Need help with a construction of a tree of objects
    By macwadu in forum AWT / Swing
    Replies: 5
    Last Post: 08-09-2010, 10:48 AM
  3. Copy Default table model to another default table model?
    By greatmajestics in forum AWT / Swing
    Replies: 2
    Last Post: 04-28-2010, 04:08 PM
  4. Replies: 1
    Last Post: 03-10-2009, 01:12 PM
  5. AVL-tree construction
    By student89 in forum Advanced Java
    Replies: 0
    Last Post: 10-27-2008, 05:33 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
  •