简体   繁体   中英

My System.out.println(); keeps printing null instead of the text it should print

I started Java a while ago and I am now busy with abstract classes. I've been busy with this code for two days now and I can't find out how to fix this. The methods names might not be in English, sorry for that but I think it wouldn't be too big of a problem. I don't know why the output is as following: null null null null null null null null null null null null null null null null

I hope you guys can help me out. I would appreciate any help! Thanks in advance.

Main class, which is also abstract:

public abstract class Boek {
public String isbn;
public String auteur;
public String paginas;
public String titel;

public abstract void setIsbn(String isbn);
public abstract void getIsbn(); 
public abstract void setAuteur(String auteur);
public abstract void getAuteur();
public abstract void setPaginas(int paginas);
public abstract void getPaginas();
public abstract void setTitel(String titel);
public abstract void getTitel();

public static void main(String[] args) {
    Studieboek  sb = new Studieboek();
    Roman       ro = new Roman();
    Dichtbundel db = new Dichtbundel();

    sb.setAuteur("J.K. Rowling");
    sb.setIsbn("547896587412");
    sb.setPaginas(251);
    sb.setTitel("Harry Potter");
    sb.addNAuteur("R.K. Dale");
    sb.addOndertitel("Exactly");
    sb.printSB();

    ro.setAuteur("Suzanne Vermeer");
    ro.setIsbn("9632589632574");
    ro.setPaginas(311);
    ro.setTitel("De Zwarte Piste");
    ro.printRO();

    db.setAuteur("A.Y. Lmao");
    db.setIsbn("5781236985478");
    db.setPaginas(171);
    db.setTitel("Rijmen kreng");
db.addGedicht("Rijmpje");
    db.printDB();
}
}

First subclass:

public class Studieboek extends Boek {  
private String ondertitel;
private String nAuteur;

@Override
public void setIsbn(String isbn) {

}

@Override
public void getIsbn() {

}

@Override
public void setAuteur(String auteur) {

}

@Override
public void getAuteur() {

}

@Override
public void setPaginas(int paginas) {

}

@Override
public void getPaginas() {

}

@Override
public void setTitel(String titel) {

}

@Override
public void getTitel() {

}

public void printSB() {
    System.out.println(titel + " " + auteur + " " + paginas + " " + isbn + " " + ondertitel + " " + nAuteur);
}

public void addOndertitel(String ondertitel) {

}

public void addNAuteur(String nAuteur) {

}

I have two more subclasses after this but I don't think it is necessary for the code to work, because the code in both other subclasses are exactly the same and give the exact same output.

Again, any help is appreciated. Thanks in advance.

Sincerely,

Double

Your setters aren't doing anything.

A correct way to implement eg your setter for isbn would be:

@Override
public void setIsbn(String isbn) {
    this.isbn = isbn;
}

Note the part where it actually sets something.

Your getters are equally wrong. A getter should return something, ie not be declared as void and have a return statement:

@Override
public String getIsbn() {
    return this.isbn;
}

I know they should contain a return value, but the return value gives an error and whenever I leave it empty it seems fine. (from the comments)

I'm guessing the error is something along "a void method cannot return anything" and yes, it will be fine - unless you try something like

System.out.println(sb.getIsbn()); // Will also print "null"

I'll leave the rest up to you.


Bonus 1: If you have an abstract parent class anyway, you can move the common getters and setters to Boek instead of just declaring them as abstract . This will save you from having to re-implement them in each of your subclasses again.

Bonus 2: public fields (as in public String isbn; ) are usually discouraged. You already have the getters and setters, so make them protected (see also: encapsulation ).

Bonus 3: As already pointed out by Mike 'Pomax' Kamermans in the comments: You don't want to have your main method in your Boek class. Create a separate "application" class with only the main method to start up your application.

Bonus 4: I believe the standard way of what you want to achieve with your printSB , printRO and printDB methods would be to override the toString() method. Although this might be different for your special use case.

Use it as following:

public class Studieboek extends Boek {
    ...

    @Override
    public String toString() {
        return this.isbn + " " + this.auteur; // Plus the others
    }
}

And

// No need to call an additional method, toString will be invoked automatically
System.out.println(db);

Your set and get methods are empty. You need to set a value in the setters and return the value in the getters .

Right now you are trying to print a variable that has not yet been set - so yes, it is going to print null .

public class FooClass{
    private String foo;

    public void setFoo(String foo) {
        this.foo = foo;
    }

    public String getFoo() {
        return foo;
    }
}

You are printing the variable, whose values have not set. You have set the variable values in setter methods in order to print the values. Setter methods to set the values. And getter methods to return the values.

@Override
public void setTitel(String titel) {
     this.titel=titel;
}

@Override
public String getTitel() {
   return titel;
}

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