cannot initialise variable in try block?

Discussion in 'Java' started by Nandan, Apr 28, 2005.

  1. Nandan

    Nandan Guest

    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
    Nandan, Apr 28, 2005
    #1
    1. Advertising

  2. 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
    =?ISO-8859-1?Q?Daniel_Sj=F6blom?=, Apr 28, 2005
    #2
    1. Advertising

  3. Nandan

    Tom McGlynn Guest

    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
    Tom McGlynn, Apr 28, 2005
    #3
  4. Nandan

    Robert Guest

    just set cl = null first outside the try block.
    Robert, Apr 28, 2005
    #4
  5. Nandan <-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.
    Tor Iver Wilhelmsen, Apr 29, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. iceColdFire
    Replies:
    9
    Views:
    505
    John Carson
    May 23, 2005
  2. Stuart Norris
    Replies:
    5
    Views:
    501
    Dave Thompson
    Sep 29, 2003
  3. morrell
    Replies:
    1
    Views:
    937
    roy axenov
    Oct 10, 2006
  4. Replies:
    0
    Views:
    497
  5. Angus
    Replies:
    8
    Views:
    437
    Francesco S. Carta
    Sep 2, 2010
Loading...

Share This Page