Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Java 

how to handle nullPointerException on JSP

this is my JSP , it'll get the username and email using session.getAttribute("key") method.

The problem is, If someone tries to open this page directly , without logging in , Then the values of the variable username and email should be null , and the user should be redirected to another page.

But instead I'm getting a nullPointerExcepetion just as I try to run this page without beginning the session , aka , going directly to this page without logging in using the url.

Here's the code.

Code
<%@page import="supportServlets.MyBeans"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body bgcolor='#c1c1c1'>
        <%
            HttpSession studentSession=request.getSession(false);
            String username=(String)studentSession.getAttribute("username");
            String email=(String)studentSession.getAttribute("email");
            if(username.equals(null)||email.equals(null))
            {
                MyBeans.setFlag("no");
                response.sendRedirect("studentLogin.jsp");
            }
        %>
        <jsp:include page="headerLogo.jsp"/>
        <form action="mainQuizPage">
            <center>
                <div style="width: 700px; background-color: whitesmoke">
                    <center>
                        <h2>Welcome <%=username%></h2>
                        <br>
                        
                    </center>
                </div>
            </center>
        </form>
        <jsp:include page="footerLogo.jsp"/>
    </body>
</html>
A simple solution is to do a check for a null, then return an error to the user.
(Jul 1, 2015 07:13 PM)JavaProphet Wrote:  A simple solution is to do a check for a null, then return an error to the user.

To extend off of this, you never want to catch a NullPointerException. So unit test your code before your server goes live to make sure anything that might cause an NPE is fixed (usually the if(obj != null) check is applied here).
well, it's simple , I didn't think of it before. You can't check if a String is null using .equals() method.

Because,

String a;
Here 'a' is an object of class String that contains method named equals().

now if a = null

then you can't call a.equals(null);

because that will be like ---> null.equals(null);
(Jul 3, 2015 12:04 PM)SunnyAkuma Wrote:  well, it's simple , I didn't think of it before. You can't check if a String is null using .equals() method.

Because,

String a;
Here 'a' is an object of class String that contains method named equals().

now if a = null

then you can't call a.equals(null);

because that will be like ---> null.equals(null);

No just use == for null checks. You can't invoke a non-static method off of a null object.
(Jul 3, 2015 01:46 PM)JavaProfit Wrote:  No just use == for null checks. You can't invoke a non-static method off of a null object.

Did you even read my comment ?
(Jul 5, 2015 12:37 PM)SunnyAkuma Wrote:  Did you even read my comment ?

Yes and you blabbered on about something that could be written in 1 sentence.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Java] by saltycrackerDec 18, 2020 12:51 PM 1 168 Dec 18, 2020 12:51 PM
Last Post: popek122
[Java] by Aus10Feb 11, 2018 11:14 PM 2 162 Feb 11, 2018 11:14 PM
Last Post: Aus10
[Java] by MohinderJan 10, 2017 01:30 PM 7 212 Jan 10, 2017 01:30 PM
Last Post: 3cli‌‌pse