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

来源:互联网 发布:手机屏幕恶作剧软件 编辑:程序博客网 时间:2024/05/28 05:14

连接数据库中出现的错误,虽然解决了,但不知道具体原因,望各位大神能解答下。

下面是解决过程;

====================错误代码========================================
try{
    Class.forName("com.mysql.jdbc.Driver");
    String url=***;                                                                       //***为省略内容
    String user=***;                                                                    //***为省略内容
    String password=***;                                                            //***为省略内容
    conn=DriverManager.getVonnection(url,user,password);
}catch(ClassNotFoundException e){
   e.printStackTrace();
}catch (SQLException e){
   e.printStackTrace();
}
return conn;
}
=================方法===========================
测试结果nullpointerexception
把return conn;移到try{}catch{}块的try{}块中就没有问题了。
具体如下

================正确代码=========================
try{

    ***
    conn=DriverManager.getVonnection(url,user,password);

     return conn;

}catch(ClassNotFoundException e){
   e.printStackTrace();
}catch (SQLException e){
   e.printStackTrace();
}
}

=================================================

这就没问题了

可为什么这样不明白

原创粉丝点击