0

This line of code gives the following warning:

    short[] sh = null;

    for (int i = 0, n = b.length; i < n; i++) {
        sh[i] = 0;

    }  

warning: The variable sh can only be null at this location.

short[] sh;

for (int i = 0, n = b.length; i < n; i++) {
    sh[i] = 0;

} 

And, this code gives the following warning:

warning: The local variable sh may not have been initialized.

5 Answers 5

2

This is because you need to initialize the array. Try this:

short[] sh = new short[b.length];

If you don't initialize, you will get those warnings, and will get NullPointerException if you run it.

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

Comments

1

You just declared a variable.

You need to create the array:

short[] arr = new short[size];

Comments

0

sh will always be null in your code:

short[] sh = new short[b.length];

Comments

0

Initialization means to create the array, in Java use the "new" keyword

short[] arr = new short[10];

Comments

0

sh is a variable that represents an array of shorts.

warning: The variable sh can only be null at this location.

sh is initialized but not properly, it is null:

short[] sh = new short[b.length];

warning: The local variable sh may not have been initialized.

Since the local variables are not initialized automatically like instance variables you have to initialize it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.