Java Exception Handling

Unconstructed Class object; NoSuchMethodException

Page 1 of 1

4 Replies - 1394 Views - Last Post: 24 December 2009 - 11:18 AM Rate Topic: -----

#1 AugustusLysias  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 3
  • Joined: 24-December 09

Java Exception Handling

Posted 24 December 2009 - 07:11 AM

How can I write a simple, brief program that declares an object of a class, but does not construct it. That is the purpose of the program and the exception handling, to test what happens with a class object that is not constructed when attempting to access a method of that object. That means the class object is not to have a statement such as = new Button(); after it. However, I cannot compile the program without doing so. I then want to call a method with the object to see whether doing so produces an Exception. I would use a try block and a catch block to catch the Exception if there is one, and then display the associated message using the getMessage( ) of the exception thrown to the catch block. The error is deliberate in order to observe exception handling in Java.
It would seem simple, but I keep receiving a message that the object can be initialized before being used when I compile the program. An example is below. Correct suggestions are appreciated in advance. Again, note that this is a deliberate error, so that I can observe the result returned by the handler's getMessage(). Although Error is the class of Exception used, I am thinking NoSuchMethodException because of the non-Constructed object may be more appropriate. Thanx...

import java.awt.*;
public class NonConstruct
{
public static void main(String[] args)throws Exception
{
Button myButton;

try
{ 
myButton.getLabel();
}
catch(Exception error)
{
System.out.println(error.getMessage());
}
}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Java Exception Handling

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Java Exception Handling

Posted 24 December 2009 - 07:24 AM

Button myButton = null;
So the compiler will "see" that has been initialized
Was This Post Helpful? 1

#3 crazyjugglerdrummer  Icon User is offline

  • GAME OVER. NERD WINS.
  • member icon

Reputation: 124
  • View blog
  • Posts: 690
  • Joined: 07-January 09

Re: Java Exception Handling

Posted 24 December 2009 - 07:28 AM

I don't think java even allows unitialized reference variables. You can set it to null (which is what it wants you to do if you're not declaring the container to hold an object) so it doesn't point to an object at all. If you're just trying to get an exception to be thrown, set it to null. :D
Was This Post Helpful? 0
  • +
  • -

#4 AugustusLysias  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 3
  • Joined: 24-December 09

Re: Java Exception Handling

Posted 24 December 2009 - 11:00 AM

Thanks. I did set the button to null as shown, but I'm not certain it created the exception I was expecting and hoping to see. The program did compile and the try block did not create an error, or perhaps it did create the error desired. The console displays "null" which means either that the getLabel() in the try block returned null, or the exception occurred and the getMessage() of the catch block returned the "null" to the console screen. Did the exception occur, and was it the getMessage() of the catch block that produced the null message? Again, thanks for the help and the definitive answer.
Was This Post Helpful? 1

#5 AugustusLysias  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 3
  • Joined: 24-December 09

Re: Java Exception Handling

Posted 24 December 2009 - 11:18 AM

Please forego a response to the last post. I put an error message in the output of the catch block as follows
System.out.println("Error message is " + error.getMessage());
to determine if the exception occurred. The catch block did catch the exception. Thanx...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1