当前位置: 技术问答>java相关
java.lang.NullPointerException ????????
来源: 互联网 发布时间:2017-03-09
本文导语: ==============连接数据库的Bean====================== package bbsclass; import java.sql.*; public class bbsconn{ String sDBDriver = "sun.jdbc.odbc.JdbcOdbcDriver"; String sConnStr = "jdbc:odbc:bbs"; Connection conn = null; ResultSet rs = null; ...
==============连接数据库的Bean======================
package bbsclass;
import java.sql.*;
public class bbsconn{
String sDBDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
String sConnStr = "jdbc:odbc:bbs";
Connection conn = null;
ResultSet rs = null;
public bbsconn() {
try {
Class.forName(sDBDriver);
}
catch(java.lang.ClassNotFoundException e) {
System.err.println("bbsreg(): " + e.getMessage());
}
}
public ResultSet executeQuery(String sql) {
rs = null;
try {
conn = DriverManager.getConnection(sConnStr);
Statement stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
}
catch(SQLException ex) {
System.err.println("aq.executeQuery: " + ex.getMessage());
System.err.println("aq.executeQuerystrSQL: " + sql);
}
return rs;
}
.....
}
=========只是知执行一个查询的过程==========================
import bbsclass.bbsconn;
import java.io.PrintStream;
import java.sql.ResultSet;
import java.sql.SQLException;
public class TestBbs implements java.io.Serializable
{
bbsconn bbsCon=null;
ResultSet rs;
public void TestBbs()
{
bbsCon = new bbsconn();
rs=null;
}
public boolean search()
{
ResultSet rs = null;
try
{
rs = bbsCon.executeQuery("select * from user");
if (rs.next())
{
return true;
}
else
return false;
}
catch (SQLException e)
{
System.err.println(e.getMessage());
System.out.println(e.getMessage());
return false;
}
}
public static void main(String[] args)
{
TestBbs testBbs = new TestBbs();
if (testBbs.search())
{
System.out.print("有数据!");
}
else
{
System.out.print("没有数据!");
}
}
}
执行TestBbs 的时候总是报:
java.lang.NullPointerException
at TestBbs.search(TestBbs.java:22)
at TestBbs.main(TestBbs.java:40)
Exception in thread "main" Normal Termination
我该怎么处理????
package bbsclass;
import java.sql.*;
public class bbsconn{
String sDBDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
String sConnStr = "jdbc:odbc:bbs";
Connection conn = null;
ResultSet rs = null;
public bbsconn() {
try {
Class.forName(sDBDriver);
}
catch(java.lang.ClassNotFoundException e) {
System.err.println("bbsreg(): " + e.getMessage());
}
}
public ResultSet executeQuery(String sql) {
rs = null;
try {
conn = DriverManager.getConnection(sConnStr);
Statement stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
}
catch(SQLException ex) {
System.err.println("aq.executeQuery: " + ex.getMessage());
System.err.println("aq.executeQuerystrSQL: " + sql);
}
return rs;
}
.....
}
=========只是知执行一个查询的过程==========================
import bbsclass.bbsconn;
import java.io.PrintStream;
import java.sql.ResultSet;
import java.sql.SQLException;
public class TestBbs implements java.io.Serializable
{
bbsconn bbsCon=null;
ResultSet rs;
public void TestBbs()
{
bbsCon = new bbsconn();
rs=null;
}
public boolean search()
{
ResultSet rs = null;
try
{
rs = bbsCon.executeQuery("select * from user");
if (rs.next())
{
return true;
}
else
return false;
}
catch (SQLException e)
{
System.err.println(e.getMessage());
System.out.println(e.getMessage());
return false;
}
}
public static void main(String[] args)
{
TestBbs testBbs = new TestBbs();
if (testBbs.search())
{
System.out.print("有数据!");
}
else
{
System.out.print("没有数据!");
}
}
}
执行TestBbs 的时候总是报:
java.lang.NullPointerException
at TestBbs.search(TestBbs.java:22)
at TestBbs.main(TestBbs.java:40)
Exception in thread "main" Normal Termination
我该怎么处理????
|
空指针异常,你给不该赋null值的变量赋了null值,改成下面这个试试
import bbsclass.bbsconn;
import java.io.PrintStream;
import java.sql.ResultSet;
import java.sql.SQLException;
public class TestBbs implements java.io.Serializable
{
bbsconn bbsCon=new bbsconn();;
ResultSet rs=null;
public void TestBbs()
{
}
public boolean search()
{
try
{
rs = bbsCon.executeQuery("select * from user");
if (rs.next())
{
return true;
}
else
return false;
}
catch (SQLException e)
{
System.err.println(e.getMessage());
System.out.println(e.getMessage());
return false;
}
}
public static void main(String[] args)
{
TestBbs testBbs = new TestBbs();
if (testBbs.search())
{
System.out.print("有数据!");
}
else
{
System.out.print("没有数据!");
}
}
}
import bbsclass.bbsconn;
import java.io.PrintStream;
import java.sql.ResultSet;
import java.sql.SQLException;
public class TestBbs implements java.io.Serializable
{
bbsconn bbsCon=new bbsconn();;
ResultSet rs=null;
public void TestBbs()
{
}
public boolean search()
{
try
{
rs = bbsCon.executeQuery("select * from user");
if (rs.next())
{
return true;
}
else
return false;
}
catch (SQLException e)
{
System.err.println(e.getMessage());
System.out.println(e.getMessage());
return false;
}
}
public static void main(String[] args)
{
TestBbs testBbs = new TestBbs();
if (testBbs.search())
{
System.out.print("有数据!");
}
else
{
System.out.print("没有数据!");
}
}
}