Java String toString方法null错误

时间:2013-11-15 17:29:52

标签: java

我想通过使用方法来获取订单的项目,项目是咖啡松饼和timBits

public String toString()

每件事情都正常工作,除了我没有得到我为他们取代的订单项而不是以下内容:

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

上面的数字代表订单中每件商品的数量。

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.
  

4 个答案:

答案 0 :(得分:4)

不要在每个子类中声明字段kind。将赋值添加到构造函数,例如:

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

答案 1 :(得分:2)

Item#toString方法中:

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

您使用kind变量,但绝不会通过您的应用程序初始化它。

这是因为您在每个子类上hiding kind字段。相反,在父类中将其声明为protected,并在每个子节点上相应地初始化它。

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

您可以通过将kind字段标记为final并禁止在执行构造函数时修改除其子项之外的任何其他类来限制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"); } } 字段。一个样本:

{{1}}

答案 2 :(得分:0)

您要在String kind的每个子类内分配Item。但是,此kind与超类kind的{​​{1}}字段不同,因为子类字段掩盖了超类的字段。您有两种方法可以解决此问题。

  1. 展开Item构造函数以接受Item并添加到String kind的调用。

    super
  2. 在子类构造函数中分配给 super(description,quantity,"Muffin"); ,以便为kindkind中声明的Item中指定的toString()分配正确的 public Muffin (String description, int quantity) { kind = "Muffin"; }

    {{1}}

答案 3 :(得分:0)

看起来您永远不会初始化变量kind,即使您从所有子类中的超类“Item”扩展,您只需重载变量名kind并重新定义它。因此,当您调用toString()方法时,您的父类(Item)永远不会初始化它的kind变量。你需要做的是在构造函数中设置kind,以便你可以通过super()传递它。