Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > cannot initialise variable in try block?

Reply
Thread Tools

cannot initialise variable in try block?

 
 
Nandan
Guest
Posts: n/a
 
      04-28-2005

The java compiler (1.4.2) won't compile the foll, it says cl may not
have been initialized.

Class cl;
try {
cl = gui.getClass();
Method hidden = cl.getMethod("isVisible", null);
Boolean falsebool = new Boolean(false); //for shame
if((Boolean) hidden.invoke(gui,null)==falsebool) { //lordy!
System.err.println("Obj is hidden " + key);
continue;
}
Object o = valuemethodmap.get(cl);
if (o == null)
continue;
String mtname = o.toString();
/*
* System.err.println("ID " + key + " isa " + gui.getClass() + "
* and methodmap gives " + mtname);
*/
Method mt = cl.getMethod(mtname, null);
Object retval = mt.invoke(gui, null);
Element guielem = new Element("control").setAttribute("name",
key.toString());
Element tentsel = new Element("contents").setAttribute(
"method", mtname);
tentsel.setText(retval.toString());
guielem.addContent(tentsel);
problem.addContent(guielem);
} catch (NoSuchMethodException nsme) {
System.err.println("Method " + nsme + "not found for" + cl);
}


How can a catch statement be reached without at least one statement being
(willan on-being) executed? Is this in the JVM spec or something, that
exceptions may (say) be raised in another thread or something?

Or is this another discipline issue, like not compiling on unreachable code?

--
Nandan Bagchee

So I think people who are mature and experienced, with a business background,
may be overrated. We used to call these guys 'newscasters', because they had
neat hair and spoke in deep, confident voices, and generally didn't know much
more than they read on the teleprompter.

-- Paul Graham


 
Reply With Quote
 
 
 
 
=?ISO-8859-1?Q?Daniel_Sj=F6blom?=
Guest
Posts: n/a
 
      04-28-2005
Nandan wrote:
> The java compiler (1.4.2) won't compile the foll, it says cl may not
> have been initialized.
>
> Class cl;
> try {
> cl = gui.getClass();

<snip rest of try block>
> } catch (NoSuchMethodException nsme) {
> System.err.println("Method " + nsme + "not found for" + cl);
> }
>
> How can a catch statement be reached without at least one statement being
> (willan on-being) executed?


If getClass() in the code above would throw NoSuchMethodException, no
value would be assigned to cl, since the assignment would never take
place. That is the reason you get the error.

To fix it, move the "cl = gui.getClass();" line outside the try block.
In general, don't put non-throwing code in try blocks.

--
Daniel Sj÷blom
Remove _NOSPAM to reply by mail
 
Reply With Quote
 
 
 
 
Tom McGlynn
Guest
Posts: n/a
 
      04-28-2005
Daniel Sj÷blom wrote:

> Nandan wrote:
>
>> The java compiler (1.4.2) won't compile the foll, it says cl may not
>> have been initialized.
>>
>> Class cl;
>> try {
>> cl = gui.getClass();

>
> <snip rest of try block>
>
>> } catch (NoSuchMethodException nsme) {
>> System.err.println("Method " + nsme + "not found for"
>> + cl);
>> }
>>
>> How can a catch statement be reached without at least one statement being
>> (willan on-being) executed?

>
>
> If getClass() in the code above would throw NoSuchMethodException, no
> value would be assigned to cl, since the assignment would never take
> place. That is the reason you get the error.
>
> To fix it, move the "cl = gui.getClass();" line outside the try block.
> In general, don't put non-throwing code in try blocks.
>


Just to expand on this point...

You're the one who said that the compiler shouldn't rely on the following
code executing. You put it in the try block. So the compiler is just
doing what you told it. That's what it means to put it in the try block.
"Try this code but carefully and be ready to handle an exception at any point."

Note that if gui==null then you will indeed manage to fail before cl gets initialized.

One quick way to get the code to work without moving things around, is to
simply set cl to null when you define it. Being equal to null is not the
same as being unitialized.

By the by, the error message probably needs to be something like

"Method "+mename+" not found for "+cl+". Exception: "+nsme

since nsme is not the name of the method, but the exception you
got when you looked for it. But you'll need to define mtname
before the try block then too.

Regards,
Tom McGlynn

 
Reply With Quote
 
Robert
Guest
Posts: n/a
 
      04-28-2005
just set cl = null first outside the try block.

 
Reply With Quote
 
Tor Iver Wilhelmsen
Guest
Posts: n/a
 
      04-29-2005
Nandan <(E-Mail Removed)-state.edu> writes:

> Class cl;
> try {
> cl = gui.getClass();


Just use "Class cl = null;" for that first line - method-local
variables do not have the same default initialization as fields have.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to initialise member variable in template construction Angus C++ 8 09-01-2010 11:06 PM
C#Net2008 Initialise DateTime Variable Lennie_Kuah@xtra.co.nz C Programming 0 05-05-2010 12:45 AM
How to create an initialise pointer to pointer to function variable; iceColdFire C++ 9 05-23-2005 07:05 AM
Try, Try, Try, again... Rick12N4@netscape.net Computer Support 3 01-29-2005 04:02 PM
[Q] Initialise a struct with variable length cahracter strings Stuart Norris C Programming 5 09-29-2003 04:12 AM



Advertisments