Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The local variable might not have been initialized - Detect unchecked exception throw within a method

I have some code with this structure:

public void method() {
    Object o;
    try {
        o = new Object();
    } catch (Exception e) {
        //Processing, several lines
        throw new Error(); //Our own unchecked exception
    }
    doSomething(o);
}

I have quite a few methods in which I have the same code in the catch block, so I want to extract it to a method so that I can save some lines. My problem is, that if I do that, I get a compiler error " The local variable o might not have been initialized".

public void method() {
    Object o;
    try {
        o = new Object();
    } catch (Exception e) {
        handleError();
    }
    //doSomething(o); compiler error
}


private void handleError() throws Error {
    //Processing, several lines
    throw new Error();
}

Is there any workaround?

like image 871
Luis Sep Avatar asked Aug 07 '13 09:08

Luis Sep


People also ask

What happens when the local variable is not initialized?

If the programmer, by mistake, did not initialize a local variable and it takes a default value, then the output could be some unexpected value. So in case of local variables, the compiler will ask the programmer to initialize it with some value before they access the variable to avoid the usage of undefined values.

Why is my variable not initialized?

Notice that a variable that is not initialized does not have a defined value, hence it cannot be used until it is assigned such a value. If the variable has been declared but not initialized, we can use an assignment statement to assign it a value.

Which exception is raised when user attempts to access a variable that has not been initialized?

NullPointerException is a runtime condition where we try to access or modify an object which has not been initialized yet. It essentially means that the object's reference variable is not pointing anywhere and refers to nothing or 'null'. In the given example, String s has been declared but not initialized.


2 Answers

You need to initialize local variables before they are used as below

public void method() {
    Object o=null;
    try {
        o = new Object();
    } catch (Exception e) {
        handleError();
    }
   doSomething(o); 
}

You will not get the compilation failure until you use local variable which was not initialized

like image 154
Sanjaya Liyanage Avatar answered Oct 14 '22 19:10

Sanjaya Liyanage


Since o is getting initialized within the try block and initializing o might throw an exception, java thinks that doSomething(o) statement might reach without o being initialized. So java wants o to be initialized incase new Object() throws exception.

So initializing o with null will fix the issue

public void method() {
    Object o = null;
    try {
        o = new Object(); //--> If new Object() throws exception then o remains uninitialized
    } catch (Exception e) {
        handleError();
    }
    if(o != null)
      doSomething(o);
}
like image 25
sanbhat Avatar answered Oct 14 '22 19:10

sanbhat