0
class temperature {
    public double degrees = 0;
    public boolean isCelcius = false;

    public double returnDegrees(){
        return this.degrees;
    }

    public void setTemperature(double temeperatureT, boolean isCelciusT){
        this.degrees = temeperatureT;
        this.isCelcius = isCelciusT;
    }

    public void convertToC (){
        if (this.isCelcius == false) {
            this.degrees = ( 5 * (degrees - 32) ) / 9;
            this.isCelcius = true;
        }
        return;
    }

    public void convertToF (){
        if (this.isCelcius == true){
            this.degrees = ( ( 9 * degrees ) / 5 ) + 32;
            this.isCelcius = false;
        }
        return;
    }   
}



 public static temperature convertStringToTemperature(String tempString){
        temperature tempTemp;
        String split[] = tempString.split(" ");
        tempTemp.degrees = Double.parseDouble( split[0] )
        if (split[1] == "F")
            tempTemp.isCelcius = false;
        else if(split[1] == "C")
            tempTemp.isCelcius = true;
        else{
            System.out.println("error determining units");
            tempTemp.isCelcius = true;
        }

        return tempTemp; 
    }

For the Code:

>tempTemp.setTemperature(Double.parseDouble( split[0] ), true);

it says that the variable tempTemp may not have been initialized although it doesn't say it below for the other uses of that variable. What is my issue here?

Thanks, this is my first time making a custom class in java.

4
  • 4
    temperature tempTemp = new temperature(); Commented Sep 29, 2014 at 20:50
  • 2
    Obligatory sidenote - Java naming conventions use PascalCase for class names. Commented Sep 29, 2014 at 20:52
  • By the way, always use an uppercase 1st letter when naming a class. And you can as well assign a null when you are sure, that before using a field it gets initialized by some statement, but then the compiler is happy. Commented Sep 29, 2014 at 20:52
  • Somebody downvoted this answer @ChrisChevalier, but I don't know why. It is a well written question despite being from a newbie perspective. You may want to read How to Ask, for reference, but well done anyway. Commented Sep 29, 2014 at 20:56

6 Answers 6

1

Thats because you only created a reference to an object without creating the object itself.

temperature tempTemp = new temperature();

now the reference is initialized to point to a real object.

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

Comments

1

temperature tempTemp; just creates local variable but not point/refer to any object or does not get assigned any value. Before invoking a method on an object, you need to get handle(reference) for the object.

temperature tempTemp = new temperature();

creates a local variable tempTemp and this variable refers/points to a temperature object and on the next line when tempString.split(" "); is executed, the split method is invoked on the object created on previous line.

Note that you can even initialize tempTemp variable with null, however that would result in NullPointerException in the next line.

Comments

1

At the line

temperature tempTemp;

you only create the variable, but you still need to initialize it. In order to initialize it, you need to allocate it to an object such as

temperature tempTemp = new temperature();

Comments

0

At the line

temperature temptemp;

you are declaring a variable called temptemp of temperature class. However, you are not initializing it. You initialize it by associating it to an object, like this:

temperature temptemp = new temperature();

in the code above you instantiate temperature and assign the object resulting from the instantiation to temptemp, which is of temperature class. So this line declares your variable and initializes it as well. If you do not specify what your object is, your intention cannot be guessed.

Comments

0

Because tempTemp is not initialized inside convertStringToTemperature method.

This is an inbuilt feature that Java language has - this guarantees that the variable or objects are initialized before they can be used.

In your case, tempTemp instance of temperature class is local to convertStringToTemperature method and is not initialized.

And, this is true for local variables also. Try defining a primitive type inside your method without initializing and do a simple mathematical operation on it. You will get the same error from the Java compiler.

Also, start Java Class/ Type name with uppercase letter and for anything else follow camel casing as already pointed out in other answers.

Comments

0
  1. If your convertStringToTemperature method on same class then you can use this.setTemperature() but your method can not be static in that case. As you cannot use "this" in a static context.

  2. If your convertStringToTemperature method in different class You need to initialize your temperature object or you can pass temperature object in parameter like bellow:

public static temperature convertStringToTemperature(String tempString,temperature tempTemp){

         //temperature tempTemp = new temperature();            
        String split[] = tempString.split(" ");
        tempTemp.degrees = Double.parseDouble( split[0] );
        if (split[1] == "F")
            tempTemp.isCelcius = false;
        else if(split[1] == "C")
            tempTemp.isCelcius = true;
        else{
            System.out.println("error determining units");
            tempTemp.isCelcius = true;
        }

        return tempTemp; 
    }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.