Syntax error, insert "VariableDeclarators" to complete LocalVariableDeclaration occurs

Viewed 45411
public class Water {
    private Graphic graphic;
    private float speed;
    private float distanceTraveled;

    public Water(float x, float y, float direction) 
    {
        speed = 0.7f;

        graphic = new Graphic();
        graphic.setType("WATER");   

        graphic.setX(x);
        graphic.setY(y);

        direction = graphic.getDirection(); //direction from Hero as water is fired
    }
    public Water update(int time) 
    {
        graphic.draw();
        return Water.this;
        distanceTraveled; // this is where the error occured...
    }
}

When I tried to call distanceTraveled, I am getting the error as:

Syntax error, insert "VariableDeclarators" to complete LocalVariableDeclaration

3 Answers

You have to either print distanceTraveled or do any operation like increment. Don't write only variable.

CORRECT: <%! ...code... %> (JSP DECLARATION)

WRONG : <% ...code... %> (JSP SCRIPLET)

WRONG : <%= ...code... %> (JSP EXPRESSION)

Example:

<!-- ------------------------------------- -->
<html><body><h1> 
    <%!
    public static String fn(){

        return( "[CORRECT:USE ! MARK]");
    }; 
    %>

    <%= fn() %>
</h1></body></html>
<!-- ------------------------------------- -->

Using "<%" or "<%=" instead of "<%!" will get error:

org.apache.jasper.JasperException: Unable to compile class for JSP:

An error occurred at line: [3] in the jsp file: [/index.jsp] Syntax error, insert "VariableDeclarators" to complete LocalVariableDeclaration

Related