Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The other answers have given you some useful advice on avoiding the error. But I would like to try to explain how your understanding of what the error means is confused.</p> <p>This line:</p> <pre><code>URL hp = new URL("http://www.java2s.com"); </code></pre> <p>does two things at once. It declares a variable (which is more generally referred to by the compiler as a "symbol") named <code>hp</code>, which can point to an instance of URL; and it creates an instance of URL and makes <code>hp</code> point to it.</p> <p>You interpreted the error to mean "the url object hp is not being created". So, first of all, <code>hp</code> is not an object -- it is at most a reference to an object, and of course it can also be <code>null</code>, in which case it is a reference to nothing. But the symbol <code>hp</code> exists, within the scope of its declaration, regardless of whether an object reference is assigned to it.</p> <p>If the object creation had failed -- i.e. the <code>new URL ...</code> portion of the statement had failed -- then most likely an exception would have occurred as you expected. But even if for some obscure reason the creation had failed but not thrown an exception, the likely result would be that <code>hp</code> would be <code>null</code>, in which case a valid attempt to dereference the variable <code>hp</code> would result in a <code>NullPointerException</code>.</p> <p>All of which is just to illustrate that the error you received has nothing to do with whether <code>hp</code> has been assigned a value, and simply indicates that <code>hp</code> has not been declared within the scope in which you are attempting to use it.</p> <p>The issue is that a <code>try</code> block creates its own scope, so variables declared within it are not accessible outside the block. You would receive exactly the same error if the first line inside your <code>try</code> block read simply <code>URL hp;</code>. As shown in the other answers, the resolution to this is to declare <code>hp</code> outside the <code>try</code> block, so that the later reference is valid. (It would also work to move the last line into the <code>try</code> block, but it makes sense to limit the contents of that block only to the statements that require the specific error handling.)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload