请教:: Servlet.service() for servlet jsp threw exception java.lang.NullPointerExce

hhhhhhh_____ 软件开发  2015-08-15 10:46:29
刚我启动浏览器的时候,提示错误:
严重: Servlet.service() for servlet jsp threw exception
java.lang.NullPointerException
at org.apache.jsp.index_jsp._jspService(index_jsp.java:105)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:388)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:620)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Unknown Source)
当我把这段代码删了,页面可以正常显示,但是我不知道这段代码哪里出错了,请各位指教
<div class="feature">
<%
String info;
String dt;
String user;

List<HashMap<String, Object>> messageList = JDBCOption.query(SQLConstant.getmessage);
for (int i = 0; i < messageList.size(); i++) {
HashMap hm = messageList.get(i);
user = hm.get("author").toString();
info = hm.get("info").toString();
dt = hm.get("published").toString();
%>
<div class="message">
<h3><%=user %></h3>
<p><%=info %></p>
<h3><%=dt %></h3>
</div>

<%
}
%>

</div>

源代码如下
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.util.List"%>
<%@ page import="java.util.HashMap"%>
<%@page import="com.dedo.BBS.Jdbc.SQLConstant"%>
<%@page import="com.dedo.BBS.Jdbc.JDBCOption"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<!-- DW6 -->
<head>
<!-- Copyright 2005 Macromedia, Inc. All rights reserved. -->
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>Untitled Document</title>
<link rel="stylesheet" href="/CSS/common.css" type="text/css" />
<style type="text/css">
<!--
.message {
padding: 10px;
float: left;
height: 200px;
width: 200px;
position: relative;
}
-->
</style>
</head>
<!-- The structure of this file is exactly the same as 2col_rightNav.html;
the only difference between the two is the stylesheet they use -->
<body>
<jsp:include page="head.jsp" flush="true" />
<!-- end masthead -->
<div id="content">
<div id="breadCrumb">
<a href="#">所有</a> /正向<a href="#"></a> / <a href="#">反向</a>/
</div>
<h2 id="pageName">公告区</h2>
<div class="feature">//这里有错******
<%
String info;
String dt;
String user;

List<HashMap<String, Object>> messageList = JDBCOption.query(SQLConstant.getmessage);
for (int i = 0; i < messageList.size(); i++) {
HashMap hm = messageList.get(i);
user = hm.get("author").toString();
info = hm.get("info").toString();
dt = hm.get("published").toString();
%>
<div class="message">
<h3><%=user %></h3>
<p><%=info %></p>
<h3><%=dt %></h3>
</div>

<%
}
%>

</div>
</div>
<!--end content -->
<div id="navBar">
<div id="search">
<form action="/SubMessage" method="post">
<label>输入发布内容</label>
<textarea name="message" cols="26" rows="20"></textarea>
<input name="goButton" type="submit" value="发布" />
</form>
</div>
<div id="headlines">
<h3>小白</h3>
<p> </p>
</div>
</div>
<!--end navbar -->
<jsp:include page="foot.jsp" flush="true" />
<br />
</body>
</html>

package com.dedo.BBS.Servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Timestamp;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dedo.BBS.Jdbc.JDBCOption;
import com.dedo.BBS.Jdbc.SQLConstant;
import com.dedo.BBS.Tool.Common;



/**
* Servlet implementation class SubMessage
*/
public class SubMessage extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public SubMessage() {
super();
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
}

protected void processRequest(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException
{
String message = request.getParameter("message");
java.util.Date d = new java.util.Date();
JDBCOption.update(SQLConstant.addmessage, 1,message,new Timestamp(d.getTime()));
response.sendRedirect("index.jsp");
}

}

package com.dedo.BBS.Jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.dedo.BBS.Tool.Common;

public class JDBCOption {
private static DBConfig dbconfig = null;

static {//连接数据库
dbconfig = new DBConfig();
try {
Class.forName(dbconfig.getDriver());
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
/*
* 查询操作
*/
public static List query(String sql, Object... paramters) {
List returnValue = new ArrayList();
ResultSet rs = null;
Connection conn = null;
PreparedStatement ps = null;

try {
conn = DriverManager.getConnection(dbconfig.getUrl(), dbconfig.getUsername(), dbconfig.getPassword());
ps = conn.prepareStatement(sql);
for (int i = 0; i < paramters.length; i++) {
ps.setObject(i + 1, paramters[i]);
}

rs = ps.executeQuery();

while (rs.next()) {
ResultSetMetaData md = rs.getMetaData();
Map map = new HashMap();

for (int i = 0; i < md.getColumnCount(); i++) {
map.put(md.getColumnLabel(i + 1), rs.getObject(i + 1));
}
returnValue.add(map);
}

} catch (SQLException e) {
e.printStackTrace();
}finally{
Common.free(conn, ps, rs);
}

return returnValue;
}



}
...全文
256 点赞 收藏 7
写回复
7 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
daliner6 2016-09-01
原来是我数据库和代码写错了一个字母 希望自己以后可以吸取教训 --这个是什么意思,写错一个字母也会导致这样的问题?
回复
hhhhhhh_____ 2015-08-16
引用 1 楼 libingxin 的回复:
你的hashMap的value类型 为object hm.get("author") 有肯能为null 你可以用(String)hm.get("author")
你好,我发现,当我把数据库里面的数据清空时,页面可以正常显示,但是当我提交数据的时候就会提示错误。我很困惑
<form action="/SubMessage" method="post">
				<label>输入发布内容</label>
				<textarea name="message" cols="26" rows="20"></textarea>
				<input name="goButton" type="submit" value="发布" />
			</form>
servlet代码如下:
package com.dedo.BBS.Servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Timestamp;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dedo.BBS.Jdbc.JDBCOption;
import com.dedo.BBS.Jdbc.SQLConstant;
import com.dedo.BBS.Tool.Common;



/**
 * Servlet implementation class SubMessage
 */
public class SubMessage extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SubMessage() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}
	
	protected void processRequest(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException
	{
		String message = request.getParameter("message");
		java.util.Date d = new java.util.Date();
		JDBCOption.update(SQLConstant.addmessage, 1,message,new Timestamp(d.getTime()));
		response.sendRedirect("index.jsp");
	}

}
回复
开心的冰屋 2015-08-16
你把toString()去掉就可以了
回复
hhhhhhh_____ 2015-08-16
原来是我数据库和代码写错了一个字母 希望自己以后可以吸取教训
回复
hhhhhhh_____ 2015-08-16
引用 4 楼 libingxin 的回复:
你把toString()去掉就可以了
谢谢指教!之后我把数据声明改为了object,并且删除了tosting就解决了问题
Object info;
				Object dt;
				Object user;

				List <HashMap<String, Object>>messageList = JDBCOption.query(SQLConstant.getmessage);
				for (int i = 0; i < messageList.size(); i++) {
					HashMap hm = messageList.get(i);
		
					user = hm.get("author");
					info = hm.get("info");
					dt = hm.get("published");
					System.out.println(user);
					System.out.println(info);
					System.out.println(dt);
回复
hhhhhhh_____ 2015-08-15
引用 1 楼 libingxin 的回复:
你的hashMap的value类型 为object hm.get("author") 有肯能为null 你可以用(String)hm.get("author")
我去试了一下

                                String info;
				String dt;
				String user;

				List <HashMap<String, Object>>messageList = JDBCOption.query(SQLConstant.getmessage);
				for (int i = 0; i < messageList.size(); i++) {
					HashMap hm = (HashMap) messageList.get(i);
					System.out.println(hm.get("info"));
					System.out.println(hm.get("published"));
					System.out.println(hm.get("auther"));
					
					user = (String)hm.get("author").toString();
					info = (String)hm.get("info").toString();
					dt = (String)hm.get("published").toString();
					System.out.println(user);
					System.out.println(info);
					System.out.println(dt);
前面那个println可以输出,说明数据库里面有数据的,但是为什么赋值之后却不能输出呢 nihao 2014-02-02 00:00:00.0 2 八月 15, 2015 11:29:50 下午 org.apache.catalina.core.StandardWrapperValve invoke 严重: Servlet.service() for servlet jsp threw exception java.lang.NullPointerException at org.apache.jsp.index_jsp._jspService(index_jsp.java:109) at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) at javax.servlet.http.HttpServlet.service(HttpServlet.java:723) at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:388) at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313) at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260) at javax.servlet.http.HttpServlet.service(HttpServlet.java:723) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:620) at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489) at java.lang.Thread.run(Unknown Source)
回复
开心的冰屋 2015-08-15
你的hashMap的value类型 为object hm.get("author") 有肯能为null 你可以用(String)hm.get("author")
回复
相关推荐
发帖
Web 开发
创建于2007-09-28

8.0w+

社区成员

Java Web 开发
申请成为版主
帖子事件
创建了帖子
2015-08-15 10:46
社区公告
暂无公告