0

I have omitted irrelevant parts of the code:

[...]
    try {
        URL url = new URL(updateUrl);
        BufferedReader input = new BufferedReader(new InputStreamReader(url.openStream()));
[...]
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
    } finally {
        input.close();
    }
[...]

The problem is that on the finally "input.close()" Eclipse says that "input cannot be resolved".

I think it may be an scope problem, but I have seen code from other guys and it has usually this same form, so I do not know why it is not working here.

Any hints?

Thanks a lot in advance,

Fran Marzoa
  • 4,293
  • 1
  • 37
  • 53

3 Answers3

2

It is indeed a scope error.
Your input is declared inside the try block, so it can't be seen inside the finally block. Declare it outside, so that it is visible to both, and you should be fine:

[...]
    BufferedReader input = null;
    try {
        URL url = new URL(updateUrl);
        input = new BufferedReader(new InputStreamReader(url.openStream()));
[...]
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
    } finally {
        if (input != null)
        {
            try {
              input.close();
            }
            catch (IOException exc)
            {
              exc.printStackTrace();
            }
        }
    }
[...]
  • Thanks a lot. Sometimes after too much coding you are so offuscated that does not see such obvius things. I will select your answer as soon as stackoverflow let me doing it (it says that I must wait seven minutes) – Fran Marzoa Jul 06 '12 at 09:25
  • 1
    Make sure that in your `finally` block you check whether `input` is `null` before trying to close it. If your URL is malformed, `input` will never be assigned to, but you'll still try to call close on it. – hcarver Jul 06 '12 at 09:28
  • @Hbcdev Good point, updated the code. And Fran: you're welcome. – S.L. Barth is on codidact.com Jul 06 '12 at 09:32
  • Erm... now I do not know who was the first that gives the right answer. I think it was you, because the first time I see these answers your one was first... – Fran Marzoa Jul 06 '12 at 16:29
1

declare BufferedReader input instance globally or outside first try/catch block as:

[...]
BufferedReader input;
    try {
        URL url = new URL(updateUrl);
        input = new BufferedReader(new InputStreamReader(url.openStream()));
[...]
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
    } finally {
        input.close();
    }
[...]
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
1

You're right, it is a scope problem. Java uses block scope, which means local variables declared in one scope are invisible in any scope that is not contained within it. try blocks and finally blocks are not exceptions to this rule.

BufferedReader input;
try {
    URL url = new URL(updateUrl);
    input = new BufferedReader(new InputStreamReader(url.openStream()));
} catch (MalformedURLException e) {
    e.printStackTrace();
} catch (IOException e) {
} finally {
    if (input != null) {
        try {
            input.close();
        } catch (IOException e) {
            // Log or ignore this
        }
    }
}
Darshan Rivka Whittle
  • 32,989
  • 7
  • 91
  • 109