user2994159
user2994159

Reputation: 11

Java String toString method null error

Please I am trying to get the items of an order, the items are coffee muffin and timBits, by using the method

public String toString()

Every thing is working properly except that I do not get the items of the order I get null for them instead of the following:

    Muffin   "Bran"              ,3
    Coffee   "Latte"             , 1
    TimBits  "Assorted"         , 24
    Muffin   "Chocolate"        , 1
    Coffee   "Decaf"            , 2
   TimBits   "Chocolate"        , 12
   Muffin    "PeanutButter"     , 2
   Muffin    "Blueberry"        , 5

the numbers above represents the quantity of each item in the order.

class Item
{
  protected String description;
  protected int quantity;
  protected String kind;
  private double cost;

   public double getCost()
   {
      return this.cost;
   }

 public Item (String description, int quantity)
 {
   this.description = description;
   this.quantity = quantity;
 }

 public String toString()
 {
   return "Item: " + "      " +kind + "      " + ": description: "  + "      " +description  +"quantity:" +"        " + quantity ;

 }

 class Coffee extends Item
{
  protected double cost1Coffee;
  String kind = "Coffee";
  public Coffee (String description, int quantity)


 {
   super(description, quantity);
   cost1Coffee = 4 ;
 }

 }
}


 class Muffin extends Item
{
  protected double cost1Muffin;
  protected double cost2Muffin;
  protected double cost3Muffin;
  String kind = "Muffin";
  public Muffin (String description, int quantity)

 {
   super(description,quantity);
   cost1Muffin = 1;
   cost2Muffin = 0.75;
   cost3Muffin = 0.50;
 }



}


 class TimBits extends Item
{
 protected double  cost1TimBits ;
 String kind = "TimBits";
 public TimBits (String description, int quantity)

 {
   super(description, quantity);
   cost1TimBits = 0.25;
 }

}



/***************************************************************/
/***************************************************************/

class A4Q1Util
{
 private static ArrayList<Item> order;

 private static int count = 0;

 public static Item getItem()
 {
  Item item;

  if (order==null)
  {
   order = new ArrayList<Item>();

   order.add(new Muffin("Bran", 3));
   order.add(new Coffee("Latte", 1));
   order.add(new TimBits("Assorted", 24));
   order.add(new Muffin("Chocolate", 1));
   order.add(new Coffee("Decaf", 2));
   order.add(new TimBits("Chocolate", 12));
   order.add(new Muffin("PeanutButter", 2));
   order.add(new Muffin("Blueberry", 5));
  }

  item = null;
  if (count<order.size())
  {
   item = order.get(count);
   count++;
  }
  {
  return item;
  }


 }
}



output:

Item:       null      : description:       Branquantity:        3 
Item:       null      : description:       Lattequantity:        1 
Item:       null      : description:       Assortedquantity:        24 
Item:       null      : description:       Chocolatequantity:        1 
Item:       null      : description:       Decafquantity:        2 
Item:       null      : description:       Chocolatequantity:        12 
Item:       null      : description:       PeanutButterquantity:        2 
Item:       null      : description:       Blueberryquantity:        5 

Program completed normally.

Upvotes: 1

Views: 233

Answers (4)

Luiggi Mendoza
Luiggi Mendoza

Reputation: 85779

In your Item#toString method:

public String toString() {
    return "Item: " + "      " +kind + "      " + ": description: "  + "      " +description  +"quantity:" +"        " + quantity ;
}

You use kind variable, but never initialize it through your application.

This is because you're hiding the kind field on every child class. Instead, declare it as protected in parent class and just initialize it accordingly on each child.

class Coffee extends Item {
    protected double cost1Coffee;
    //drop this
    //String kind = "Coffee";
    public Coffee(...) {
        super(...);
        kind = "Coffee";
    }
}

You can even be more restrictive about the kind field by marking it as final and disallowing any other class modifying it except its children when executing the constructor. A sample of this:

class Item {
    //other fields...
    protected final String kind;
    protected Item (String description, int quantity, String kind) {
        this.description = description;
        this.quantity = quantity;
        this.kind = kind;
    }
    public Item (String description, int quantity) {
        this(description, quantity, "uncategorized");
    }
}

class Coffee extends Item {
    public Coffee(String description, int quantity) {
        //similar in other subclasses of Item
        super(description, quantity, "Coffee");
    }
}

Upvotes: 2

WillBD
WillBD

Reputation: 1919

It looks like you never initialize your variable kind and even though you are extending from the superclass "Item" throughout all of your child classes, you're simply overloading the variable name kind and redefining it. Therefore when you make the call to the toString() method, your parent class (Item) never gets it's kind variable initialized. What you need to do is also set the kind in the constructor so that you can pass it in through super().

Upvotes: 0

sushain97
sushain97

Reputation: 2802

You're assigning to String kind inside each subclass of Item. However, this kind is not the same as the kind field of the superclass Item since the subclass field masks that of the superclass. You have two options to fix this.

  1. Expand the Item constructor to accept a String kind and add to the call to super.

     super(description,quantity,"Muffin");
    
  2. Assign to kind inside the subclass constructors so that the correct kind is assigned to (the one declared in Item which is used by toString()).

     public Muffin (String description, int quantity)
     {
        kind = "Muffin";
     }
    

Upvotes: 0

Tobias
Tobias

Reputation: 7771

Don't declare the field kind in every sub-class. Add the assignment to the constructors, e.g.:

public TimBits (String description, int quantity) {
    super(description, quantity);
    kind = "TimBits";
    cost1TimBits = 0.25;
}

Upvotes: 4

Related Questions