Suhail Gupta
Suhail Gupta

Reputation: 23276

error : else without if . But i have placed it immediately after it

When i execute the following code :

<script type="text/javascript">
function UploadMessage() {
<%! String message = null; 
    boolean AttemptToUploadFile = false;
%>
    <% 
        message = (String)request.getAttribute("SuccessMessage");
        if(message != null) {
    %>
            alert("File Successfully Uploaded !");
            <% } %>
            <% else if((Boolean)request.getAttribute("UploadAttempt")) { %>
                  alert("Unable to upload file");
                <%}%>

}

I get the following error :

media/work documents/UnderTest/NetbeansCurrent/ProjectSnippets/build/generated/src/org/apache/jsp/portfolio_005fone_jsp.java:252: error: 'else' without 'if'
else if((Boolean)request.getAttribute("UploadAttempt")) { 
1 error

The error says if without else but i have placed else immediately after if. Then why the error?

Upvotes: 1

Views: 499

Answers (3)

user1252434
user1252434

Reputation: 2121

You haven't placed it immediatety after if. There is %> + newline + <% between it. JSP must output the newline in the generated code, so there is something like out.print("\n\t\t"); (including indention) between } and else if in the resulting java code.

Upvotes: 0

nfechner
nfechner

Reputation: 17525

Your error is in these two lines:

<% } %>
<% else if((Boolean)request.getAttribute("UploadAttempt")) { %>

In the resulting java code, they will (basically) be translated to:

}
out.append("\n");
else if(...

So you need to place the closing parentheses in the same scriptlet like this:

<% } else if((Boolean)request.getAttribute("UploadAttempt")) { %>

Upvotes: 2

Guffa
Guffa

Reputation: 700512

You have to put them in the same script block. Just remove the ending %> and the beginning <%:

<% }
     else if (...

Upvotes: 6

Related Questions