PostgreSQL JDBC 和 Android - NullPointerException
Posted
技术标签:
【中文标题】PostgreSQL JDBC 和 Android - NullPointerException【英文标题】:PostgreSQL JDBC and Android - NullPointerException 【发布时间】:2013-04-29 13:55:42 【问题描述】:android 上的 PostgreSQL JDBC 驱动程序有问题。 我使用 postgresql-9.2-1002.jdbc4.jar JDBC 驱动程序。
这是我的代码:
package xyz;
import java.sql.*;
public class BaseManager
private String baseUrl = "jdbc:postgresql://0.0.0.0/database?user=username&password=secret";
private Connection conn = null;
public Integer getUserId(String login)
Integer result = -1;
try
Class.forName("org.postgresql.Driver");
conn = DriverManager.getConnection(baseUrl);
//query here
conn.close();
catch (SQLException e)
e.printStackTrace();
catch (ClassNotFoundException e1)
e1.printStackTrace();
return result;
当我运行这个方法时,我得到了 NullPointerException:
04-29 15:24:43.458: W/System.err(549): org.postgresql.util.PSQLException: Something unusual has occured to cause the driver to fail. Please report this exception.
04-29 15:24:43.468: W/System.err(549): at org.postgresql.Driver.connect(Driver.java:287)
04-29 15:24:43.468: W/System.err(549): at java.sql.DriverManager.getConnection(DriverManager.java:175)
04-29 15:24:43.468: W/System.err(549): at java.sql.DriverManager.getConnection(DriverManager.java:140)
04-29 15:24:43.468: W/System.err(549): at xyz.BaseManager.getUserId(BaseManager.java:20)
04-29 15:24:43.468: W/System.err(549): at xyz.LoginActivity.checkPassword(LoginActivity.java:50)
04-29 15:24:43.478: W/System.err(549): at java.lang.reflect.Method.invokeNative(Native Method)
04-29 15:24:43.478: W/System.err(549): at java.lang.reflect.Method.invoke(Method.java:511)
04-29 15:24:43.478: W/System.err(549): at android.view.View$1.onClick(View.java:3039)
04-29 15:24:43.478: W/System.err(549): at android.view.View.performClick(View.java:3511)
04-29 15:24:43.478: W/System.err(549): at android.view.View$PerformClick.run(View.java:14105)
04-29 15:24:43.488: W/System.err(549): at android.os.Handler.handleCallback(Handler.java:605)
04-29 15:24:43.488: W/System.err(549): at android.os.Handler.dispatchMessage(Handler.java:92)
04-29 15:24:43.488: W/System.err(549): at android.os.Looper.loop(Looper.java:137)
04-29 15:24:43.498: W/System.err(549): at android.app.ActivityThread.main(ActivityThread.java:4424)
04-29 15:24:43.498: W/System.err(549): at java.lang.reflect.Method.invokeNative(Native Method)
04-29 15:24:43.498: W/System.err(549): at java.lang.reflect.Method.invoke(Method.java:511)
04-29 15:24:43.498: W/System.err(549): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-29 15:24:43.518: W/System.err(549): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-29 15:24:43.518: W/System.err(549): at dalvik.system.NativeStart.main(Native Method)
04-29 15:24:43.518: W/System.err(549): Caused by: android.os.NetworkOnMainThreadException
04-29 15:24:43.528: W/System.err(549): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
04-29 15:24:43.528: W/System.err(549): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
04-29 15:24:43.528: W/System.err(549): at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
04-29 15:24:43.538: W/System.err(549): at libcore.io.IoBridge.connect(IoBridge.java:112)
04-29 15:24:43.538: W/System.err(549): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
04-29 15:24:43.548: W/System.err(549): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
04-29 15:24:43.558: W/System.err(549): at java.net.Socket.connect(Socket.java:842)
04-29 15:24:43.558: W/System.err(549): at java.net.Socket.connect(Socket.java:785)
04-29 15:24:43.568: W/System.err(549): at org.postgresql.core.PGStream.<init>(PGStream.java:60)
04-29 15:24:43.568: W/System.err(549): at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:101)
04-29 15:24:43.568: W/System.err(549): at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:64)
04-29 15:24:43.568: W/System.err(549): at org.postgresql.jdbc2.AbstractJdbc2Connection.<init>(AbstractJdbc2Connection.java:136)
04-29 15:24:43.568: W/System.err(549): at org.postgresql.jdbc3.AbstractJdbc3Connection.<init>(AbstractJdbc3Connection.java:29)
04-29 15:24:43.578: W/System.err(549): at org.postgresql.jdbc3g.AbstractJdbc3gConnection.<init>(AbstractJdbc3gConnection.java:21)
04-29 15:24:43.578: W/System.err(549): at org.postgresql.jdbc4.AbstractJdbc4Connection.<init>(AbstractJdbc4Connection.java:31)
04-29 15:24:43.598: W/System.err(549): at org.postgresql.jdbc4.Jdbc4Connection.<init>(Jdbc4Connection.java:24)
04-29 15:24:43.598: W/System.err(549): at org.postgresql.Driver.makeConnection(Driver.java:393)
04-29 15:24:43.628: W/System.err(549): at org.postgresql.Driver.connect(Driver.java:267)
04-29 15:24:43.628: W/System.err(549): ... 18 more
04-29 15:24:43.898: E/System(549): Uncaught exception thrown by finalizer
04-29 15:24:43.898: E/System(549): java.lang.NullPointerException
04-29 15:24:43.898: E/System(549): at org.postgresql.jdbc2.AbstractJdbc2Connection.close(AbstractJdbc2Connection.java:651)
04-29 15:24:43.898: E/System(549): at org.postgresql.jdbc2.AbstractJdbc2Connection.finalize(AbstractJdbc2Connection.java:1003)
04-29 15:24:43.898: E/System(549): at java.lang.Daemons$FinalizerDaemon.doFinalize(Daemons.java:182)
04-29 15:24:43.898: E/System(549): at java.lang.Daemons$FinalizerDaemon.run(Daemons.java:168)
04-29 15:24:43.898: E/System(549): at java.lang.Thread.run(Thread.java:856)
我不知道出了什么问题。 我知道我应该使用网络服务(它更安全),但我只有 SQL 托管。 有人可以帮帮我吗?
【问题讨论】:
一般来说我建议避免使用android中的JDBC。运行一个应用服务器,公开 REST API 调用供您的应用使用,然后从 android 应用调用它。见***.com/q/15853367/398670 【参考方案1】:事件日志显示
Caused by: android.os.NetworkOnMainThreadException
您应该将代码移到它自己的线程或AsyncTask
您可以阅读此异常 here 并了解它发生的原因。
【讨论】:
感谢您的回答。我会尝试使用 AsyncTask。以上是关于PostgreSQL JDBC 和 Android - NullPointerException的主要内容,如果未能解决你的问题,请参考以下文章
如何做批处理 jdbc/insert!和 jdbc/更新!在 Clojure 中使用 postgresql?
如何在 android 中安全(间接)查询 postgresql 数据库?