main方法可以正常调用,jsp调用出现java.lang.NullPointerException

qq_38838736 2017-08-06 11:29:31
package dao;

import java.sql.*;

public class Dao {
private String dri="com.mysql.jdbc.Driver";
private String url="jdbc:mysql://localhost:3306/Tourism";
private String Id="root";
private String pwd="root";
private Connection conn=null;

/**
* 连接数据库
*/
public void getconntion(){
if(conn==null){
try {
Class.forName(dri);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
conn=DriverManager.getConnection(url,Id,pwd);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* 查询
*/
public ResultSet executeQuery(String sql){
getconntion();
ResultSet rs=null;
try {
Statement state=conn.createStatement();
rs=state.executeQuery(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rs;
}
}
数据库的账号和密码是admin和123
这个是登录的方法


然后在main方法里测试一切正常

但是在jsp页面调用的时候
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
<%@ page import="impl.Tri" %>
<%@ page import="impl.Tribu" %>
<%@ page import="dao.Dao" %>
<%@ page import="java.sql.*"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<% request.setCharacterEncoding("utf-8"); %>
<%
Tri tri = new Tribu();
String username = request.getParameter("username");
String password = request.getParameter("password");
if(tri.login(username, password)){
session.setAttribute("username", username);
request.getRequestDispatcher("index.jsp").forward(request, response);
}else{
out.print("账号或密码错误,3秒后返回登录界面!");
response.setHeader("refresh","3;url=login.jsp");
}
%>
就会出现这个情况,完全懵逼了

求大神讲解,网上搜半天找不到答案~~~
...全文
316 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
李德胜1995 2017-08-07
  • 打赏
  • 举报
回复
Dao类的38行空指针异常。。。。
粉墨听禅 2017-08-07
  • 打赏
  • 举报
回复

String username = request.getParameter("username");
String password = request.getParameter("password");
这个地方从哪有 requestsetParameter("username","xxx");吗,应该是没获取到,有的话你再检查一下两次名字是否完全相同
lifewell1 2017-08-07
  • 打赏
  • 举报
回复
把dao方法贴出来,感觉是bean没有实例化

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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