HTTP状态500:org.apache.jasper.JasperException:java.lang.NullPointerException

时间:2016-07-15 07:16:39

标签: java nullpointerexception

我正在尝试创建一个java网络应用程序,它采用了演员的名字和姓氏,并列出了他们所扮演的所有电影片段。

我正在使用Glassfish Server 4.1.1 和mysql连接器5.1.39 以及netbeans ide 8.1

这是我的开始页面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>

这是index.jsp将请求发送到

的display.jsp页面
<%-- 
    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>

我正在使用mysql提供给我们的默认数据库,安装时是sakila。

两个文件都正在编译 但是当我点击提交按钮时,我得到了一个 HTTP状态500 - 内部服务器错误

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 

来自服务器的日志

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)

如何解决此错误。 谢谢

这是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);
    }
  }
}

1 个答案:

答案 0 :(得分:0)

嘿伙计们错误是在运行时出现并且错误发生在while循环因此我意味着演员设置为null无论我输入什么

这让我意识到我实际上是在输入一个不在数据库中的actor名称,也没有检查actor是否为null。

所以我犯了一个非常愚蠢的错误,键入一个不在数据库中的actor导致actor为null导致java.lang.nullpointer异常,否则代码工作正常

感谢您的帮助