Creating connection with Android and Firebird JDBC

2.7k Views Asked by At

I'm having a problem in create an android connection with Firebird database, the app is crashing whenever executes the line of connection.

Code:

cod = ""+edit[0].getText();
cod = String.format("%14s", cod).replace(' ', '0');
String query = "SELECT DS_PRO FROM PROD WHERE CD_PRO = '"+cod+"'";
try{
    Class.forName("org.firebirdsql.jdbc.FBDriver").newInstance();
    System.err.println("jdbc created");
}catch(Exception e){
    System.err.println("Cannot create connection");
}

try{
    Properties props = new Properties();
    props.setProperty("user", "POINTER");
    props.setProperty("password", "pwdb");
    props.setProperty("encoding", "WIN1252");
    try{
        System.err.println("creating connection");
        java.sql.Connection connection = DriverManager.getConnection("jdbc:firebirdsql:192.168.0.196/3050:C:/Fenix/FENIX.FDB",props);
        System.err.println("created connection");
    }
    catch(Exception e){
        System.err.println("error in connection to server");
        System.err.println(e.getMessage());
    }
}
catch(Exception e){
    System.err.println("error in connection to server");
    System.err.println(e.getMessage());
}

This is the message in logcat of eclipse:

01-29 09:22:44.774: W/System.err(28820): jdbc created
01-29 09:22:44.774: W/System.err(28820): creating connection
01-29 09:22:44.974: E/AndroidRuntime(28820): FATAL EXCEPTION: AsyncTask #3
01-29 09:22:44.974: E/AndroidRuntime(28820): Process: com.example.testeconnection, PID: 28820
01-29 09:22:44.974: E/AndroidRuntime(28820): java.lang.RuntimeException: An error occured while executing doInBackground()
01-29 09:22:44.974: E/AndroidRuntime(28820):    at android.os.AsyncTask$3.done(AsyncTask.java:300)
01-29 09:22:44.974: E/AndroidRuntime(28820):    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
01-29 09:22:44.974: E/AndroidRuntime(28820):    at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
01-29 09:22:44.974: E/AndroidRuntime(28820):    at java.util.concurrent.FutureTask.run(FutureTask.java:242)
01-29 09:22:44.974: E/AndroidRuntime(28820):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
01-29 09:22:44.974: E/AndroidRuntime(28820):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
01-29 09:22:44.974: E/AndroidRuntime(28820):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
01-29 09:22:44.974: E/AndroidRuntime(28820):    at java.lang.Thread.run(Thread.java:811)
01-29 09:22:44.974: E/AndroidRuntime(28820): Caused by: java.lang.ExceptionInInitializerError
01-29 09:22:44.974: E/AndroidRuntime(28820):    at org.firebirdsql.jdbc.FBDriverPropertyManager.<clinit>(FBDriverPropertyManager.java:142)
01-29 09:22:44.974: E/AndroidRuntime(28820):    at org.firebirdsql.jdbc.AbstractDriver.connect(AbstractDriver.java:103)
01-29 09:22:44.974: E/AndroidRuntime(28820):    at java.sql.DriverManager.getConnection(DriverManager.java:179)
01-29 09:22:44.974: E/AndroidRuntime(28820):    at com.example.testeconnection.BdConnect.doInBackground(BdConnect.java:55)
01-29 09:22:44.974: E/AndroidRuntime(28820):    at com.example.testeconnection.BdConnect.doInBackground(BdConnect.java:1)
01-29 09:22:44.974: E/AndroidRuntime(28820):    at android.os.AsyncTask$2.call(AsyncTask.java:288)
01-29 09:22:44.974: E/AndroidRuntime(28820):    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
01-29 09:22:44.974: E/AndroidRuntime(28820):    ... 4 more
01-29 09:22:44.974: E/AndroidRuntime(28820): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.Set java.util.Properties.entrySet()' on a null object reference
01-29 09:22:44.974: E/AndroidRuntime(28820):    at org.firebirdsql.jdbc.FBConnectionHelper.loadDpbParameterTypes(FBConnectionHelper.java:289)
01-29 09:22:44.974: E/AndroidRuntime(28820):    at org.firebirdsql.jdbc.FBConnectionHelper.<clinit>(FBConnectionHelper.java:106)
01-29 09:22:44.974: E/AndroidRuntime(28820):    ... 11 more

somebody have any information of this error?

1

There are 1 best solutions below

0
On

The problem is with the properties files in the assets folder, as suggested by Mark. In the initializer

props = loadProperties("assets/isc_dpb_types.properties");

returns null, which causes the NullReferenceException some lines below. In Android Studio version 2.2 the solution is to add
assets.srcDirs = ['assets'] here:

android {
compileSdkVersion 24
buildToolsVersion "24.0.0"
defaultConfig {
 ....
}
buildTypes {
    release {
       ...
    }
}
sourceSets {
    main {
        assets.srcDirs = ['assets']
    }
}

}

in the project graddle file. And then the project structure looks like this: enter image description here