0

i am trying to create a java web app which takes the first name and last name of an actor and lists out all the movie titles that they have acted in.

i am using Glassfish Server 4.1.1 and mysql connector 5.1.39 along with netbeans ide 8.1

this is my startting page index.jsp

<%-- 
    Document   : index
    Created on : Jul 15, 2016, 11:27:47 AM
    Author     : akhilb1
--%>

<%@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>
        <h1>Hello World!</h1>
        <form name="myForm" action="display.jsp" method="POST">
            <table border="0">
                <tbody>
                    <tr>
                        <td>First name :</td>
                        <td><input type="text" name="first" value="" size="50" /></td>
                    </tr>
                    <tr>
                        <td>Last Name :</td>
                        <td><input type="text" name="last" value="" size="50" /></td>
                    </tr>
                </tbody>
            </table>
            <input type="reset" value="Clear" name="clear" />
            <input type="submit" value="Submit" name="submit" />
        </form>
    </body>
</html>

and this is my display.jsp page to which the index.jsp sends the request to

<%-- 
    Document   : display
    Created on : Jul 15, 2016, 11:38:53 AM
    Author     : akhilb1
--%>
<%@page import="java.sql.*"%>
<% Class.forName("com.mysql.jdbc.Driver");%>
<%@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>
        <h1>Hello World!</h1>
        <%! 
             public class Actor{

            String URL="jdbc:mysql://localhost:3306/sakila";
            String USERNAME="root";
            String PASSWORD="admin";
            Connection connection =null;
            PreparedStatement selectActors=null;
            ResultSet resultSet = null;



                public Actor(){
                    try {
                    connection=DriverManager.getConnection(URL,USERNAME,PASSWORD);
                    selectActors =connection.prepareStatement(
                                                "SELECT a.first_name,a.last_name,c.title "
                                                +"FROM actor a,film_actor b, film c"
                                                +"WHERE a.first_name=?"
                                                +"AND a.last_name=?"
                                                +"AND a.actor_id=b.actor_id"
                                                +"AND b.film_id=c.film_id");
                    }catch (SQLException e){

                    }
                }
                public ResultSet getACtors(String first,String last){



                    try {
                    selectActors.setString(1,first);
                    selectActors.setString(2,last);


                    resultSet=selectActors.executeQuery();
                    }catch (SQLException e){

                    }

                    return resultSet;
                }


            }
                %>
                <%
                    String firstName= new String();
                    String lastName= new String();

                    if(request.getParameter("first")!=null)
                    {
                        firstName= request.getParameter("first");
                    }
                     if(request.getParameter("last")!=null)
                    {
                        lastName= request.getParameter("last");
                    }

                     Actor actor = new Actor();
                     ResultSet actors= actor.getACtors(firstName,lastName);

                    %>
                    <table border="1">

                        <tbody>
                            <tr>
                                <td>First Name :</td>
                                <td>Last Name :</td>
                                <td>Title</td>
                            </tr>
                            <% while (actors.next()) { %>
                            <tr>
                                <td><%= actors.getString("first_name") %></td>
                                <td><%= actors.getString("last_name") %></td>
                                <td><%= actors.getString("title") %></td>
                            </tr>
                            <% } %>
                        </tbody>
                    </table>

        </body>
    </html>

i am using the default database that is given to us by mysql that is sakila upon installing.

both the files are compiling properly but when i hit the submit button i get a HTTP Status 500 - Internal Server Error

type Exception report

messageInternal Server Error

descriptionThe server encountered an internal error that prevented it from fulfilling this request.

exception 
org.apache.jasper.JasperException: java.lang.NullPointerException


root cause 
java.lang.NullPointerException


note The full stack traces of the exception and its root causes are available in the GlassFish Server Open Source Edition 4.1.1 logs.


GlassFish Server Open Source Edition 4.1.1 

the logs from the server

Warning:   StandardWrapperValve[jsp]: Servlet.service() for servlet jsp threw exception
java.lang.NullPointerException
    at org.apache.jsp.display_jsp._jspService(display_jsp.java:133)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:111)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:411)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:473)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:377)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
    at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:416)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:283)
    at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
    at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:206)
    at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:180)
    at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
    at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:283)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:200)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:132)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:111)
    at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
    at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:536)
    at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:591)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:571)
    at java.lang.Thread.run(Thread.java:745)

how is it possible to resolve this error. thank you

here is the display_jsp.java

package org.apache.jsp;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import java.sql.*;

public final class display_jsp extends org.apache.jasper.runtime.HttpJspBase
    implements org.apache.jasper.runtime.JspSourceDependent {


             public class Actor{

            String URL="jdbc:mysql://localhost:3306/sakila";
            String USERNAME="root";
            String PASSWORD="admin";
            Connection connection =null;
            PreparedStatement selectActors=null;
            ResultSet resultSet = null;

            public Actor(){
                try {
                connection=DriverManager.getConnection(URL,USERNAME,PASSWORD);
                selectActors =connection.prepareStatement(
                                            "SELECT a.first_name,a.last_name,c.title "
                                            +"FROM actor a,film_actor b, film c"
                                            +"WHERE a.first_name=?"
                                            +"AND a.last_name=?"
                                            +"AND a.actor_id=b.actor_id"
                                            +"AND b.film_id=c.film_id");
                }catch (SQLException e){

                }
            }
            public ResultSet getACtors(String first,String last){



                try {
                selectActors.setString(1,first);
                selectActors.setString(2,last);


                resultSet=selectActors.executeQuery();
                }catch (SQLException e){

                }

                return resultSet;
            }


        }

  private static final JspFactory _jspxFactory = JspFactory.getDefaultFactory();

  private static java.util.List<String> _jspx_dependants;

  private org.glassfish.jsp.api.ResourceInjector _jspx_resourceInjector;

  public java.util.List<String> getDependants() {
    return _jspx_dependants;
  }

  public void _jspService(HttpServletRequest request, HttpServletResponse response)
        throws java.io.IOException, ServletException {

    PageContext pageContext = null;
    HttpSession session = null;
    ServletContext application = null;
    ServletConfig config = null;
    JspWriter out = null;
    Object page = this;
    JspWriter _jspx_out = null;
    PageContext _jspx_page_context = null;

    try {
      response.setContentType("text/html;charset=UTF-8");
      response.setHeader("X-Powered-By", "JSP/2.3");
      pageContext = _jspxFactory.getPageContext(this, request, response,
                null, true, 8192, true);
      _jspx_page_context = pageContext;
      application = pageContext.getServletContext();
      config = pageContext.getServletConfig();
      session = pageContext.getSession();
      out = pageContext.getOut();
      _jspx_out = out;
      _jspx_resourceInjector = (org.glassfish.jsp.api.ResourceInjector) application.getAttribute("com.sun.appserv.jsp.resource.injector");

      out.write('\n');
      out.write('\n');
 Class.forName("com.mysql.jdbc.Driver");
      out.write("\n");
      out.write("\n");
      out.write("<!DOCTYPE html>\n");
      out.write("<html>\n");
      out.write("    <head>\n");
      out.write("        <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n");
      out.write("        <title>JSP Page</title>\n");
      out.write("    </head>\n");
      out.write("    <body>\n");
      out.write("        <h1>Hello World!</h1>\n");
      out.write("        ");
      out.write("\n");
      out.write("            ");

                String firstName= new String();
                String lastName= new String();

                if(request.getParameter("first")!=null)
                {
                    firstName= request.getParameter("first");
                }
                 if(request.getParameter("last")!=null)
                {
                    lastName= request.getParameter("last");
                }

                 Actor actor = new Actor();
                 ResultSet actors= actor.getACtors(firstName,lastName);


      out.write("\n");
      out.write("                <table border=\"1\">\n");
      out.write("               \n");
      out.write("                    <tbody>\n");
      out.write("                        <tr>\n");
      out.write("                            <td>First Name :</td>\n");
      out.write("                            <td>Last Name :</td>\n");
      out.write("                            <td>Title</td>\n");
      out.write("                        </tr>\n");
      out.write("                        ");
 while (actors.next()) { 
      out.write("\n");
      out.write("                        <tr>\n");
      out.write("                            <td>");
      out.print( actors.getString("first_name") );
      out.write("</td>\n");
      out.write("                            <td>");
      out.print( actors.getString("last_name") );
      out.write("</td>\n");
      out.write("                            <td>");
      out.print( actors.getString("title") );
      out.write("</td>\n");
      out.write("                        </tr>\n");
      out.write("                        ");
 } 
      out.write("\n");
      out.write("                    </tbody>\n");
      out.write("                </table>\n");
      out.write("\n");
      out.write("    </body>\n");
      out.write("</html>\n");
    } catch (Throwable t) {
      if (!(t instanceof SkipPageException)){
        out = _jspx_out;
        if (out != null && out.getBufferSize() != 0)
          out.clearBuffer();
        if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
        else throw new ServletException(t);
      }
    } finally {
      _jspxFactory.releasePageContext(_jspx_page_context);
    }
  }
}
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555

1 Answers1

0

hey guys the error was coming in run time and the error was happening at the while loop hence i that means that the actors set was null for whatever i was entering

which led me to realize that i was actually typing in an actor name which as not in the database and also did not check whether the actor was null.

so yea i made a very silly mistake of typing an actor not in the database which led to actors being null which led to the java.lang.nullpointer exception else the code is working perfectly fine

thanks for the help