exception java.lang.NullPointerException的问题

问题描述

exception java.lang.NullPointerException的问题


public class StaffDao {

//验证登录
    public String CheckLogin(String username, String password){
        String id = null;
        String sql="select * from staff where staff_name='"+username+"' and staff_password='"+password+"'";
        Statement stat = null;
        ResultSet rs = null;
        Connection conn = new DBHelper().getConn();
        try{

stat = conn.createStatement();//请问这行问题出在哪?

            rs = stat.executeQuery(sql);
            while (rs.next()) {
                id = rs.getString("staff_no");
            }
        }
        catch(SQLException ex){}
        return id;
    }
    //验证密码
    public boolean CheckPassword(String id, String password){
        boolean ps = false;
        String sql="select *from staff where staff_no='"+id+"' and staff_password='"+password+"'";
        Statement stat = null;
        ResultSet rs = null;
        Connection conn = new DBHelper().getConn();
        try{
            stat = conn.createStatement();
            rs = stat.executeQuery(sql);
            while (rs.next()) {
                ps=true;
            }
        }
        catch(SQLException ex){}
        return ps;
    }

解决方案

在Java中对值为null的指针调用任何方法,就会引发空指针异常(java.lang.NullPointerException)。空指针异常绝对是Java中最难查找和调试的一种异常,你永远无法得到任何有用的调试信息。通过个人日常总结和网上搜集整理,归纳如下,供大家参考。
空指针异常产生的主要原因如下:
所以在Java中一般报空指针异常的原因有以下几种:
1字符串变量未初始化;
2接口类型的......
答案就在这里:空指针问题(java.lang.NullPointerException)
----------------------

解决方案二:

Connection conn = new DBHelper().getConn();
很可能是你连接字符串不对,数据库配置问题等,getConn();返回了null

时间: 2025-01-07 23:06:14

exception java.lang.NullPointerException的问题的相关文章

java web java.lang.NullPointerException

问题描述 java web java.lang.NullPointerException 严重: Servlet.service() for servlet [selectServlet] in context with path [/Exercise] threw exceptionjava.lang.NullPointerException at DB.DataBaseConnection.selectOperation1(DataBaseConnection.java:45) at Ser

servlet-关于java.lang.NullPointerException的问题

问题描述 关于java.lang.NullPointerException的问题 提示的错误是:严重: Servlet.service() for servlet Shopping threw exceptionjava.lang.NullPointerException at cn.itcast.Shopping.doGet(Shopping.java:45) at javax.servlet.http.HttpServlet.service(HttpServlet.java:617) at

hashmap-Java中遇到的一个异常Exception in thread "main" java.lang.NullPointerException

问题描述 Java中遇到的一个异常Exception in thread "main" java.lang.NullPointerException import java.util.*; public class NewHashTest{ public static void main(String[] args) { HashMap hash = new HashMap(); Emp emp1 = new Emp(""laoda""111&q

web 环境项目(intellj部署的tomcat) 重启时报 Exception in thread "HouseKeeper" java.lang.NullPointerException (转)

Exception in thread "HouseKeeper" java.lang.NullPointerException at org.logicalcobwebs.proxool.HouseKeeperController.getHouseKeeperToRun(HouseKeeperController.java:52) at org.logicalcobwebs.proxool.HouseKeeperThread.run(HouseKeeperThread.java:33

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

问题描述 我的代码如下:JTabbedDemo.javaimport javax.swing.*;import java.awt.*;public class JTabbedDemo{public static void main(String[] args) throws Exception{//显示外观风格UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());JFrame jframe=new JFrame("

JSP页面中出现的一个异常:org.apache.jasper.JasperException: java.lang.NullPointerException

先把出现的异常情况贴出来吧: type Exception report messagejava.lang.NullPointerException descriptionThe server encountered an internal error that prevented it from fulfilling this request. exception org.apache.jasper.JasperException: java.lang.NullPointerException

ndroid-新人求助 java.lang.NullPointerException

问题描述 新人求助 java.lang.NullPointerException 04-26 21:42:40.039: E/AndroidRuntime(1125): FATAL EXCEPTION: main 04-26 21:42:40.039: E/AndroidRuntime(1125): Process: com.example.remotecontrol, PID: 1125 04-26 21:42:40.039: E/AndroidRuntime(1125): java.lang

sharepreferences-安卓:java.lang.NullPointerException:

问题描述 安卓:java.lang.NullPointerException: logcat显示错误: FATAL EXCEPTION: main Process: org.droidplanner, PID: 21924 java.lang.RuntimeException: Unable to resume activity {org.droidplanner/org.droidplanner.android.activities.FlightActivity}: java.lang.Nul

ssh-学习用MyEclipse 2013开发SSH项目遇到java.lang.NullPointerException问题,请帮助!

问题描述 学习用MyEclipse 2013开发SSH项目遇到java.lang.NullPointerException问题,请帮助! SaveOfficeAction.java出错:java.lang.NullPointerException package we.app.action; import we.app.data.*; import com.opensymphony.xwork2.ActionSupport; public class SaveOfficeAction exten