-4

I have a constructor:

    private String name;
    private int price;  
  public Fruit (String name, int price){
        this.name = name;
        this.price = price;

        System.out.println("NAME/PRICE SET");       
        if (getCheapestFruit() == null){
            setCheapestFruit(name, price);
            System.out.println("CHEAPEST NAME/PRICE INITIALIZED");
        } else {
            if(getCheapestFruit().price> price){
                setCheapestFruit(name, price);
                System.out.println("CHEAPEST NAME/PRICE SET");
            }           
        }
    }

And I want to set the cheapestFruit.

public Fruit cheapestFruit = null;
public Fruit getCheapestFruit(){
    return this.cheapestFruit;
}
public void setCheapestFruit(String name, int price){
    this.cheapestFruit.price = price;
    this.cheapestFruit.name = name;
}

And this die at this.cheapestFruit.price = price; with null pointer exception. How can I set it correctly?

korte alma
  • 59
  • 6
  • 2
    Hello and welcome to StackOverflow. Please take some time to read the help page, especially the sections named ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) and ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask). And more importantly, please read [the Stack Overflow question checklist](http://meta.stackexchange.com/q/156810/204922). You might also want to learn about [Minimal, Complete, and Verifiable Examples](http://stackoverflow.com/help/mcve). – Clijsters Dec 28 '17 at 13:11
  • @khelwood I have some idea about exception. I have problems with object here, as I think... – korte alma Dec 28 '17 at 13:23

2 Answers2

0

well for one, it is a bad practice to have cheapestFruit as a part of Fruit Object, as whenever you create an object, another cheapest fruit object has to be created. This is why you get the nullpointer exception

Solution: change then cheapestFruit to static with this:

public static Fruit CHEAPEST_FRUIT = null; 

Now you have just one instance of cheapest fruit per any amount of objects Fruit created.

And now the corrected setter:

public static void setCheapestFruit(Fruit fruit){
    CHEAPEST_FRUIT = fruit;
}

Another thing to be considered: since you have a setter, why make variable cheapestFruit public?

Zeta Reticuli
  • 329
  • 2
  • 13
0

Simply putting, you cant have your cheapestFruit inside Fruit class. That, as you pointed out will only end in leading to a StackOverflowException. What you should do to keep your code coherent is hold the cheapestFruitvariable in another entity as FruitStore.

public class Fruit {

    String name;
    int price;

    public Fruit(String name, int price){
      this.name = name;
      this.price = price;
    }
}

public class FruitStore {

    public Fruit cheapestFruit;
    public Fruit expensiveFruit;

    public void setCheapestFruit(Fruit fruit){
       if(fruit.price < cheapestFruit.price){
          cheapestFruit = fruit;
       }else{
          System.out.print("It's not cheapest");
       }
    }
}

Or if you really want to keep everything in the same class which doesn't make a lot of sense you have to create different constructors do some work arounds.

Greggz
  • 1,873
  • 1
  • 12
  • 31
  • The Fruit constructor call the setCheapestFruit, so this not will be an infinite loop? – korte alma Dec 28 '17 at 13:26
  • @kortealma Why would your `Fruit` constructor call `setCheapestFruit`? – khelwood Dec 28 '17 at 13:29
  • @khelwood Why wouldn't it ? I guess he wants to update the `cheapestFruit` if the `price` of this new `Fruit` is lower. – Greggz Dec 28 '17 at 13:36
  • @khelwood because when I create a Fruit I have to check if the current created is the cheapest, or not, and if is the cheapest than I have to change the cheapest – korte alma Dec 28 '17 at 13:36
  • @kortealma Yes it might give infinite loop but is because you' re thinking this out wrong. Your CheapFruit should be separate from the Fruit class. `Fruit class` should only hold name and price. It shouldn't know about who is the cheapest, that job belongs to other entity, perhaps a `FruitStore` – Greggz Dec 28 '17 at 13:43
  • @kortealma Check my new answer – Greggz Dec 28 '17 at 14:01