2013-04-22 32 views
0

我想用Android连接到SQL Server。NullPointerException取决于createStatement设备

这是我的代码:

Class.forName("net.sourceforge.jtds.jdbc.Driver"); 
conn = DriverManager.getConnection(db_connect_string, db_userid, db_password); 

try{ 
Statement ps = conn.createStatement(); 
ResultSet rs = ps.executeQuery ("select * from cliente"); 
..... 
} 
catch (SQLException e) { 
e.printStackTrace(); 
} 

一切都OK了,如果我跑在三星Galaxy王牌的应用程序,但如果我想在其他设备中运行,这是错误:

java.lang.NullPointerException 

,但此错误是路线:

Statement ps = conn.createStatement(); 

我不知道为什么,根据设备运行这个或N OT。

回答

0
Statement ps = db.conn.createStatement(); 

这应该是

Statement ps = conn.createStatement(); 

这里分贝值null我GUSS,所以你从下面这行代码得到NullPointerException
如有任何问题,请让我知道。

编辑:

如果数据库中不存在你的代码,然后把系统输出,如:

conn = DriverManager.getConnection(db_connect_string, db_userid, db_password); 
Sysout.out.Println("value of conn is "+conn); 

并检查康恩的价值。根据我的理解,这是空的。

FYI:

Class.forName("net.sourceforge.jtds.jdbc.Driver"); 
conn = DriverManager.getConnection(db_connect_string, db_userid, db_password); 

这些拖行必有我一个try/catch块。

+0

对不起,“db。”不在代码中,我不知道为什么在这里。问题是为什么在某些设备上它工作而在另一个设备上不工作 – mglprz87 2013-04-22 12:21:38

+0

如果db不在你的代码中并且变量conn不存在于db对象中,那么你将在运行时得到编译时问题而不是NullPointerexception。 – 2013-04-22 12:23:01

+0

我已经改变了答案,db。不应该在这里,一切都在同一个班,就像现在的代码一样。对不起 – mglprz87 2013-04-22 12:25:30

相关问题