PostgreSQL JDBC and Android – NullPointerException-Collection of common programming errors

I’ve got problem with PostgreSQL JDBC driver on Android.
I use postgresql-9.2-1002.jdbc4.jar JDBC driver.

Here’s my code:

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;      
}
}

When I run this method I’ve got 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.(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.(AbstractJdbc2Connection.java:136)
04-29 15:24:43.568: W/System.err(549):  at org.postgresql.jdbc3.AbstractJdbc3Connection.(AbstractJdbc3Connection.java:29)
04-29 15:24:43.578: W/System.err(549):  at org.postgresql.jdbc3g.AbstractJdbc3gConnection.(AbstractJdbc3gConnection.java:21)
04-29 15:24:43.578: W/System.err(549):  at org.postgresql.jdbc4.AbstractJdbc4Connection.(AbstractJdbc4Connection.java:31)
04-29 15:24:43.598: W/System.err(549):  at org.postgresql.jdbc4.Jdbc4Connection.(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)

I’ve no idea what’s wrong. I know I should use web service (it’s safer) but I’ve got SQL hosting only.

Can anybody help me, please?

Originally posted 2013-11-16 20:49:38.