2

我在创建与 Firebird 数据库的 android 连接时遇到问题,每当执行连接线时应用程序就会崩溃。

代码:

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

这是 eclipse 的 logcat 中的消息:

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

有人有这个错误的任何信息吗?

4

1 回答 1

1

问题出在资产文件夹中的属性文件上,正如 Mark 所建议的那样。在初始化器中

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

返回 null,这会导致以下几行出现 NullReferenceException。在 Android Studio 2.2 版中,解决方案是在
此处添加 assets.srcDirs = ['assets'] :

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

}

在项目 gradle 文件中。然后项目结构如下所示: 在此处输入图像描述

于 2016-11-30T14:42:30.937 回答