Sams Teach Yourself Java 2 in 24 Hours
- Table of Contents
- Copyright
- About the Author
- About the Technical Editor
- Acknowledgments
- We Want to Hear from You!
- Reader Services
- Introduction
- Hour 1. Becoming a Programmer
- Hour 2. Writing Your First Program
- Hour 3. Vacationing in Java
- Hour 4. Understanding How Java Programs Work
- Part II: Learning the Basics of Programming
- Hour 5. Storing and Changing Information in a Program
- Hour 6. Using Strings to Communicate
- Hour 7. Using Conditional Tests to Make Decisions
- Hour 8. Repeating an Action with Loops
- Part III: Working with Information in New Ways
- Hour 9. Storing Information with Arrays
- Hour 10. Creating Your First Object
- Hour 11. Describing What Your Object Is Like
- Hour 12. Making the Most of Existing Objects
- Part IV: Programming a Graphical User Interface
- Hour 13. Building a Simple User Interface
- Hour 14. Laying Out a User Interface
- Hour 15. Responding to User Input
- Hour 16. Building a Complex User Interface
- Part V: Creating Multimedia Programs
- Hour 17. Creating Interactive Web Programs
- Hour 18. Handling Errors in a Program
- Hour 19. Creating a Threaded Program
- Hour 20. Reading and Writing Files
- Part VI: Creating Multimedia Programs
- Hour 21. Using Fonts and Color
- Hour 22. Playing Sound Files
- Hour 23. Working with Graphics
- Hour 24. Creating Animation
- Part VII: Appendixes
- Appendix A. Tackling New Features of Java 2 Version 1.4
- Appendix B. Using the Java 2 Software Development Kit
- Appendix C. Programming with the Java 2 Software Development Kit
- Appendix D. Using Sun ONE Studio
- Appendix E. Where to Go from Here: Java Resources
- Appendix F. This Book's Web Site
Catching Errors as You Set Up URLs
When you set up a URL object, you must make sure that the text used to set up the address is in a valid format. http://java.sun.com and http://www.samspublishing.com are valid, but something such as http:www.javaworld.com would not be because of the missing // marks.
The getURL() method takes a string of text as an argument. The string is checked to see if it's a valid Web address, and if it is, the method returns that valid address. If it's erroneous, the method sends back a null value. The following is the getURL() method:
URL getURL(String urlText) {
URL pageURL = null;
try {
pageURL = new URL(getDocumentBase(), urlText);
}
catch (MalformedURLException m) { }
return pageURL;
}
The first line of this method includes three things, in this order:
- The type of object or variable that is returned by the method—a URL object in this case. If this is void, no information is returned by the method.
- The name of the method—getURL.
- The argument or arguments, if any, that this method takes—only one in this example, a String variable called urlText.
The try-catch block deals with any MalformedURLException errors that occur when URL objects are created.
If the string variable sent to the method is a valid Web address, it will be sent back as a valid URL object. If not, null is returned. Because you were assigning values to six different URL objects in the pageURL array, the getURL() method makes this process easier to do.
Handling Screen Updates in the paint() Method | Next Section

Account Sign In
View your cart