JSP空指针异常无法修复

时间:2015-03-09 05:42:29

标签: java jsp servlets

我有一个jsp页面,用于从用户获取数据并使用servlet将它们添加到数据库中。当我尝试运行页面时出现null异常。但它没有任何错误。有人可以帮我弄清楚错误是什么吗?

错误 -

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

我的jsp -

<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.util.ArrayList"%>
<%@page import="com.cert.classes.Incident"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Sri Lanka CERT</title>

    <link rel="stylesheet" type="text/css" href="bootstrap/css/bootstrap.min.css" />
    <link rel="stylesheet" type="text/css" href="font-awesome/css/font-awesome.min.css" />
    <link rel="stylesheet" type="text/css" href="css/local.css" />

    <script type="text/javascript" src="js/jquery-1.10.2.min.js"></script>
    <script type="text/javascript" src="bootstrap/js/bootstrap.min.js"></script>        
</head>
<body>

    <div id="wrapper">
        <nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse">
                    <span class="sr-only"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <a class="navbar-brand" href="index.html">Sri Lanka CERT</a>
            </div>
            <div class="collapse navbar-collapse navbar-ex1-collapse">
                <ul class="nav navbar-nav side-nav">
                    <li><a href="main_page.jsp"><i class="fa fa-bullseye"></i> Dashboard</a></li>
                    <li><a href="search_edit.jsp"><i class="fa fa-tasks"></i> Search and Update</a></li>


                </ul>
                <ul class="nav navbar-nav navbar-right navbar-user">
                    <li class="dropdown messages-dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown"><i class="fa fa-envelope"></i> Messages<b class="caret"></b></a>
                        <ul class="dropdown-menu">
                            <li class="dropdown-header">2 New Messages</li>
                            <li class="message-preview">
                                <a href="#">
                                    <span class="avatar"><i class="fa fa-bell"></i></span>
                                    <span class="message">Security alert</span>
                                </a>
                            </li>
                            <li class="divider"></li>
                            <li class="message-preview">
                                <a href="#">
                                    <span class="avatar"><i class="fa fa-bell"></i></span>
                                    <span class="message">Security alert</span>
                                </a>
                            </li>
                            <li class="divider"></li>
                            <li><a href="#">Go to Inbox <span class="badge"></span></a></li>
                        </ul>
                    </li>
                     <li class="dropdown user-dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown"><i class="fa fa-user"></i><b class="caret"></b></a>
                        <ul class="dropdown-menu">
                            <li><a href="#"><i class="fa fa-gear"></i> Change Password</a></li>
                            <li class="divider"></li>
                            <li><a href="#"><i class="fa fa-power-off"></i> Log Out</a></li>
                        </ul>
                        <li class="divider-vertical"></li>
                    <li>
                        <form class="navbar-search">
                            <input type="text" value=<%=request.getSession().getAttribute("user") %>><br>
                        </form>
                    </li>
                    </li>
                </ul>
            </div>
        </nav>

       <div id="page-wrapper">

            <div class="row">
                <div class="col-lg-6">

                    <form role="form" action="/addticket" method="post">

                        <div class="form-group">
                            <label>First Name</label>
                            <input class="form-control" name="firstname">
                         </div>

                       <div class="form-group">
                            <label>Last Name</label>
                            <input class="form-control" name="lastname">
                         </div>

                         <div class="form-group">
                            <label>Phone</label>
                            <input class="form-control" name="phone">
                         </div>


                       <div class="form-group">
                            <label>Mode</label><br>
                            <label class="radio-inline">
                                <input type="radio" name="mode"  value="call">
                                Call
                            </label>
                            <label class="radio-inline">
                                <input type="radio" name="mode"  value="email">
                                Email
                            </label>
                            <label class="radio-inline">
                                <input type="radio" name="mode"  value="visit">
                                Visit
                            </label>
                            <label class="radio-inline">
                                <input type="radio" name="mode"  value="other">
                                Other
                            </label>
                        </div>

            <div class="form-group">
                            <label>Status</label><br>
                            <label class="radio-inline">
                                <input type="radio" name="status" value="opened">
                                Opened
                            </label>
                            <label class="radio-inline">
                                <input type="radio" name="status" value="sent">
                                Document Sent
                            </label>
                            <label class="radio-inline">
                                <input type="radio" name="status" value="submitted">
                                Document Submitted
                            </label>
                            <label class="radio-inline">
                                <input type="radio" name="status" value="closed" disabled>
                                Closed
                            </label>
                        </div>

                      <div cla                       <label>Comment</label>
                            <textarea name="comment" class="form-control" rows="3"></textarea>
                        </div>


              <input type="hidden" class="form-control" value=<%=request.getSession().getAttribute("user")%>  name="handler">

    <%
         ArrayList<Incident> list1 = (ArrayList<Incident>) getServletContext().getAttribute("main_list");
         ArrayList<Incident> list2 = (ArrayList<Incident>) getServletContext().getAttribute("sub_list");
         ArrayList<Incident> list3 = (ArrayList<Incident>) getServletContext().getAttribute("incident_list");

     %>             

                </div>
                <div class="col-lg-6">


                            <div class="form-group">
                                <label>Main Category</label>
                                <select  name="main_category" class="form-control">
                                   <%
                                    for(Incident item : list1){ %>
                                    <option label="<%=item.getType()%>" value="<%=item.getType()%>" >
                                            <%
                                            }
                                    %>
                                </select>
                            </div>

                            <div class="form-group">
                                <label>Sub Category</label>
                                <select name="sub_category" class="form-control">
                                    <%
                                    for(Incident item : list2){ %>
                                    <option label="<%=item.getType()%>" value="<%=item.getType()%>" >
                                            <%
                                            }
                                    %>
                                </select>
                            </div>

                <div class="form-group">
                                <label>Incident Type</label>
                                <select  name="incident_type" class="form-control">
                                    <%
                                    for(Incident item : list3){ %>

                                    <option label="<%=item.getType()%>" value="<%=item.getType()%>" >
                                            <%
                                            }
                                    %>
                                </select>
                            </div>

                <div class="form-group has-error">
                            <label>Target</label>
                            <input type="text" class="form-control" name="target">
                        </div>

                        <div class="form-group has-warning">
                            <label>Forwarded</label>
                            <input type="text" class="form-control" name="forwarded">
                        </div>


                        <button type="submit" class="btn btn-default">Add Ticket</button>
                        <button type="reset" class="btn btn-default">Clear</button>

                    </form>
                </div>
            </div>
        </div>
    </div>    

</body>
</html>

1 个答案:

答案 0 :(得分:0)

此代码的问题是

ArrayList<Incident> list1 = (ArrayList<Incident>) getServletContext().getAttribute("main_list");

此行if(getServletContext().getAttribute("main_list")!=null)的第一个checknull条件,然后键入强制转换为ArrayList。如果我们为此行getServletContext().getAttribute("main_list")获取空值并且您尝试使用ArrayList进行类型转换,那么您将仅获得Null Pointer excetion。请使用JSTL标记而不是直接在jsp中放置java代码...