Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6660949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:13:07+00:00 2026-05-26T02:13:07+00:00

I’m a very new to java, just trying to run some simple programs. I

  • 0

I’m a very new to java, just trying to run some simple programs.
I have this code:

import java.net.*;
import java.io.*;

class example1 {

    public static void main(String args[]){

    try{
        URL hp = new URL("http://www.java2s.com");
        System.out.println("it all worked?");
    }catch (MalformedURLException e){
        System.err.println("New URL failed");
        System.err.println("exception thrown: " + e.getMessage());
    }

    System.out.println(hp.getProtocol());

    }
}

The java compiler “cannot find symbol: hp” which would lead me to believe that the url object, hp is not being created by the line:

URL hp = new URL("http://www.java2s.com");

But shouldn’t the catch statement be reporting an error?

I tried compiling without the try-catch blocks but I was getting an error saying “unreported exception MalformedURLException; must be caught or declared to be thrown”

If i remove the last line that refers to hp, the program compiles and runs but just displays “it all worked?”.

I’m sure there is a simple explanation here but I don’t have much knowledge of java.
Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T02:13:08+00:00Added an answer on May 26, 2026 at 2:13 am

    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.

    This line:

    URL hp = new URL("http://www.java2s.com");
    

    does two things at once. It declares a variable (which is more generally referred to by the compiler as a “symbol”) named hp, which can point to an instance of URL; and it creates an instance of URL and makes hp point to it.

    You interpreted the error to mean “the url object hp is not being created”. So, first of all, hp is not an object — it is at most a reference to an object, and of course it can also be null, in which case it is a reference to nothing. But the symbol hp exists, within the scope of its declaration, regardless of whether an object reference is assigned to it.

    If the object creation had failed — i.e. the new URL ... 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 hp would be null, in which case a valid attempt to dereference the variable hp would result in a NullPointerException.

    All of which is just to illustrate that the error you received has nothing to do with whether hp has been assigned a value, and simply indicates that hp has not been declared within the scope in which you are attempting to use it.

    The issue is that a try 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 try block read simply URL hp;. As shown in the other answers, the resolution to this is to declare hp outside the try block, so that the later reference is valid. (It would also work to move the last line into the try block, but it makes sense to limit the contents of that block only to the statements that require the specific error handling.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have some data like this: 1 2 3 4 5 9 2 6
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I am trying to loop through a bunch of documents I have to put
I have thousands of HTML files to process using Groovy/Java and I need to
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.