Hack Forums
[Java] how to handle nullPointerException on JSP - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Java] how to handle nullPointerException on JSP (/showthread.php?tid=4889410)



how to handle nullPointerException on JSP - SunnyAkuma - Jul 1, 2015

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>



* - JavaProphet - Jul 1, 2015

A simple solution is to do a check for a null, then return an error to the user.


* - JavaProfit - Jul 1, 2015

(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).


* - SunnyAkuma - Jul 3, 2015

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);


* - JavaProfit - Jul 3, 2015

(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.


* - SunnyAkuma - Jul 5, 2015

(Jul 3, 2015 01:46 PM)Systemic 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 ?


* - JavaProfit - Jul 5, 2015

(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.