不明白为什么我收到null

时间:2015-10-16 16:48:40

标签: java

所以这是我的Superhero课程:

public class Superhero {

  public int strength;
  public int powerUp;
  public int defaultStrength = 10;
  public String name;

  public Superhero(String name) {
     this.strength = 10;
     System.out.println("The Superheroes available are :" + name);
  }

  public Superhero(String name, int strength) {
     if (strength >= 0) {
      this.strength = strength;
      System.out.println("The Superheroes available are :" + name);
     } else {
      System.out.println("Error. Strength cannot be < 0");
     }
  }

  public void setStrength( int strength ) {        
     this.strength = strength;
  }

  public int getStrength() {
    return strength;
  }

  public void powerUp(int powerUp) {
    this.strength += powerUp;
  }

}

以下是我的Fight课程,这里的问题是我运行时的问题我得到的结果是获胜者的结果是null而且我不明白为什么会这样做。

import java.io.*;

public class Fight {

  public static void main (String args[]) {

    Superhero gambit = new Superhero( "Gambit" );

    Superhero groot = new Superhero( "Groot", 79);

    System.out.println( "Gambit's strength is: " + gambit.strength);
    System.out.println( "Groot's strength is: " + groot.strength);
    System.out.println("The winner of the fight is: " + fight(gambit, groot));

  } 

  static String fight(Superhero a, Superhero b)
  {
    if (a.strength > b.strength)
    {
       return a.name;
    } else
    { 
       return b.name;
    }
  }
}

6 个答案:

答案 0 :(得分:7)

看看你的构造函数:

public Superhero(String name) {
   this.strength = 10;
   System.out.println("The Superheroes available are :" + name);
}

设置实例字段strength,但不使用name实例字段执行任何。你的其他构造函数是一样的。你需要包括:

this.name = name;

将参数中的值复制到实例变量中。在两个构造函数中执行此操作。否则,您最终会得到name的默认值,这是一个空引用。

顺便说一句,我强烈建议您将字段设为私有,并添加getName()方法以从fight方法中检索名称。如果强度低于0,我抛出异常,而不是只打印出错误消息,我会使构造函数不带strength参数只链接到执行的参数:

public Superhero(String name) {
    this(name, 10);
}

public Superhero(String name, int strength) {
    if (strength < 0) {
        throw new IllegalArgumentException("strength cannot be negative");
    }
    this.strength = strength;
    this.name = name;
    System.out.println("The Superheroes available are :" + name);
}

(构造函数显示的消息有点奇怪,因为它只列出了一个名称,但这是另一回事。)

答案 1 :(得分:3)

问题在于你的构造函数:

public Superhero(String name) {
   this.strength = 10;
   System.out.println("The Superheroes available are :" + name);
}

public Superhero(String name, int strength) {
   if (strength >= 0) {
      this.strength = strength;
      System.out.println("The Superheroes available are :" + name);
   } else {
      System.out.println("Error. Strength cannot be < 0");
   }
}

您的构造函数的参数为​​String name,但您从未将实例变量设置为值。包含对象的未初始化变量的默认值为null

答案 2 :(得分:3)

您永远不会在超级英雄的任何构造函数中设置名称。要修复第一个构造函数,例如:

public Superhero(String name) {
   this.name = name;
   this.strength = 10;
   System.out.println("The Superheroes available are :" + name);
}

答案 3 :(得分:2)

您永远不会将“传入”名称分配给班级的属性字段“名称”。所以后者保持为空。

顺便说一下:你真的想仔细阅读这些异常痕迹。它们提供了您需要的所有信息,以便了解最新情况。

最后注意事项:考虑使用关键字final作为您班级的属性。这样你就不会遇到这个问题;因为编译器会告诉你字段“name”没有在你的代码中初始化。

答案 4 :(得分:1)

创建一个getter方法,返回超级英雄类中的String名称,然后在Fight类中调用该方法。我还建议将您的超级英雄级别中的全局变量从公共变更为私有,因此只能在该类中访问它们。

编辑:如另一个答案中所述,将名称作为参数的构造函数永远不会分配给变量。

答案 5 :(得分:1)

您没有在构造函数中设置name变量的值,请使用此

public Superhero(String name) {
   this.strength = 10;
   this.name = name;
   System.out.println("The Superheroes available are :" + name);
}

public Superhero(String name, int strength) {

   this.name = name;
   if (strength >= 0) {
      this.strength = strength;
      System.out.println("The Superheroes available are :" + name);
   } else {
      System.out.println("Error. Strength cannot be < 0");
   }
}