0

I'm having troubles finding out why my code is displaying a null pointer exception error when it runs the getScale () method from the subclass "Weather". The getScale method in the subclass is suppose to change the private variable (scale) depending on the short form in to the long form (return Celcius instead of C).

*PS this is a school computer science inheritance exercise

Thanks for any feedback.

Main Method

    Temperature number = new Temperature ();
    Temperature scale = new Temperature ();

    System.out.print ("Enter temperature: ");
    number.setNumber (Double.parseDouble (stdin.readLine ()));
    System.out.print ("Enter scale (C/F/K): ");
    scale.setScale (stdin.readLine ());

    Weather windSpeed = new Weather ();
    Weather windDirection = new Weather ();
    Weather visibility = new Weather ();

    System.out.print ("Enter wind speed (km/h): ");
    windSpeed.setWindSpeed (Double.parseDouble (stdin.readLine ()));
    System.out.print ("Enter wind direction: ");
    windDirection.setWindDirection (stdin.readLine ());
    System.out.print ("Enter visibility (m): ");
    visibility.setVisibility (Double.parseDouble (stdin.readLine ()));

    System.out.println ("\nTemperature: " + number.getNumber () + " " + scale.getScale ());
    System.out.println ("Wind Speed: " + windSpeed.getWindSpeed () + " km/h");
    System.out.println ("Wind Direction: " + windDirection.getWindDirection ());
    System.out.println ("Visibility: " + visibility.getVisibility () + " m");

    Temperature scaleFull = new Weather ();

    System.out.println ("\nTemperature: " + number.getNumber () + " " + scaleFull.getScale ());
    System.out.println ("Wind Speed: " + windSpeed.getWindSpeed () + " km/h");
    System.out.println ("Wind Direction: " + windDirection.getWindDirection ());
    System.out.println ("Visibility: " + visibility.getVisibility () + " m");

Temperature Class (superclass)

private double number;
private String scale;   

public void setNumber (double number)
{
    this.number = number;
}


public double getNumber ()
{
    return number;
}


public void setScale (String scale)
{
    this.scale = scale;
}


public String getScale ()
{
    return scale;
}

Weather Class (subclass extends Temperature)

 private double windSpeed;
private String windDirection;
private double visibility;

public void setWindSpeed (double windSpeed)
{
    this.windSpeed = windSpeed;
}


public void setWindDirection (String windDirection)
{
    this.windDirection = windDirection;
}


public void setVisibility (double visibility)
{
    this.visibility = visibility;
}


public double getWindSpeed ()
{
    return windSpeed;
}


public String getWindDirection ()
{
    return windDirection;
}


public double getVisibility ()
{
    return visibility;
}


public String getScale ()
{
    if ((super.getScale ()).equals ("C"))
    {
        return ("Celcius");
    }
    else if ((super.getScale ()).equals ("F"))
    {
        return ("Fahrenheit");

    }
    else if ((super.getScale ()).equals ("K"))
    {
        return ("Kelvin");
    }

    return (" units");

}
J.King
  • 21
  • 5

0 Answers0