问题描述
代码:<%@ page language="java" import="java.util.*" pageEncoding="gbk"%><%@page contentType="image/jpeg"%> <%@ page import="java.text.*" %><%@ page import="java.sql.*" %><%@ page import="wh.rjst.conn.*" %><%@ page import ="wh.rjst.daoimp.*" %><%@ page import="java.io.*" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> </head> <body> <% Connection conn=null; PreparedStatement ps=null; ResultSet rs=null; try { int id=Integer.parseInt(request.getParameter("id")); System.out.println(id); String sql="select active_img from active where id="+id+""; ps=conn.prepareStatement(sql); rs=ps.executeQuery(); rs.next(); ServletOutputStream sout=response.getOutputStream(); InputStream in=rs.getBinaryStream("active_img"); byte b[]=new byte[4096]; for(int i=in.read(b);i!=-1;) { sout.write(b); in.read(b); } sout.flush(); sout.close(); } catch (ConnException ex) { throw new DaoException("无法获取连接", ex); } catch (SQLException ex) { throw new DaoException("数据库访问异常", ex); } finally { try { if (conn != null) conn.close(); if (ps != null) ps.close(); } catch (SQLException ex) { } } %> </body></html>出现的问题:2008-11-3 10:32:22 org.apache.catalina.core.StandardWrapperValve invoke严重: Servlet.service() for servlet jsp threw exceptionjava.lang.NullPointerExceptionat org.apache.jsp.showpic_jsp._jspService(showpic_jsp.java:76)at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:332)at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)at wh.rjst.filters.CodeFilter.doFilter(CodeFilter.java:26)at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)at java.lang.Thread.run(Thread.java:619)
解决方案
String sql="select active_img from active where id="+id+""; ps=conn.prepareStatement(sql); rs=ps.executeQuery(); rs.next(); ServletOutputStream sout=response.getOutputStream(); InputStream in=rs.getBinaryStream("active_img"); 你应该给rs.next()加上一个if()判断.这样当你查出来的rs没有值时,也不会出现空指针
解决方案二:
第一此运行的时候没有传入id参数的值