Android Question 2nd time through: failure

67biscuits

Member
I am having difficulty understanding why, after searching through helps and tutorials for an while, when this code runs through with an empty file, it is all right, but when the code is refreshed and I run the sub for a second time it throws an error.

B4X:
Private Sub cmdTester_Click
    stats.clear
End Sub

Sub historical(i As String)
    ' historical setup: (item, first date, last date, buys, favourite)
    Private HS As histoire
    DateTime.DateFormat = "E, d  MMM"
    HS.Initialize(i,DateTime.Now,DateTime.Now,1,False)
    stats.Add(HS)

Log(stats.Size)

For Each sh As histoire In stats
    Log(sh.getName)
Next

    Log(HS.getFavoured)
    Get_or_Save_TF(False)
End Sub

Also, if in the For Each I try to access the sh.Getname in a If statement, (If sh.getName = i Then ...). it throws the same error

Error:
** Service (starter) Destroy (ignored)**
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create (first time) **
** Activity (main) Resume **
25
Error occurred on line: 335 (Main)
java.lang.RuntimeException: Field: ba not found in: java.lang.String
at anywheresoftware.b4a.shell.Shell$FieldCache.getField(Shell.java:923)
at anywheresoftware.b4a.shell.Shell.getField(Shell.java:697)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:360)
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.debug.Debug.delegate(Debug.java:262)
at b4a.grocereaze.main._historical(main.java:776)
at b4a.grocereaze.main._clvgroceries_itemclick(main.java:713)
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:351)
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.keywords.Common.CallSub4(Common.java:1114)
at anywheresoftware.b4a.keywords.Common.CallSubNew3(Common.java:1077)
at b4a.example3.customlistview$ResumableSub_PanelClickHandler.resume(customlistview.java:805)
at b4a.example3.customlistview._panelclickhandler(customlistview.java:748)
at b4a.example3.customlistview._panel_click(customlistview.java:735)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:221)
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:7701)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:610)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:952)
** Activity (main) Pause, UserClosed = true **
** Service (starter) Destroy (ignored)**


Thanks for any help or light shed.
 

emexes

Expert
Licensed User
Longtime User
B4X:
' historical setup: (item, first date, last date, buys, favourite)
Private HS As histoire
DateTime.DateFormat = "E, d  MMM"
HS.Initialize(i,DateTime.Now,DateTime.Now,1,False)

Is histoire a Class or a Type?

Given the parameters to .Initialize, it seems to be a Class.

So maybe the error is in the Class file.

Also, what type are the First Date and Last Date field variables? (and why do those field variable names have spaces in them?)

You are .Initialize'ing them with Longs (from DateTime.Now) but the nearby DateTime.DateFormat suggests they might be strings.[/Code][/QUOTE]
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
but the nearby DateTime.DateFormat suggests they might be strings
not really. he set a format, yes. But he does not give a formatted date to the type/class. He is giving LONG-Values.

So the format is used in the class i guess to output the date formatted
 
Upvote 0

67biscuits

Member
histoire WAS a class, but I got so frustrated with it not working I eventually eliminate that, tried TYPE'ing it (can't remember that issue) but am now using a Map, infinitely more easy! Even with that I had an issue as writemap saves a string. not a list, as I require. I have a sub to convert user entered csv string to a list so I employed that to fix that wee issue.
But as for the date question, the date format was just for output for calling the getDate sub from the class, the dates were (are) saved as long for more manipulation later on. Calculated duration, adding and subtracting from dates, first dates, present dates and future dates etc. Attempting to educate an old app i wrote many moons ago to predict WHEN to to add favourites to a grocery list.

What is the output of Log(stats), before it crashes?
Oddly, maybe you can shed light on this, the stats.size output was [stats.size*4]. I did not request the factoring. I could not quite make heads nor tails of the output. Maybe all the data that builds the class...?

Admittedly I am thrashing around blindly. Its been almost 30 years since I VB'd, and that was mostly front end stuff for MS office, excel, DB queries etc. But even then I was more the BASICA whizzer at the time, between punch cards, sys36, fortran, cobol and other medievel languages. I am dabbling again after a long diversion in carpentry

I am trying to learn about classes etc, but its difficult, I am tactile learner so need to type code and see to learn.

cheers, lads.

(Im off to work now)

Charley
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
ChatGPT 4o says...

Your error message suggests that the issue is with an attempt to access a field (ba) that doesn't exist in java.lang.String. The most likely cause is that somewhere in your code, an histoire object is expected, but a String is actually being stored or accessed incorrectly.

Potential Issues and Fixes​

  1. Verify that stats only contains histoire objects
    You are adding HS (which is an histoire object) to stats, but something might be corrupting the list. Ensure that stats is properly declared and only holds histoire objects.
    • How is stats declared? It should be:
      B4X:
      Private stats As List
      stats.Initialize

    • Add this check before iterating over stats:
      B4X:
      For Each sh As Object In stats
          Log(sh) ' Debugging: Log what is in stats
          If sh Is histoire Then
              Dim hist As histoire = sh
              Log(hist.getName)
          Else
              Log("Unexpected type: " & GetType(sh))
          End If
      Next
      This will help identify if anything other than histoire is getting stored in stats.
  2. Ensure histoire is a properly defined class
    If histoire is a custom type (class or type in B4X), confirm that it has a proper Initialize method. If HS.getFavoured or sh.getName is causing issues, make sure these methods/properties exist and return the expected types.
    Example:
    B4X:
    Sub Class_Globals
        Private Name As String
        Private FirstDate As Long
        Private LastDate As Long
        Private Buys As Int
        Private Favourite As Boolean
    End Sub
    
    Public Sub Initialize(i As String, firstD As Long, lastD As Long, buys As Int, fav As Boolean)
        Name = i
        FirstDate = firstD
        LastDate = lastD
        Buys = buys
        Favourite = fav
    End Sub
    
    Public Sub getName As String
        Return Name
    End Sub
    
    Public Sub getFavoured As Boolean
        Return Favourite
    End Sub
    If histoire does not have an explicit Initialize method, it might not properly instantiate.
  3. Clearing stats before reuse
    When calling cmdTester_Click, stats.clear is executed. However, if stats isn't re-initialized properly before adding new elements, this could cause unexpected behavior.
    Instead of just stats.Clear, try:
    B4X:
    stats.Initialize
  4. Check for accidental assignment of a String to stats
    • If somehow stats.Add is being called with a String instead of an histoire object, it could cause the error.
    • Add this debug line after inserting into stats:
      B4X:
      Log("Added to stats: " & GetType(HS))

Summary of Fixes:​

  • Ensure stats is initialized before use.
  • Debug what’s actually being stored in stats (Log(GetType(sh)) inside the loop).
  • Ensure histoire is properly structured and has an Initialize method.
  • Double-check that HS is always an histoire object before adding it to stats.
 
Upvote 0

67biscuits

Member
I think where my confusion lies is the instantiating an object and then referring to it later. I may be thinking to dim anObject as the class itself as opposed to the an instance of the class. And in saying that, I realize that the more I know the more I see how little I know. I have to make a close study of the classes/types/modules and truly
understand. I am playing with MAP'ing for now and it is surprisingly easy, besides the list converts to string factor when it "File.Writemap".
In your example where you " For Each sh As Object In stats ", I thought I tried that but just created different error msgs. I need to pay attention to where and when lists/arrays/classes etc get intialized, that shall in itself fix many woes, methinks.
That foray was a flurry of bewilderment on my part. I shall pore over your tutorial with diligent enthusiasm, JohnC. Thank you

Charley
 
Upvote 0
Top