-1

I am getting the error in Java:

Exception in thread "main" java.lang.Error: Unresolved compilation problem
The local variable b1 may not have been initialized at Test.main(Test.java:20)

Here is my code:

import java.lang.String;
public class Test {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        char a[]={'s','k'};
        String s0=new String(a);
        String s1=new String("Amar");
        String s2= "amarnath";
        byte[] b1;
        try{
         b1=s2.getBytes("ASCII");
        }
        catch(Exception e){}
        for(int i =0;i<s2.length();i++)
        {
            System.out.println(b1[i]);
        }

    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
smasher
  • 294
  • 2
  • 17
  • 3
    consider moving `for(...)` inside `try` after `b1 = ... ` to print array contents only if it is successfully initialized. Now, if `b1` is not assigned due to exception, it is not initialized when entering `for()`--thus you're getting this error. – Alex Salauyou Apr 15 '16 at 15:49
  • Don’t swallow exceptions.It’s not only good general advice, I think it will also solve your concrete problem. – Ole V.V. Apr 15 '16 at 16:06
  • @SashaSalauyou thanks .. it worked.. But can you tell why my code is having the problem. Is this due to the try scope ? – smasher Apr 15 '16 at 18:58

3 Answers3

1

PROBLEM

If there is an error inside the try block, then b1 would not have been initialized and would have a value of null.

If this happens, then there would be a problem when you try and print out b1's values in the following line:

System.out.println(b1[i]);

Java is cautious and won't let the possibility of that happen.


SOLUTIONS

  1. Initialize b1 with a default value
    • This will prevent the possibility that b1 is uninitialized (which is what Java worried about)
  2. Put the for loop inside the try block (as @SashaSalauyou said in the comments)
    • If there is an error with b1's inialization in the try block, then the block will quit, and the for loop would not be run. Therefore, there would be no code using the uninitialized variable such as in aforementioned line of code.
Jonathan Lam
  • 16,831
  • 17
  • 68
  • 94
1

The compiler complains because if the b1 is not initialized and if something bad happens inside the try block, it will be then used in the following block, System.out.println(b1[i]);, uninitialized.

In order to make it compile, you should initialize your variable at least with null, as it is local variable.

Ioan
  • 5,152
  • 3
  • 31
  • 50
0

The problem here is that getBytes(String encoding) throws UnsupportedEncodingException. If such an exception occurs in your code (this will happen if you put wrong encoding for example getBytes("foo")), your byte array will remain uninitialized. The catch block will handle the exception and continue with the for loop where you are trying to use b1. The compiler sees that possibility and throws an error in order to prevent usage of uninitialized variable later.

public class Test {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        char a[]={'s','k'};
        String s0=new String(a);
        String s1=new String("Amar");
        String s2= "amarnath";
        byte[] b1;
        try{
         b1=s2.getBytes("ASCII");
         for(int i =0;i<s2.length();i++)
         {
            System.out.println(b1[i]);
         }
        }
        catch(UnsupportedEncodingException uee){ 
            //handle the exception or at least log it
        }
    }
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Altair
  • 168
  • 1
  • 9