I'm building this library, but it generates an error, tells me that the drivers are missing and therefore the connection does not open because of null value.
Something is missing.
Errore:
P.S. I know you can avoid the LoadBitmap and SaveBitmap to pass images in byte arrays, I'll optimize the code later, now I'm trying to make it work
Something is missing.
B4X:
Public Sub Update(filenameDB As String, TableName As String,ID As Int, Field As String, fn as string)
Dim J As JavaObject = Me
J.RunMethod("SetDB",Array As String(filenameDB))
J.RunMethod("update",Array As Object(TableName,Field,ID,fn))
End Sub
#if Java
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import anywheresoftware.b4a.BA.Permissions;
@Permissions(values={"android.permission.WRITE_EXTERNAL_STORAGE"})
String url = "jdbc:sqlite:C://sqlite/db/test.db";
public void SetDB(String filename) {
url = "jdbc:sqlite:" + filename;
BA.Log(url);
}
public Connection connect() {
// SQLite connection string
Connection conn = null;
try {
conn = DriverManager.getConnection(url); ' FIRST ERROR ---------------
} catch (SQLException e) {
System.out.println(e.getMessage());
BA.Log("Errore: " + e.getMessage());
}
return conn;
}
public void update(String tablename, String fieldname, int materialId, String filename) {
// update sql
String updateSQL = "UPDATE " + tablename
+ " SET "+ fieldname + " = ? "
+ "WHERE ID= ?";
try {
BA.Log("1");
Connection conn = connect();
BA.Log("2: " + updateSQL);
PreparedStatement pstmt = conn.prepareStatement(updateSQL);
// ----------------- HERE: Crash - Conn is nul --------------------
// set parameters
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
#End If
Errore:
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
jdbc:sqlite:/storage/emulated/0/test.sql
1
Error: No suitable driver found for jdbc:sqlite:/storage/emulated/0/test.sql
2: UPDATE mytable SET bitmap = ? WHERE ID= ?
sqlite_updatebitmap (B4A line: 15)
J.RunMethod("updatePicture",Array As Object(Table
java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:131)
at b4a.example.sqlite._updatebitmap(sqlite.java:161)
at b4a.example.main._buttonsave1_click(main.java:482)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:186)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:170)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:166)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
at android.view.View.performClick(View.java:6199)
at android.widget.TextView.performClick(TextView.java:11090)
at android.view.View$PerformClick.run(View.java:23647)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'java.sql.PreparedStatement java.sql.Connection.prepareStatement(java.lang.String)' on a null object reference
at b4a.example.sqlite.updatePicture(sqlite.java:228)
... 19 more
P.S. I know you can avoid the LoadBitmap and SaveBitmap to pass images in byte arrays, I'll optimize the code later, now I'm trying to make it work
Last edited: