B4J Question B4XTable last page button error

Chris2

Active Member
Licensed User
I have added conditional formatting to a B4XTable based on this, but am getting an error in the routine when I click on the last page button.

For Example:
B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private xui As XUI
    Private table1 As B4XTable
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout1")
    MainForm.Show
   
    table1.MaximumRowsPerPage=2
   
    table1.AddColumn("co1", table1.COLUMN_TYPE_NUMBERS)
    table1.AddColumn("col2", table1.COLUMN_TYPE_NUMBERS)
    table1.AddColumn("col3", table1.COLUMN_TYPE_NUMBERS)
   
    Dim data As List
    data.Initialize
    data.Add(Array(1, 6, 5))
    data.Add(Array(2, 5, 8))
    data.Add(Array(7, 2, 1))
    data.Add(Array(9, 6, 3))
    table1.SetData(data)
End Sub


Private Sub table1_DataUpdated
'    https://www.b4x.com/android/forum/threads/solved-how-to-change-the-color-of-a-b4xtable-row-according-to-content-and-then-disable-it.115988/#content
'    change cell background colour based on value being above targetMax

        For j=0 To table1.Columns.Size-1  
            Log(j)
            Dim col As B4XTableColumn = table1.Columns.Get(j)
           
            For i = 0 To table1.VisibleRowIds.Size - 1   
                Dim RowId As Long = table1.VisibleRowIds.Get(i)
                Log(RowId)
                Dim Value As Float = table1.GetRow(RowId).Get(col.Id)
                Dim pnl As B4XView = col.CellsLayouts.Get(i+1)
                SetCellColour(pnl, NumberFormat(Value, 1, 4))
               
            Next  
    Next
   
End Sub

'if high then set red
Private Sub SetCellColour (pnl As B4XView, value As Double)
       
        If value > 5 Then

            pnl.Color=xui.Color_RGB(255, 129, 129)
           
        Else
           
            pnl.Color=xui.Color_Transparent
           
        End If
   
End Sub
This works fine if I use just the lblFirst, lblBack, or lblNext B4XTable navigation buttons, but clicking lblLast gives:
B4X:
Error occurred on line: 882 (B4XTable)
java.lang.NumberFormatException: For input string: "null"
    at java.base/jdk.internal.math.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2054)
    at java.base/jdk.internal.math.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
    at java.base/java.lang.Double.parseDouble(Double.java:543)
    at anywheresoftware.b4a.BA.ObjectToNumber(BA.java:434)
    at b4j.example.main._table1_dataupdated(main.java:181)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:629)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:237)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
    at jdk.internal.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:111)
    at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:100)
    at anywheresoftware.b4a.keywords.Common.CallSub4(Common.java:514)
    at anywheresoftware.b4a.keywords.Common.CallSubNew(Common.java:461)
    at b4j.example.b4xtable$ResumableSub_ImplUpdateDataFromQuery.resume(b4xtable.java:3128)
    at anywheresoftware.b4a.shell.DebugResumableSub$DelegatableResumableSub.resumeAsUserSub(DebugResumableSub.java:47)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:629)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:237)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
    at jdk.internal.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:111)
    at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:100)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:98)
    at anywheresoftware.b4a.shell.DebugResumableSub$DelegatableResumableSub.resume(DebugResumableSub.java:42)
    at anywheresoftware.b4a.keywords.Common$2$1.run(Common.java:1051)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
    at java.base/java.lang.Thread.run(Thread.java:834)
I've added some logs to the GetRow sub of the B4XTable:
B4XTable GetRow:
'Gets the data of the row with the given row id. The data is returned as a map with the columns ids as the keys.
Public Sub GetRow(RowId As Long) As Map
log("GetRow")
log(rowId)
    Dim m As Map
    m.Initialize
    Dim rs As ResultSet = sql1.ExecQuery2("SELECT * FROM data WHERE rowid = ?", Array As String(RowId))
    If rs.NextRow Then
        For Each c As B4XTableColumn In Columns
        log(c.id)
        log(c.ColumnType)
            Select c.ColumnType
                Case COLUMN_TYPE_TEXT
                    m.Put(c.Id, rs.GetString(c.SQLID))
                Case COLUMN_TYPE_NUMBERS
                    m.Put(c.Id, rs.GetDouble(c.SQLID))
                Case COLUMN_TYPE_DATE
                    m.Put(c.Id, rs.GetLong(c.SQLID))
            End Select
        Next
    End If
    rs.Close
    Return m
End Sub
which gives the following when I generate the error:
logs leading to error:
0             log(j)
4             log(rowID) in table1_DataUpdated
GetRow
4             log(rowId) in GetRow
co1        log(c.id)
2            log(c.ColumnType)
col2        log(c.id)
2            log(c.ColumnType)
col3        log(c.id)
2            log(c.ColumnType)
0            log(rowID) in table1_DataUpdated
GetRow
0            log(rowId) in GetRow
Error occurred on line: 882 (B4XTable)
java.lang.NumberFormatException: For input string: "null"
....
Which looks to me like the error occurs because we have a rowId of 0 in the B4XTable list VisibleRowIds.
I can see where this is added in the B4XTable class, but am not sure why.

Am I doing something wrong here, or is this a mistake in B4xTable?

I've attached my example program.
Many thanks.
 

Attachments

  • B4XTableLastPageTest.zip
    2.6 KB · Views: 68
Solution
B4X:
            For i = 0 To table1.VisibleRowIds.Size - 1   
                Dim RowId As Long = table1.VisibleRowIds.Get(i)
               If RowId > 0 Then
                Dim Value As Float = table1.GetRow(RowId).Get(col.Id)
                Dim pnl As B4XView = col.CellsLayouts.Get(i+1)
                SetCellColour(pnl, NumberFormat(Value, 1, 4))
               Else 
                  'empty row. You might want to clear the color here.
               End If
            Next

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X:
            For i = 0 To table1.VisibleRowIds.Size - 1   
                Dim RowId As Long = table1.VisibleRowIds.Get(i)
               If RowId > 0 Then
                Dim Value As Float = table1.GetRow(RowId).Get(col.Id)
                Dim pnl As B4XView = col.CellsLayouts.Get(i+1)
                SetCellColour(pnl, NumberFormat(Value, 1, 4))
               Else 
                  'empty row. You might want to clear the color here.
               End If
            Next
 
Upvote 0
Solution
Top