Tabraiz Ali
Tabraiz Ali

Reputation: 677

Error: incompatible operand types and object

In the below Java code which is placed in a JSP file,

if (false == session.getAttribute("loggedin")) {  
    response.sendRedirect("login.jsp");
}
else if (null == session.getAttribute("loggedin")) {
    response.sendRedirect("login.jsp");
}

I am getting the following compilation error:

incompatible operand types and object

How is this caused and how can I solve it?

Upvotes: 1

Views: 5793

Answers (1)

JB Nizet
JB Nizet

Reputation: 691635

session.getAttribute() returns an Object. An Object can't be compared to a boolean. So the expression false == session.getAttribute("loggedin") is invalid. If you want to check if Boolean.FALSE is stored in the session attribute, the code should be

 if (Boolean.FALSE.equals(session.getAttribute("loggedin")))

Note that you'd better put all the Java code in regular Java classes and limit yourself to the JSP EL in the JSPs. Scriptlets should not be used anymore.

Upvotes: 4

Related Questions