Android Question b4a and sql concting problem

nimavao

Member
Hi strugle with a problem that it's really dirve me crazy😭
my app made of layout with a Listview I made a sql declare ir intialize it but when I run program it give me some eror I've been working on it for 10 days and 8 hrs per day but I couldnt solve it becuse im not programer please guide me in easiest way that posible .
I tested my code step by step and this part is problem :

MainListBtn_Click that open layout and it's listview:
Sub MainListBtn_Click

    Activity.RemoveAllViews

    Activity.LoadLayout("laycontent")

    Dim Cur As Cursor
    Dim strContentList As String
    Dim   sqlDB As SQL
    Cur = sqlDB.ExecQuery ("Select * FROM tblContent")
    For i = 0 To Cur.RowCount -1
        Cur.Position = i
        strContentList = Cur.GetString("ContentList")
        contentListView.AddSingleLine2 (strContentList,i)
    Next
endsub
    'contentListView.SingleLineLayout.Label.TextColor = Colors.Black
End Sub

and it's shows me these Erors when I run :
Error occurred on line: 113 (Main)
java.lang.RuntimeException: Object should first be initialized.
at anywheresoftware.b4a.sql.SQL.checkNull(SQL.java:55)
at anywheresoftware.b4a.sql.SQL.ExecQuery(SQL.java:210)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:348)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:157)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:205)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:201)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
at android.view.View.performClick(View.java:7448)
at android.view.View.performClickInternal(View.java:7425)
at android.view.View.access$3600(View.java:810)
at android.view.View$PerformClick.run(View.java:28309)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7697)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:952)
 

Attachments

  • codexii.zip
    287 KB · Views: 48
Solution
Hi strugle with a problem that it's really dirve me crazy😭
my app made of layout with a Listview I made a sql declare ir intialize it but when I run program it give me some eror I've been working on it for 10 days and 8 hrs per day but I couldnt solve it becuse im not programer please guide me in easiest way that posible .
I tested my code step by step and this part is problem :

MainListBtn_Click that open layout and it's listview's listview:
Sub MainListBtn_Click

    Activity.RemoveAllViews

    Activity.LoadLayout("laycontent")

    Dim Cur As Cursor
    Dim strContentList As String
    Dim   sqlDB As SQL
    Cur = sqlDB.ExecQuery ("Select * FROM tblContent")
    For i = 0 To Cur.RowCount -1
        Cur.Position = i
        strContentList = Cur.GetString("ContentList")...

RB Smissaert

Well-Known Member
Licensed User
Longtime User
Hi strugle with a problem that it's really dirve me crazy😭
my app made of layout with a Listview I made a sql declare ir intialize it but when I run program it give me some eror I've been working on it for 10 days and 8 hrs per day but I couldnt solve it becuse im not programer please guide me in easiest way that posible .
I tested my code step by step and this part is problem :

MainListBtn_Click that open layout and it's listview's listview:
Sub MainListBtn_Click

    Activity.RemoveAllViews

    Activity.LoadLayout("laycontent")

    Dim Cur As Cursor
    Dim strContentList As String
    Dim   sqlDB As SQL
    Cur = sqlDB.ExecQuery ("Select * FROM tblContent")
    For i = 0 To Cur.RowCount -1
        Cur.Position = i
        strContentList = Cur.GetString("ContentList")
        contentListView.AddSingleLine2 (strContentList,i)
    Next
endsub
    'contentListView.SingleLineLayout.Label.TextColor = Colors.Black
End Sub

and it's shows me these Erors when I run :

Made some changes and this will work:

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Private xui As XUI
    Private SpTimer As Timer
    Private sqlDB As SQL
    Type RecordData (ArtistName As String, AlbumName As String)
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    Private TstPrgrsBar As ProgressBar
    Private contentListView As ListView

    
End Sub

Sub Activity_Create(FirstTime As Boolean)
    
    Activity.LoadLayout("Layout")
    
    SpTimer.Initialize("SpTimer",4000)
    SpTimer.Enabled = True
    
'    For x = 0 To 4000 -1
'        TstPrgrsBar.Progress = x
'        Sleep(2)
'    Next

    If File.Exists (File.DirInternal , "inbrcodes.db") = False Then
        File.Copy(File.DirAssets , "inbrcodes.db" , File.DirInternal , "inbrcodesConfirm.db")
    End If
    
    sqlDB.Initialize(File.DirInternal,"inbrcodesConfirm.db",True)
    
End Sub

Sub SpTimer_Tick
    Activity.RemoveAllViews
    SpTimer.Enabled = False
    Activity.LoadLayout("layMain")
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Button1_Click
    xui.MsgboxAsync("Hello world!", "B4X")
End Sub

Private Sub exitbtn_Click
    Dim exitvalue As Int
    exitvalue = Msgbox2 ( "آیا از خروج اطمینان دارید؟" , "پیام خروج" , " بله " , "خیر" , "" , Null)
    If (exitvalue = DialogResponse.POSITIVE) Then
    ExitApplication    
        
        
    End If
    
    
    
End Sub

Private Sub Activity_KeyPress (KeyCode As Int) As Boolean 'Return True to consume the event
    
    If KeyCode = KeyCodes.KEYCODE_BACK Then
    Dim exitvalue As Int
    exitvalue = Msgbox2 ( "آیا از خروج اطمینان دارید؟" , "پیام خروج" , " بله " , "خیر" , "" , Null)
    If (exitvalue = DialogResponse.POSITIVE) Then
        ExitApplication
    Else
        Return True    
    End If
    
    End If
    
    
    
End Sub

Private Sub contentListView_ItemClick (Position As Int, Value As Object)
    
End Sub

Sub MainListBtn_Click
    
    Activity.RemoveAllViews
    Activity.LoadLayout("laycontent")
    
    Dim Cur As Cursor
    Dim strContentList As String
'don't do Dim sqlDB As SQL here !!
    
    Cur = sqlDB.ExecQuery ("Select * FROM tblContent")
    
    For i = 0 To Cur.RowCount -1
        Cur.Position = i
        strContentList = Cur.GetString("ContentList")
        contentListView.AddSingleLine2 (strContentList,i)
    Next

    contentListView.SingleLineLayout.Label.TextColor = Colors.Black
    
End Sub

RBS
 
Upvote 1
Solution

nimavao

Member
Made some changes and this will work:

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Private xui As XUI
    Private SpTimer As Timer
    Private sqlDB As SQL
    Type RecordData (ArtistName As String, AlbumName As String)
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    Private TstPrgrsBar As ProgressBar
    Private contentListView As ListView

   
End Sub

Sub Activity_Create(FirstTime As Boolean)
   
    Activity.LoadLayout("Layout")
   
    SpTimer.Initialize("SpTimer",4000)
    SpTimer.Enabled = True
   
'    For x = 0 To 4000 -1
'        TstPrgrsBar.Progress = x
'        Sleep(2)
'    Next

    If File.Exists (File.DirInternal , "inbrcodes.db") = False Then
        File.Copy(File.DirAssets , "inbrcodes.db" , File.DirInternal , "inbrcodesConfirm.db")
    End If
   
    sqlDB.Initialize(File.DirInternal,"inbrcodesConfirm.db",True)
   
End Sub

Sub SpTimer_Tick
    Activity.RemoveAllViews
    SpTimer.Enabled = False
    Activity.LoadLayout("layMain")
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Button1_Click
    xui.MsgboxAsync("Hello world!", "B4X")
End Sub

Private Sub exitbtn_Click
    Dim exitvalue As Int
    exitvalue = Msgbox2 ( "آیا از خروج اطمینان دارید؟" , "پیام خروج" , " بله " , "خیر" , "" , Null)
    If (exitvalue = DialogResponse.POSITIVE) Then
    ExitApplication   
       
       
    End If
   
   
   
End Sub

Private Sub Activity_KeyPress (KeyCode As Int) As Boolean 'Return True to consume the event
   
    If KeyCode = KeyCodes.KEYCODE_BACK Then
    Dim exitvalue As Int
    exitvalue = Msgbox2 ( "آیا از خروج اطمینان دارید؟" , "پیام خروج" , " بله " , "خیر" , "" , Null)
    If (exitvalue = DialogResponse.POSITIVE) Then
        ExitApplication
    Else
        Return True   
    End If
   
    End If
   
   
   
End Sub

Private Sub contentListView_ItemClick (Position As Int, Value As Object)
   
End Sub

Sub MainListBtn_Click
   
    Activity.RemoveAllViews
    Activity.LoadLayout("laycontent")
   
    Dim Cur As Cursor
    Dim strContentList As String
'don't do Dim sqlDB As SQL here !!
   
    Cur = sqlDB.ExecQuery ("Select * FROM tblContent")
   
    For i = 0 To Cur.RowCount -1
        Cur.Position = i
        strContentList = Cur.GetString("ContentList")
        contentListView.AddSingleLine2 (strContentList,i)
    Next

    contentListView.SingleLineLayout.Label.TextColor = Colors.Black
   
End Sub

RBS
I dont belive that it works :D thank you , I just change your code a little (delete space befor :
B4X:
    Cur = sqlDB.ExecQuery ("Select * FROM tblContent")
and made it like this :
B4X:
Cur = sqlDB.ExecQuery ("Select * FROM tblContent")
and it worked unbelievably
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
and it's shows me these Erors when I run :
I think you did a great job with your project for someone who just started with B4X only 2 weeks ago, except where you had to initialize the database. That's what bogged you down. Since you are new, maybe we should tell you few things you should be doing and to stay away from others:
1. Use the newer B4XPages project style instead of the Default type when you create a project
2. Use xCustomlistview instead of Listview.
3. Use Resultset instead of Cursor when you are working with SQLite queries.
4. Use Msgbox2Async instead of Msgbox2.

See this link for more details:
 
Upvote 1

nimavao

Member
I think you did a great job with your project for someone who just started with B4X only 2 weeks ago, except where you had to initialize the database. That's what bogged you down. Since you are new, maybe we should tell you few things you should be doing and to stay away from others:
1. Use the newer B4XPages project style instead of the Default type when you create a project
2. Use xCustomlistview instead of Listview.
3. Use Resultset instead of Cursor when you are working with SQLite queries.
4. Use Msgbox2Async instead of Msgbox2.

See this link for more details:
Thank you for you great sentences that motivate me 🙏 I love creating app , in B4A program I face problem ,I need to know about properties .for example when we write activity.xxxx we see some properties instead of xxxx , is there any book or app that explains about all properties and give example?if you can guide me🙏thank you
Greeting ☺️
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
is there any book or app that explains about all properties and give example?if you can guide me🙏thank you

1. If you put 'erel videos' in the search box of the forum , the first search in the list is a link to all the teaching videos.
2. Also, If you look for a post made by 'klaus', you will see at the bottom of it a list of all the booklets links he made over time. All are constantly revised and up to date by klaus.
 
Upvote 1

DonManfred

Expert
Licensed User
Longtime User
Upvote 1
Top