Write to file only in debug

Discussion in 'Java' started by jny0, Aug 3, 2009.

  1. jny0

    jny0 Guest

    Hi,

    I have a problem where my code will write to a file when I'm stepping
    throught the code in debug, but won't when I run it properly. The
    really wierd thing is, that code which I wrote two weeks ago, and
    which did work then, has also stopped working. I've always had this
    problem when writing code for an applet, but this is just a piece of
    normal text based code.

    Any ideas?

    Regards,
    JNY0
    jny0, Aug 3, 2009
    #1
    1. Advertising

  2. jny0 <> wrote:
    > I have a problem where my code will write to a file when I'm stepping
    > throught the code in debug, but won't when I run it properly. The
    > really wierd thing is, that code which I wrote two weeks ago, and
    > which did work then, has also stopped working. I've always had this
    > problem when writing code for an applet, but this is just a piece of
    > normal text based code.


    Just blind guessing:
    A misuse of "assert"? e.g.: assert myMethodToWriteToFile();

    in the debugger, asserts are active so your code is run.
    in a normal build/run asserts are disabled, and that means
    not just the checking, but also evaluation of the assert-
    expression (the call to "myMethodToWriteToFile()") is skipped.
    Andreas Leitgeb, Aug 3, 2009
    #2
    1. Advertising

  3. jny0

    jny0 Guest

    Hi Andreas,

    Here's the call I use:
    OutputToFile(iGenerationCtr, dPermNegScore, fFile);

    ....and here's the method.
    private static boolean OutputToFile(int iGenerationCtr, double
    dPermNegScore, File fFile){

    try
    {
    String sPermutationString = "";
    BufferedWriter out = new BufferedWriter(new FileWriter(fFile,
    true));
    out.write(Integer.toString(iGenerationCtr)
    +",");
    out.write(sPermutationString);
    out.write((Double.toString(dPermNegScore)));
    out.write("\n");
    out.close
    ();
    }
    catch (Exception e)
    {
    System.out.println("Error Writing to File " + e.getMessage
    ());
    }
    return true;
    }

    How would I implement code which would activate an assert? How would
    this code have stopped working, when it was working aweek ago?

    Regards,
    JNY0
    jny0, Aug 3, 2009
    #3
  4. jny0 <> wrote:
    > Hi Andreas,
    > Here's the call I use:
    > OutputToFile(iGenerationCtr, dPermNegScore, fFile);
    >
    > ...and here's the method.
    > private static boolean OutputToFile(int iGenerationCtr, double
    > [...]
    > }


    Obviously no "assert" in this part.
    Is your entire code completely free of any "assert" statement?

    If any part (not just the immediate caller of OutputToFile) of
    your code has "assert" with a method call, examine that closer.

    assert is a very likely cause for your symptoms, but it isn't the
    only possible one. e.g. Perhaps the code you compile and run is
    not the same as the one you run in the debugger, or you do incre-
    mental builds and it missed on some dependency that turns out
    to be relevant.

    > How would I implement code which would activate an assert? How would
    > this code have stopped working, when it was working aweek ago?


    Well, *something* must have changed since two weeks ago.
    perhaps you inadvertedly added some bug (e.g. added an "assert")
    or changed the compile-scripts (ant?) to the effect of switching
    off assertions. Or maybe some playing in your IDE's configuration
    caused your project to be build differently than before...

    PS: About your code sample: the are guidelines to Java programming
    that I really encourage you to follow: Firstly, don't start
    method names with capital letters, and secondly don't
    "catch(Exception ...)". Rather catch those specific Exceptions
    that you really expect (e.g. IOException, ...).
    Andreas Leitgeb, Aug 3, 2009
    #4
  5. jny0

    Nigel Wade Guest

    jny0 wrote:

    > Hi,
    >
    > I have a problem where my code will write to a file when I'm stepping
    > throught the code in debug, but won't when I run it properly. The
    > really wierd thing is, that code which I wrote two weeks ago, and
    > which did work then, has also stopped working. I've always had this
    > problem when writing code for an applet, but this is just a piece of
    > normal text based code.
    >
    > Any ideas?
    >
    > Regards,
    > JNY0


    Are you absolutely certain that the class files being executed when you "run it
    properly" (presumably outside your IDE) are exactly the same as the class files
    your IDE is single-stepping through? Some IDEs don't re-build
    the "distribution" class/jar files unless you explicitly tell them to.

    --
    Nigel Wade
    Nigel Wade, Aug 3, 2009
    #5
  6. jny0

    jny0 Guest

    Just downloaded a new version of NetBeans, and the problem seems to be
    sorted. Must have been a config setting I changed at some point.
    I've searched, but can find no reference to it?

    At least the problem's sorted.

    Cheers,
    JNY0
    jny0, Aug 3, 2009
    #6
    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.

Share This Page