0

I have 3 classes : diet, dietTest and weeklyDiet

this is my code for diet :

public class Diet 
{
    private double weight;
    private double calorie;
    
    public double getW()
    {
        return weight;
    }
    
    public double getC()
    {
        return calorie;
    }
    
    public void setW(double w)
    {
        weight = w;
    }
    
    public void setC(double c)
    {
        calorie = c;
    }
    
    public Diet(double w, double c)
    {
        setW(w);
        setC(c);
    }
}

this is my code for weeklyDiet:

public class WeeklyDiet 
{
    private Diet [] onDiet;
    
    public void retrieveDietRecord(Diet[] d)
    {
        Diet [] onDiet = d;
    }
    
    public double highestCalorieIntake()
    {
        double highest = onDiet[0].getC();
        
        for(Diet calories : onDiet)
        {
            if(calories.getC() > highest)
            {
                highest = calories.getC();
            }
        }
        
        return highest;
    }
    
    public double averageWeight()
    {
        double total = 0;
        
        for(int i = 0 ; i < onDiet.length ; i++)
        {
            total += onDiet[i].getW();
        }
        
        return total / 2;
    }
}

this is my code for dietTest:

public class DietTest 
{
    public static void main(String [] args)
    {
        Diet d1 = new Diet(56.8, 1500);
        Diet d2 = new Diet(59.1, 1850.5);
        
        Diet [] myDiet = new Diet[2];
        myDiet[0] = d1;
        myDiet[1] = d2;
        
        WeeklyDiet w = new WeeklyDiet();
        w.retrieveDietRecord(myDiet);
        
        w.averageWeight();
        
    }
}

By running this code, seems like my Diet array in WeeklyDiet is not initialised, because the getAverage and findHighest methods cant use the data in array. I want to ask why when i execute w.averageWeight() method, the system show nullPointerException.

shmosel
  • 49,289
  • 6
  • 73
  • 138
Nino
  • 11
  • 3
  • In `retrieveDietRecord()` you're assigning `d` to a local variable instead of the field. Change it to `onDiet = d;`. – shmosel Apr 07 '22 at 06:18
  • javascript is not java. Also you should change method names that describe what the method does. retrieveDietRecord is not a good name to set a value, since you don't actually retrieve anything. – Stultuske Apr 07 '22 at 06:28

2 Answers2

1

You are initializing array, which exists only in retrieveDietRecord

    public void retrieveDietRecord(Diet[] d)
    {
        Diet [] onDiet = d;
    }

Diet [] onDiet = d; This is not the instance variable onDiet in WeeklyDiet, but a local variable, whose scope is only inside the method. If you want to initialize the instance variable, you have to do it like this:

public void retrieveDietRecord(Diet[] d) {
   this.onDiet = d;
}

Keyword this is not mandatory in this exact case, but it's not a bad idea to be explicit.

Chaosfire
  • 4,818
  • 4
  • 8
  • 23
-2
public void retrieveDietRecord(Diet[] d) {
   this.onDiet = d;
}
Tushar
  • 13
  • 5
  • 2
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 07 '22 at 06:46