回 帖 发 新 帖 刷新版面

主题:关于JSP的一个问题:NullPointerException错误

是新手,刚写了一个JSP程序,在IE里执行的时候,出现了下列错误:
Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:207)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:240)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:187)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:809)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:200)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:146)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:209)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:596)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:433)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:948)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:144)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:596)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:433)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:948)
at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2358)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:133)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:596)
at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:118)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:594)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:116)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:594)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:433)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:948)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:127)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:596)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:433)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:948)
at org.apache.coyote.tomcat4.CoyoteAdapter.service(CoyoteAdapter.java:152)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:799)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:705)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:577)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683)
at java.lang.Thread.run(Thread.java:534)


root cause

java.lang.NullPointerException
at java.lang.String.<init>(String.java:141)
at org.apache.jsp.do_enter_jsp._jspService(do_enter_jsp.java:90)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:92)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:809)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:162)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:240)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:187)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:809)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:200)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:146)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:209)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:596)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:433)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:948)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:144)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:596)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:433)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:948)
at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2358)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:133)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:596)
at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:118)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:594)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:116)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:594)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:433)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:948)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:127)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:596)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:433)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:948)
at org.apache.coyote.tomcat4.CoyoteAdapter.service(CoyoteAdapter.java:152)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:799)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:705)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:577)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683)
at java.lang.Thread.run(Thread.java:534)

愿程序:
<%@ page contentType="text/html;charset=gb2312" %>
<html>
<head>
<title>录入成功</title>
<head>
<body>
<%@ page language = "java" import="java.lang.* ,java.sql.* " %>
<form>
<%!
Connection con = null;
Statement stmt = null;
ResultSet rs = null;


public void jspInit() //执行数据库连结动作
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //载入驱动程式类别
con = DriverManager.getConnection("jdbcdbc:LocalServer"," "," "); //建立数据库连线
//建立Statement变量, 并设定记录指标类型为可前後移动
stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
}
catch(Exception ex)
{
System.out.println(ex.toString());
}
}
public void jspDestroy() //关闭变量
{
try
{
stmt.close(); //关闭Statement变量
con.close(); //关闭Connection变量
rs.close(); //关闭ResultSet变量
}
catch(Exception ex)
{
System.out.println(ex.toString());
}
}
%>
<table width = "760" align = "center" >
<%
String course_id = new String(request.getParameter("course"));
String unit_num = new String(request.getParameter("chapter"));
String degreed = new String(request.getParameter("degreed"));
String content = new String(request.getParameter("point"));
if(course_id.equals(new String("0"))){
out.println("<tr><td>请选择一个课程!</td></tr>");
}else if(unit_num.equals(new String("0"))){
out.println("<tr><td>请选择一个单元!</td></tr>");
}else if(degreed.equals(new String("0"))){
out.println("<tr><td>请选择一个难度系数!</td></tr>");
} else {
String sql = "insert into coursepoint(course_id,unit_num,degreed,content) values("+course_id+","+unit_num
+","+degreed+","+content+")";
jspInit();
int n = stmt.executeUpdate(sql);
jspDestroy();
out.println("<tr><td>You have enter one information point.<br><br>");
out.println("课程名:"+course_id);
out.println("<br><br>章节:"+unit_num);
out.println("<br><br>难度系数:"+degreed);
out.println("<br><br>内容:"+content+"</td></tr>");
}
%>
</table>
</form>
</body>
</html>

回复列表 (共17个回复)

沙发

把你创建的所有对象都 out.println()一下,看是不是有哪个对象是null,我觉得可能是你的获取表单那部分有空对象或者你数据库联接那部分有问题

板凳

我最后才发现原来是我对参数名写错了!
结果参数没传到这个网页上!
太马虎了!

3 楼

这程序看上去怎么那么恶心啊

4 楼

顶一下3楼的。说话客气点好不好?大家都是作程序的,给留点面子。记得我原来发过一个帖子,你说我写的代码很乱,不适合编程。我那时刚刚开始学习编程,你的断言让我失去信心,是在朋友的不断鼓励下我才继续下去。我还在继续学习,继续编程。想来你也不是什么老手、高手,你不是也不会在JSP中获取当前日期吗?别人给你答复你不是也看不懂吗?我这个新手都看懂了,你都有了百十来分了,这么适合编程的人还看不懂?你也做个不恶心的程序给我们看看!话不要说得太绝,给别人留点面子,也给自己留点余地,我们都是从新手走过来的!!!

5 楼

谢谢4楼!
不过,我想问三楼的,我的程序哪里恶心了?希望你在给出批评的时候也给个令人信服的理由,那样我也一样会感激你的!

6 楼

你那个程序 段行、缩进、都不合格,注释还少得可怜, 你想想, 要是有个千行左右的程序, 注释比例就像你这个这么多, 还是别人写的, 让你来维护, 你不想死啊?

程序本身没什么,只是要易于维护,两个月后你自己都不想看自己的程序了,你觉得呢?

7 楼

hlnccc
你的评价很中肯!
的确,我是有不喜欢写注释的坏习惯,太懒了!还有缩进方面.
以后会注意的!

8 楼

说来惭愧,我也是到了公司才注意这些的,你这段代码要是让我们经理看见了,肯定看你黑眼,万一没几天你辞职了,这代码谁维护谁倒霉。你可以写个java程序,写个类,写两个方法,然后用一下 javadoc命令,用法和 javac 差不多, 然后在你的程序目录会有很多 htm文件,你看看那些都是什么, 像java-api的东西

尽量多的写注释会变得有趣,

9 楼

顶8楼的,做程序必须把格式弄好,谁也不愿意收拾烂摊子,新去公司你看见这样的代码完全可以拒绝经理不接手这样的活,维护起来费劲还不讨好。刺激你是恨铁不成钢,总比你发出帖子来没人搭理你好吧,另外日期那个是用ASP写的,所问非所答,比如说经理叫你干这个,你这个没完成,你干那个去了,那个是完成的挺漂亮,不过有用吗?经理根本不领你情,没准还挖苦你两句,会就是会,不会就是不会,没什么丢人的,
回3楼话,日期问题  一,那个人是用ASP写的,怪那个人就是所问非所答,怪我的话就是不会ASP

10 楼

3楼给我说地相当惭愧,不过别人说不见得是坏处,你自己要是没缺点谁说你干啥啊,你也不用那么激奋,当初有人说我是朽木不可雕也,我也不是挺过来了



<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.lang.*, java.sql.* " %>
<html>

    <head>
        <title>录入成功</title>
    <head>

    <body>
        <form>
            <%
                Connection con = null;
                Statement stmt = null;
                ResultSet rs = null;
                //=======================执行数据库连结动作========================

                public void jspInit()
                {
                    try
                    {
                        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //载入驱动程式类别
                        con = DriverManager.getConnection("jdbcdbc:LocalServer"," "," "); //建立数据库连线
                        //建立Statement变量, 并设定记录指标类型为可前後移动
                        stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
                        ResultSet.CONCUR_READ_ONLY);
                    }catch(Exception ex)
                    {
                        System.out.println(ex.toString());
                    }
                }

                //==========================关闭变量===========================

                public void jspDestroy()
                {
                    try
                    {
                        stmt.close(); //关闭Statement变量
                        con.close(); //关闭Connection变量
                        rs.close(); //关闭ResultSet变量
                    }
                    catch(Exception ex)
                    {
                        System.out.println(ex.toString());
                    }
                }

            %>

            <table width = "760" align = "center" >

            <%
                String course_id = new String(request.getParameter("course"));
                String unit_num = new String(request.getParameter("chapter"));
                String degreed = new String(request.getParameter("degreed"));
                String content = new String(request.getParameter("point"));

                if(course_id.equals(new String("0")))
                {
                out.println("<tr><td>请选择一个课程!</td></tr>");
                }
                else
                {
                    if(unit_num.equals(new String("0")))
                    {
                        out.println("<tr><td>请选择一个单元!</td></tr>");
                    }
                    else
                    {
                        if(degreed.equals(new String("0"))){
                            out.println("<tr><td>请选择一个难度系数!</td></tr>");
                        }
                        else
                        {
                            String sql = "insert into coursepoint(course_id,unit_num,degreed,content) values("+course_id+","+unit_num
                            +","+degreed+","+content+")";
                            jspInit();
                            int n = stmt.executeUpdate(sql);
                            jspDestroy();
                            out.println("<tr><td>You have enter one information point.<br><br>");
                            out.println("课程名:"+course_id);
                            out.println("<br><br>章节:"+unit_num);
                            out.println("<br><br>难度系数:"+degreed);
                            out.println("<br><br>内容:"+content+"</td></tr>");
                        }
                    }
                }

            %>
            </table>
        </form>
    </body>
</html>

我来回复

您尚未登录,请登录后再回复。点此登录或注册