Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Not A Statement - Java Error

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Not A Statement - Java Error

    I am trying to compile below code in JDK 1.6 but getting an error "Not a statement" in the below line
    File file = new File("/data/userfiles/data/conf/dataConfig.xml"); Since this code has been compiled in previous version like 1.3 and 1.4 version.

    Can you please tell me what exactly wrong in this because presence of dataConfig.xml need not be checked in the compile time, it has to be checked only during execution.

    Kindly provide me a solution , i have to provide this by tomorrow



    Source Code

    try
    {
    if (loglevel != 0)
    // writelog("Retrieving configuration");
    File file = new File("/data/userfiles/data/conf/dataConfig.xml");
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.parse(file);
    doc.getDocumentElement().normalize();
    NodeList nodeLst = doc.getElementsByTagName("Configuration");
    String servername;
    Node fstNode = nodeLst.item(0);
    Element fstElmnt = (Element)fstNode;
    NodeList fstNmElmntLst = fstElmnt.getElementsByTagName("Server");
    Element fstNmElmnt = (Element)fstNmElmntLst.item(0);
    NodeList fstNm = fstNmElmnt.getChildNodes();
    servername = ((Node)fstNm.item(0)).getNodeValue();



    Element sndElmnt = (Element)fstNode;
    NodeList sndNmElmntLst = sndElmnt.getElementsByTagName("Log");
    Element sndNmElmnt = (Element)sndNmElmntLst.item(0);
    NodeList sndNm = sndNmElmnt.getChildNodes();

    String nodeval;
    Node mynode = (Node)sndNm.item(0);
    nodeval = mynode.getNodeValue().trim();
    loglevel = Integer.parseInt(nodeval);



    sndElmnt = (Element)fstNode;
    sndNmElmntLst = sndElmnt.getElementsByTagName("LogPath");
    sndNmElmnt = (Element)sndNmElmntLst.item(0);
    sndNm = sndNmElmnt.getChildNodes();


    mynode = (Node)sndNm.item(0);
    logpath = mynode.getNodeValue().trim();
    if (loglevel != 0)
    writelog("Log Path: " + logpath);
    if (loglevel != 0)
    writelog("Server : " + ((Node)fstNm.item(0)).getNodeValue());



    return servername;

    }
    catch (Exception e)
    {
    writelog("Error in Get Server IP:" + e.toString());
    return ("ERROR");
    }
    Last edited by shagil.a.gopinath; September 22nd, 2012 at 03:10 AM.


  2. #2
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Not A Statement - Java Error

    Hello shagil.a.gopinath!
    Please edit your post and use the code tags to improve its readability. And also it would help if you provided the full error message.

    A notice on your code;
    if (loglevel != 0)
    .
    .
    .
    You should place the statements that will execute if the condition is true, in the same block (ie inside the same pair of {}'s) The way it is now, only the first statement after if will execute. ie only
    File file = new File("/data/userfiles/data/conf/dataConfig.xml");
    Is this what you want or you want all the statements to execute?
    If it is the latter then you should place in the same block.

    Hope this helps.

  3. #3
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Not A Statement - Java Error

    Quote Originally Posted by andreas90 View Post
    ...The way it is now, only the first statement after if will execute...
    The way it is now, when loglevel != 0, everything will execute.
    When loglevel == 0, everything except the following line will execute:File file = new File("/data/userfiles/data/conf/dataConfig.xml");

  4. #4
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Not A Statement - Java Error

    This is a compiler error, not an execution error. The error manifestation at compile time does not depend on the value of the expression in the parentheses of the if() statement.

    (My setup: java and javac versions 1.6. I don't know what happened with earlier versions.)

    The following gives compiler errors regardless of the value of the variable in parentheses (At compile time, it doesn't evaluate the variable values.) Since you commented out the statement after the if(), this is the code that the compiler sees
            if (SomeBooleanExpression)
                File file = new File("whatever");

    I mean, even if the compiler didn't barf, that statement couldn't possibly do anything useful, since the variable named "file" would go out of scope immediately after being created.

    The following also gives compiler errors: (It's not just some quirk of File objects or any other particular type of variable.)
            if (SomeBooleanExpression)
                int x = 10;

    The following does not give compiler errors (assuming SomeBooleanExpression has a defined boolean value):
            if (SomeBooleanExpression)
            { /* No executable statements inside the braces */ }
            File file = new File("whatever"); // Always executed
    Perhaps this suggests one possible "fix" that will make the program compilable (and, therefore, executable) so that the assignment can be completed.


    Interestingly, the following also does not give compiler errors (assuming SomeBooleanExpression has a defined boolean value), even though the statement inside the block can't possibly do anything useful, and, in fact may be optimized away:
            if (SomeBooleanExpression)
            {
                File file = new File("whatever"); // Won't do anything useful
            }
    Of course, in this case, you will get compiler errors after this statement if any subsequent expressions use this "file" variable, since it will not be in scope.

    Anyhow...

    I'm sure that's not what you had in mind, but I think it's interesting to note that the first part of the compiler phase doesn't look inside the block to see if it does anything useful, but if it's a single useless statement (not inside a {} block) after the if(), the compiler decides not to waste any time trying to create something executable.



    Bottom line: I think most experienced programmers recommend that you always use {} braces after if () statements even if you don't need them. Then, if you change the program (maybe by adding print() stuff for debugging or maybe by commenting out things for testing), perhaps you will have a smaller chance of running into trouble.



    Cheers,

    Z
    Last edited by Zaphod_b; September 22nd, 2012 at 12:52 PM.

  5. #5
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Not A Statement - Java Error

    I think the compiler is less concerned with whether the statement has any effect and more with the fact that the following is just wrong:

    if (loglevel != 0)
        File file = new File("/data/userfiles/data/conf/dataConfig.xml");

    "Every local variable declaration statement is immediately contained by a block." (That's JLS 14.4. Local Variable Declaration Statements). This rule of Java's is a good one for making it crystal clear what the scope of file is. And it is yet another reason to follow Zaphod's advice and use braces with if statements.

    ---

    In fact the assignment comes very close to doing something (if it would compile, that is).

    String filename = ...
    if (loglevel != 0) {
        File file = new File(filename);
    }

    The declaration and assignment looks useless, but it would do something at runtime. Sometimes.

Similar Threads

  1. [SOLVED] How to modify core Java interface java.sql.Statement.execute(String sql)?
    By amughost in forum Java Theory & Questions
    Replies: 6
    Last Post: June 9th, 2012, 04:31 PM
  2. MySQL Syntax error in Java prepared statement
    By kc120us in forum JDBC & Databases
    Replies: 4
    Last Post: March 22nd, 2012, 11:31 AM
  3. Java If Statement
    By thretz in forum What's Wrong With My Code?
    Replies: 9
    Last Post: March 8th, 2012, 07:57 PM
  4. Methods Help - Unreachable statement error
    By jessica202 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 14th, 2011, 10:24 AM
  5. If Statement Java Need help
    By Keno888 in forum Loops & Control Statements
    Replies: 5
    Last Post: October 25th, 2009, 01:53 AM

Tags for this Thread