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?

Signature
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
Daniel Sjöblom - 28 Apr 2005 21:02 GMT
> 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.

Signature
Daniel Sjöblom
Remove _NOSPAM to reply by mail
Tom McGlynn - 28 Apr 2005 21:30 GMT
>> The java compiler (1.4.2) won't compile the foll, it says cl may not
>> have been initialized.
[quoted text clipped - 19 lines]
> 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
Robert - 28 Apr 2005 23:00 GMT
just set cl = null first outside the try block.
Tor Iver Wilhelmsen - 29 Apr 2005 08:10 GMT
> 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.