严重: Servlet.service() for servlet () threw exception java.lang.NullPoint

cyqcyq2008 2013-03-28 10:58:07
servlet运行错误:严重: Servlet.service() for servlet show_pathway threw exception java.lang.NullPointerException
把代码贴出来:我想根据前台传过来的参数“org_name”,通过连接数据库pathway,把“map_location” 属性里存储的图片路径取出来并把图片显示在浏览页。“map_location”存储的内容是:"/WEB-INF/KEGG PATHWAY Glycolysis - Gluconeogenesis - Reference pathway (KO)_files/ko00010_1.017949.png", 这列的数据类型是varchar。错误在哪里?

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.PageContext;

import java.io.*;
import java.sql.*;

public class show_pathway extends HttpServlet {

public void service(HttpServletRequest request,
HttpServletResponse response) throws ServletException,IOException
{
//response.setContentType("text/html;charset=UTF-8");
//request.setCharacterEncoding("UTF-8");

String org_name = request.getParameter("org_name");
String mapno = request.getParameter("mapno");
String mapscale = request.getParameter("mapscale");
String show_description = request.getParameter("show_description");
String location = null;

try{
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://127.0.0.1:3306/pathway";
String user = "root";
String password = "";
Connection conn = DriverManager.getConnection(url, user, password);

// statement用来执行SQL语句
Statement statement = conn.createStatement();
// 要执行的SQL语句
String sql = "select map_location from show_map where org_name = "+org_name+" ";
// 结果集
ResultSet rs = statement.executeQuery(sql);
while(rs.next())
{
location = rs.getString("map_location");
}
conn.close();
}catch(ClassNotFoundException e){
e.printStackTrace();
}catch(SQLException e){
e.printStackTrace();
}

//response.setContentType("image/png");

InputStream in = this.getServletContext().getResourceAsStream(location);
OutputStream outstr = response.getOutputStream();
byte[] buffer = new byte[1024];
int length = -1;
while( (length = in.read(buffer))!= -1)
{
outstr.write(buffer, 0, length);
}

in.close();
outstr.close();

}

}

...全文
502 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
int length = 0;
cyqcyq2008 2013-03-28
  • 打赏
  • 举报
回复
引用 楼主 cyqcyq2008 的回复:
servlet运行错误:严重: Servlet.service() for servlet show_pathway threw exception java.lang.NullPointerException 把代码贴出来:我想根据前台传过来的参数“org_name”,通过连接数据库pathway,把“map_location” 属性里存储的图片路径取出来并把图片显示在浏览页。“map_loc……
补一句:如果我的查询元素变成另一列属性“mapno”,即第32行变成:String sql = "select map_location from show_map where mapno = "+mapno+" "; 就会正常显示图像。为何?

51,397

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧