Android Question Getting error with if statement

ctd

Member
Licensed User
Dear Forum
I am new with BA4 and i am building an app that search a product in a database. This is my code:
B4X:
#Region  Project Attributes
    #ApplicationLabel: Barcode Scan
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Public SQL1 As SQL
    Public SQLDataBasePath As String
    Public SQLDataBaseName As String
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Private TabStrip1 As TabStrip
   
    Private CodeBarreEditText As EditText
   
    Private EffacerButton As Button
    Private RechercheButton As Button
    Private RefEditText As EditText
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    'This is the program entry point.
    'This is a good place to load resources that are not specific to a single activity.
    SQLDataBasePath = File.DirRootExternal
    SQLDataBaseName = "inventaire.db"
    'check if the database already exists
    If File.Exists(SQLDataBasePath, SQLDataBaseName) = True Then
        'if yes initialize it
        SQL1.Initialize(SQLDataBasePath, SQLDataBaseName, True)
    Else
        'if no copy it
        File.Copy(File.DirAssets, SQLDataBaseName, SQLDataBasePath, SQLDataBaseName)
        'and initialize it
        SQL1.Initialize(SQLDataBasePath, SQLDataBaseName, True)
    End If
    Activity.LoadLayout("Main")
    TabStrip1.LoadLayout("Page1", "Scan")
    TabStrip1.LoadLayout("Page2", "Affichage")
End Sub
Sub SearchEntry
    Dim cursor1 As Cursor
    'RefEditText.text = SQL1.ExecQuery("Select DES from articles WHERE COBA = " & CodeBarreEditText.Text)
    cursor1 = SQL1.ExecQuery("Select * from articles WHERE COBA = " & CodeBarreEditText.Text)
    cursor1.Position = 0
    If cursor1.GetString("DES") == "" Then
         Msgbox("ARTICLE INEXISTANT","AVERTISSEMENT")
    Else
        RefEditText.text = cursor1.GetString("DES")   
    End If
    cursor1.Close
End Sub


Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub


Sub TabStrip1_PageSelected (Position As Int)
   
End Sub

Sub CleanEntry
    CodeBarreEditText.Text = ""
    RefEditText.text = ""
End Sub



Sub RechercheButton_Click
    SearchEntry
End Sub

Sub EffacerButton_Click
    CleanEntry
End Sub
when i compile all things are correct but when search a product that does not exist i get the following error in debug mode.
Error occurred on line: 62 (Main)
android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
 

stevel05

Expert
Licensed User
Longtime User
Check that at least 1 result has been returned before trying to read it using cursor1.RowCount :

B4X:
Sub SearchEntry
    Dim cursor1 As Cursor
    'RefEditText.text = SQL1.ExecQuery("Select DES from articles WHERE COBA = " & CodeBarreEditText.Text)
    cursor1 = SQL1.ExecQuery("Select * from articles WHERE COBA = " & CodeBarreEditText.Text)
    If cursor1.RowCount > 0 Then
        cursor1.Position = 0
        If cursor1.GetString("DES") == "" Then
             Msgbox("ARTICLE INEXISTANT","AVERTISSEMENT")
        Else
            RefEditText.text = cursor1.GetString("DES")  
        End If
    End If
    cursor1.Close
End Sub
 
Upvote 0

ctd

Member
Licensed User
Dear stevel05
I no longer get the message error. But the expected message
ARTICLE INEXISTANT did not pop pup.
Kindly
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
try this:
B4X:
If cursor1.GetString("DES") =  ""  or  cursor1.GetString("DES") =  NULL  Then
 Msgbox("ARTICLE INEXISTANT","AVERTISSEMENT")
bonne chance
 
Upvote 0

ctd

Member
Licensed User
Thank a lot for helping me. I finaly get the result with:
B4X:
Sub SearchEntry
    Dim cursor1 As Cursor
    'RefEditText.text = SQL1.ExecQuery("Select DES from articles WHERE COBA = " & CodeBarreEditText.Text)
    cursor1 = SQL1.ExecQuery("Select * from articles WHERE COBA = " & CodeBarreEditText.Text)
    If cursor1.RowCount = 0 Then
        'cursor1.Position = 0
        'If cursor1.GetString("DES") =  ""  Or  cursor1.GetString("DES") =  Null Then
             Msgbox("ARTICLE INEXISTANT","AVERTISSEMENT")
            'RefEditText.text = "ARTICLE INEXISTANT"
        Else
            cursor1.Position = 0
            RefEditText.text = cursor1.GetString("DES")
        'End If
    End If
    cursor1.Close
End Sub
 
Upvote 0
Top