1

I have 3 classes: Main, AutoHuur, Auto. Everythign works but 1 thing. I can't seem to get the "Prijs" variable from my Auto.class to display in my toString in my AutoHuur.class. It keeps showing up as 0. Why is this? (I know I initialize it to 0 if it's a null, but why is it a null and not the value from the Auto.class variable prijsPerDag?) Thank you

Main.class:

    public class Main{
    public static void main(String[] args) {
        AutoHuur ah1 = new AutoHuur();
        System.out.println("Eerste autohuur:\n" + ah1 + "\n");

        Klant k = new Klant("Mijnheer de Vries");
        k.setKorting(10.0);
        ah1.setHuurder(k);
        Auto a1 = new Auto("Peugeot 207", 50.0);
        ah1.setGehuurdeAuto(a1);
        ah1.setAantalDagen(4);
        System.out.println("Eerste autohuur:\n" + ah1 + "\n");

        AutoHuur ah2 = new AutoHuur();
        Auto a2 = new Auto("Ferrari", 3500.0);
        ah2.setGehuurdeAuto(a2);
        ah2.setHuurder(k);
        ah2.setAantalDagen(1);
        System.out.println("Tweede autohuur:\n" + ah2 + "\n");

        System.out.println("Gehuurd: " + ah1.getGehuurdeAuto());
        System.out.println("Gehuurd: " + ah2.getGehuurdeAuto());
    }
}

Autohuur.class:

public class AutoHuur {
    private Klant huurder;
    private Auto gehuurdeAuto;
    private Auto prijs;

    private Integer aantalDagen;

    public AutoHuur(){
    }

    public void setHuurder(Klant nwH){
        huurder = nwH;
    }

    public void setGehuurdeAuto(Auto nwGA){
        gehuurdeAuto = nwGA;
    }

    public Auto getGehuurdeAuto(){
    return gehuurdeAuto;
    }

    public Auto getPrijs(){
    return prijs;
    }

    public void setAantalDagen(Integer nwD){
        aantalDagen = nwD;
    }

public String toString(){
    String s = "";

    if (gehuurdeAuto == null){
        s = s + "er is geen auto bekend\n"; }
    else { 
    s = s + gehuurdeAuto; }

    if (huurder == null){
    s = s + "er is geen huurder bekend\n"; }
    else { 
    s = s + huurder; }

    if (aantalDagen == null){
    s = s + "aantal dagen: 0"; }
    else { 
    s = s + "aantal dagen: " + aantalDagen; }

    if (prijs == null){
        s = s + " en dat kost 0.0"; }
    else { 
    s = s + " en dat kost" + prijs; }

    return s;
}

}

Auto.class:

public class Auto {
    private String type;
    private Double prijsPerDag;

    public Auto(String tp, Double nwPr){
        type = tp;
        prijsPerDag = nwPr;
    }

    public void setPrijsPerDag(Double prPd){
        prijsPerDag = prPd;
    }

    public Double getPrijsPerDag(){
        return prijsPerDag;
    }

    public String toString(){
        String s = type + " met prijs per dag: " + prijsPerDag + "\n";
        return s;
    }
}
7
  • 1
    prijs in AutoHuur is of type Auto. Is this correct? I suspect it should be int or double Commented Mar 14, 2017 at 19:03
  • I was assuming it does need to be Auto, because it is associated to it. But don't ask me, since I can't get it working lol. I did try it with an int/double but that also did not work for me. Commented Mar 14, 2017 at 19:08
  • 1
    What @SilverNak says. A variable of type Auto shouldn't be named prijs, that seems not logical at all. Commented Mar 14, 2017 at 19:09
  • @MCEmperor But I have 'Prijs' in my Auto class. So it should take that value, and not be a standalone variable, right? Commented Mar 14, 2017 at 19:10
  • 1
    @SomeName it does not have to be of that kind. I suppose you want to save a number there, so it should be some datatype being able to hold a number. Commented Mar 14, 2017 at 19:10

3 Answers 3

2

In AutoHuur.class you can get your prijsPerDag variable from Auto.class using an Auto object, e.g.:

gehuurdeAuto.getPrijsPerDag()

You can calculate the price:

aantalDagen * gehuurdeAuto.getPrijsPerDag()

Is this what would you like to do?

Sign up to request clarification or add additional context in comments.

6 Comments

Yes, that's exactly what I need. I tried it and it brings me to a null point exception error. What I did was private Double prijs = gehuurdeAuto.getPrijsPerDag();
You can use gehuurdeAuto.getPrijsPerDag(); only after setting the gehuurdeAuto to an Auto object, otherwise it has null value. You can leave object's attribute uninitalized (Double prijs;) and put prijs = gehuurdeAuto.getPrijsPerDag(); into the second line of your setGehuurdeAuto method (after setting gehuurdeAuto to nwGA).
New error after editing your comment: Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - bad operand types for binary operator '==' first type: double second type: <nulltype> This is for the if (prijs==null) in my AutoHuur class.
Have you created private double prijs;? That is primitive type, not an object, so you can not compare it with null, you can only compare it with 0. Or you can create prijs using the Double class - with capital D (private Double prijs;), and then you can compare it with null.
Wow it actually works, thank you! When I try to optimize my code by for example saying Double totalPrice = aantalDagen * prijs; in my toString function It gives me a null pointer. Could you tell me why? Because if I use that in the if prijs==null part it does work.
|
0

You have to remove the first System.out becasuse in Main.java your instance AutoHuur is empty, so when your want to print the result , the out will be values empty.

Comments

0

The variable prijs is an object, which is initialized to null when an instance of AutoHuur is created. Since prijs is never set, it is always null.

That's why prijs == null always evaluates to true.

You need to set prijs somewhere.


It seems that you need to work on your design desicions. You are, for example, using an Auto class name, while it is in fact a link between a daily charge and a car type. If each link is represented by such an object (in your case Auto), then it absolutely makes no sense to have more than one instance.

For example,

new Auto("Peugeot", 40.0);
new Auto("Peugeot", 40.0);

is technically perfectly valid, but it is just not logical.

Furthermore, if a class holds data of some kind, then it's good to delegate operations upon that data also to that class.

class Auto {

    private String type;
    private double pricePerDay;

    public double calculateTotalPrice(int numberOfDays) {
        return numberOfDays * this.pricePerDay;
    }
}

3 Comments

I don't have my numberOfDays in my Auto class, so then I'd have the exact same problem as I'm having now.
"I don't have my numberOfDays in my Auto class" — Apparently not, but you should.
Actually I'm not allowed to change the Main.class for my assginment, so it has to be in AutoHuur :/. Thanks for pointing it out, knowing the proper way is always a good thing.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.