Android Question how to get data from table

Makumbi

Well-Known Member
Licensed User
Please help iam able to insert data into a table but i can not display it on my chat please help on trying to display i get the error below please find attached a small project
B4X:
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
Error occurred on line: 61 (Main)
java.lang.NullPointerException: Attempt to read from field 'anywheresoftware.b4a.objects.B4XViewWrapper$B4XFont b4a.example.bbcodeparser$_bbcodeparsedata.DefaultBoldFont' on a null object reference
    at b4a.example.main._buildmessage(main.java:480)
    at b4a.example.main._additem(main.java:452)
    at b4a.example.main._activity_resume(main.java:424)
    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:144)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:176)
    at b4a.example.main.afterFirstLayout(main.java:110)
    at b4a.example.main.access$000(main.java:17)
    at b4a.example.main$WaitForLayout.run(main.java:82)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6669)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)


B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: portrait
    #CanInstallToExternalStorage: False
#End Region

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

Sub Process_Globals
    Public SQL1 As SQL
    Dim cursor1 As Cursor
End Sub

Sub Globals
    Private xui As XUI
    Private chat1 As Chat
    Private ime As IME
    Private CLV As CustomListView
    Private BBCodeView1 As BBCodeView
    Private Engine As BCTextEngine
    Private bc As BitmapCreator
    Private LastUserLeft2 As Boolean
End Sub


Sub Activity_Create(FirstTime As Boolean)
    chat1.Initialize(Activity)
    ime.Initialize("ime")
    ime.AddHeightChangedEvent
    Engine.Initialize(Activity)
    bc.Initialize(300, 300)
    'TextField.NextField = TextField
End Sub

Sub IME_HeightChanged (NewHeight As Int, OldHeight As Int)
    chat1.HeightChanged(NewHeight)
End Sub



Sub Activity_Resume
    
    AddItem("Hullo every one",LastUserLeft2)
    


End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Private Sub BuildMessage (Text As String, User As String) As List
    Dim title As BCTextRun = Engine.CreateRun(User & CRLF)
    title.TextFont  = BBCodeView1.ParseData.DefaultBoldFont
    Dim TextRun As BCTextRun = Engine.CreateRun(Text & CRLF)
    Dim time As BCTextRun = Engine.CreateRun(DateTime.Time(DateTime.Now))
    time.TextFont = xui.CreateDefaultFont(10)
    time.TextColor = xui.Color_Gray
    Return Array(title, TextRun, time)
End Sub

Private Sub AddItem (Text As String, Right As Boolean)
    Dim p As B4XView = xui.CreatePanel("")
    Dim User As String
    If Right Then User = "User 2" Else User = "User 1"
    BBCodeView1.ExternalRuns = BuildMessage(Text, User)
    BBCodeView1.ParseAndDraw
    
    CLV.Add(p, Null)
    ScrollToLastItem
End Sub
    
Private Sub ScrollToLastItem
    Sleep(100)
    If CLV.Size > 0 Then
        CLV.ScrollToItem(CLV.Size - 1)
    End If
End Sub
Sub CLV_ItemLongClick (Index As Int, Value As Object)
    Log("ItemLongClick " & Index)
    Msgbox2Async("Do you to delete this panel?", "Delete", "Confirm", "", "Cancel", Null, True)
    Wait For Msgbox_Result (Result As Int)
    If Result <> DialogResponse.POSITIVE Then Return
    'CLV.RemoveAt(Index)
End Sub


B4X:
#IgnoreWarnings: 6
Sub Class_Globals
    Private xui As XUI
    Private TextField As B4XFloatTextField
    Private CLV As CustomListView
    Private BBCodeView1 As BBCodeView
    Private Engine As BCTextEngine
    Private bc As BitmapCreator
    Private ArrowWidth As Int = 10dip
    Private Gap As Int = 6dip
    Private pnlBottom As B4XView
    Private LastUserLeft As Boolean
End Sub

Public Sub Initialize (Parent As B4XView)
    Parent.LoadLayout("1")
    Engine.Initialize(Parent)
    bc.Initialize(300, 300)
    TextField.NextField = TextField
End Sub

Private Sub lblSend_Click
    If TextField.Text.Length > 0 Then
        LastUserLeft = Not(LastUserLeft)
        AddItem(TextField.Text, LastUserLeft)
        
    
    End If
    TextField.RequestFocusAndShowKeyboard
    #if B4J
    Dim ta As TextArea = TextField.TextField
    ta.SelectAll
    #else if B4A
    Dim et As EditText = TextField.TextField
    et.SelectAll
    #end if
    Dim sx As String
    sx="User"
    'Starter.SQL1.ExecNonQuery2("INSERT INTO smsdata VALUES(?,?,?,?)", Array As Object(LTrim(RTrim(TextField.Text)), LTrim(RTrim(LastUserLeft))),LTrim(RTrim(DateTime.now)) ,sx))
    
    Starter.SQL1.ExecNonQuery2("INSERT INTO smsdata VALUES(?, ?, ?,?)", Array As Object(LTrim(RTrim(TextField.Text)), LTrim(RTrim(LastUserLeft)),LTrim(RTrim(DateTime.now)),sx))
End Sub


Sub LTrim(s As String) As String
    Dim m As Matcher = Regex.Matcher("^(\s+)", s)
    If m.Find Then
        Return s.SubString(m.GetEnd(1))
    Else
        Return s
    End If
End Sub

Sub RTrim(s As String) As String
    Dim m As Matcher = Regex.Matcher("(\s+)$", s)
    If m.Find Then
        Return s.SubString(m.GetEnd(1))
    Else
        Return s
    End If
End Sub
Public Sub HeightChanged (NewHeight As Int)
    Dim c As B4XView = CLV.AsView
    c.Height = NewHeight - pnlBottom.Height
    CLV.Base_Resize(c.Width, c.Height)
    pnlBottom.Top = NewHeight - pnlBottom.Height
    ScrollToLastItem
End Sub

Private Sub AddItem (Text As String, Right As Boolean)
    Dim p As B4XView = xui.CreatePanel("")
    Dim User As String
    If Right Then User = "User 2" Else User = "User 1"
    BBCodeView1.ExternalRuns = BuildMessage(Text, User)
    BBCodeView1.ParseAndDraw
    Dim ivText As B4XView = CreateImageView
    Dim bmpText As B4XBitmap = GetBitmap(BBCodeView1.ForegroundImageView)
    bc.SetBitmapToImageView(bmpText, ivText)
    Dim ivBG As B4XView = CreateImageView
    Dim bmpBG As B4XBitmap = DrawBubble(bmpText.Width, bmpText.Height, Right)
    bc.SetBitmapToImageView(bmpBG, ivBG)
    p.SetLayoutAnimated(0, 0, 0, CLV.AsView.Width, bmpText.Height + 3 * Gap)
    p.AddView(ivBG, 0, Gap, bmpBG.Width * xui.Scale, bmpBG.Height * xui.Scale)
    p.AddView(ivText, Gap + ArrowWidth, 2 * Gap, bmpText.Width, bmpText.Height)
    CLV.Add(p, Null)
    ScrollToLastItem
End Sub

Private Sub ScrollToLastItem
    Sleep(100)
    If CLV.Size > 0 Then
        CLV.ScrollToItem(CLV.Size - 1)
    End If
End Sub

Private Sub DrawBubble (Width As Int, Height As Int, Right As Boolean) As B4XBitmap
    Width = Ceil(Width / xui.Scale)
    Height = Ceil(Height / xui.Scale)
    Dim ScaledGap As Int = Ceil(Gap / xui.Scale)
    Dim ScaledArrowWidth As Int = Ceil(ArrowWidth / xui.Scale)
    Dim nw As Int = Width + 3 * ScaledGap + ScaledArrowWidth
    Dim nh As Int = Height + 2 * ScaledGap
    If bc.mWidth < nw Or bc.mHeight < nh Then
        bc.Initialize(Max(bc.mWidth, nw), Max(bc.mHeight, nh))
    End If
    bc.DrawRect(bc.TargetRect, xui.Color_Transparent, True, 0)
    Dim r As B4XRect
    r.Initialize(ScaledArrowWidth, 1, nw, nh)
    Dim clr As Int
    If Right Then clr = 0xFFEFEFEF Else clr = 0xFFC1F7A3
    bc.DrawRectRounded(r, clr, True, 0, 10)
    Dim path As BCPath
    path.Initialize(nw - 1, 1)
    path.LineTo(nw - 1 - (10 + ScaledArrowWidth), 1)
    path.LineTo(nw - 1 - ScaledArrowWidth, 10)
    path.LineTo(nw - 1, 1)
    bc.DrawPath(path, clr, True, 0)
    bc.DrawPath(path, clr, False, 2)
    Dim b As B4XBitmap = bc.Bitmap
    Return b.Crop(0, 1, nw, nh)
End Sub

Private Sub BuildMessage (Text As String, User As String) As List
    Dim title As BCTextRun = Engine.CreateRun(User & CRLF)
    title.TextFont  = BBCodeView1.ParseData.DefaultBoldFont
    Dim TextRun As BCTextRun = Engine.CreateRun(Text & CRLF)
    Dim time As BCTextRun = Engine.CreateRun(DateTime.Time(DateTime.Now))
    time.TextFont = xui.CreateDefaultFont(10)
    time.TextColor = xui.Color_Gray
    Return Array(title, TextRun, time)
End Sub

Private Sub GetBitmap (iv As ImageView) As B4XBitmap
    #if B4J
    Return iv.GetImage
    #Else If B4A
    Return iv.Bitmap
    #End If
End Sub

Private Sub CreateImageView As B4XView
    Dim iv As ImageView
    iv.Initialize("")
    Return iv
End Sub

#if B4J
Sub lblSend_MouseClicked (EventData As MouseEvent)
    lblSend_Click
    EventData.Consume
End Sub
#end if
 

Attachments

  • Backup Chat 2020-03-14 18.16.zip
    12.3 KB · Views: 125

mangojack

Well-Known Member
Licensed User
Longtime User
It is line 61 in your BuildMessage sub that is the issue.
B4X:
title.TextFont  = BBCodeView1.ParseData.DefaultBoldFont

I have never played with the BBCodeView ... so not really up on its usage.

But ... like the other BCTextRun objects declared in this sub , cannot you also use.

B4X:
title.TextFont = xui.CreateDefaultBoldFont(10)  or other size.
 
Upvote 0

Makumbi

Well-Known Member
Licensed User
It is line 61 in your BuildMessage sub that is the issue.
B4X:
title.TextFont  = BBCodeView1.ParseData.DefaultBoldFont

I have never played with the BBCodeView ... so not really up on its usage.

But ... like the other BCTextRun objects declared in this sub , cannot you also use.

B4X:
title.TextFont = xui.CreateDefaultBoldFont(10)  or other size.
But the I inserted messages are still not displayed on the customlistview wen I reload were could be the problem
 
Upvote 0

mangojack

Well-Known Member
Licensed User
Longtime User
So your error mentioned above is solved ... ?

But the I inserted messages are still not displayed on the customlistview wen I reload were could be the problem

I have no idea ... add some logs to try and determine where you code is failing.
 
Upvote 0

Makumbi

Well-Known Member
Licensed User
So your error mentioned above is solved ... ?



I have no idea ... add some logs to try and determine where you code is failing.
Yes the first error solved after adjusting to your solution now after that another error is comming here
B4X:
Private Sub AddItem (Text As String, Right As Boolean)
    Dim p As B4XView = xui.CreatePanel("")
    Dim User As String
    If Right Then User = "User 2" Else User = "User 1"
    BBCodeView1.ExternalRuns = BuildMessage(Text, User)
    'BBCodeView1.ParseAndDraw
    
    CLV.Add(p, Null)
    ScrollToLastItem
End Sub
error comes here
B4X:
 CLV.Add(p, Null)
Below is the error messages
B4X:
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
Error occurred on line: 77 (Main)
java.lang.NullPointerException: Attempt to invoke virtual method 'int anywheresoftware.b4a.objects.collections.List.getSize()' on a null object reference
    at b4a.example3.customlistview._add(customlistview.java:71)
    at b4a.example.main._additem(main.java:461)
    at b4a.example.main._activity_resume(main.java:430)
    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:144)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:176)
    at b4a.example.main.afterFirstLayout(main.java:110)
    at b4a.example.main.access$000(main.java:17)
    at b4a.example.main$WaitForLayout.run(main.java:82)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6669)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
 
Upvote 0

mangojack

Well-Known Member
Licensed User
Longtime User
CLV.Add method requires 3 parameters ... why are you passing NULL ?

Capture.PNG
 
Upvote 0
Top