简体   繁体   中英

Why do I get null as the String output?

Why does the String variable outputs String as the Name and the Building Material?

Always when I am printing out my code, the output at the Name and the building material is always "null".

public class Mauer{

    private int height;
    private int width;
    private String baumaterial;
    private String Name;

    Mauer(int h, int b, String bm, String n){ 
        this.setD(h,b, baumaterial, Name); 
        }

    public void setD(int h, int b, String bm, String n) {
      this.height=h;
      this.width=b;
      this.baumaterial = bm;
      this.Name = n;


    }

    public int getH() {
        return height; 
        }
    public int getW() { 
        return width; 
        }
    public String getBM() {
        return baumaterial;
        }
    public String getN() {
        return Name;
    }
}

... and the second part

public class Program extends Mauer{

    Program(int h, int b, String bm, String n) {
        super(h, b,  bm, n);
        // TODO Auto-generated constructor stub
    }

    public static void main(String[] args) {
        Mauer m1 = new Mauer(5,10, "Stein", "Burgmauer");
        Mauer m2 = new Mauer(7,12, "Holz", "Holzmauer");

        System.out.println("Die Höhe der Mauer ist " + m1.getH());
        System.out.println(" Die Breite der Mauer ist "+ m1.getW());
        System.out.println(" Das Material besteht aus "+ m1.getBM());
        System.out.println("Der Name der Mauer ist "+m1.getN());

        System.out.println("Die Höhe der zweiten Mauer ist "+ m2.getH());
        System.out.println(" Die Breite der zweiten Mauer ist "+ m2.getW());
        System.out.println(" Das zweite Material besteht aus "+ m2.getBM());
        System.out.println("der Name der zweiten Mauer 2 ist "+ m2.getN());

    }

}

your constructor is passing as parameters to a method the empty variables that it has. Yo should pass

 this.setD(h, b, bm, n)

Name for example was only defined but not initialised so Name still is null, and you were passing It as an argument to setD. If you do debug you will see this.

In Java, all instance variables are initialized to their default values before the constructor has a chance to initialize them. For objects, the default value is null .

Your constructor for the Mauer class passes not the arguments bm and n , but the instance variables baumaterial and Name . (Normal Java naming conventions would name Name as name , lowercase.) When setD is called, it essentially sets these variables to themselves -- null .

When these values are retrieved for printing, the + operator performs string conversion, which converts null to the string "null" , which you see as output.

Fix your constructor to pass all 4 parameters to setD instead of just 2.

Incidentally, there is no reason apparent in the code given in this question for Program to extend Mauer . It's likely that you can remove the Program constructor and have Program not extend Mauer .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM