List in a list

JDS

Active Member
Licensed User
Longtime User
What i'm trying to do is to read an XML-file with orders into a list. Every order kan contain multiple orderlines.
For this i'm using the XML-parser and the following code.

B4X:
Type Tregel (regel_id As String, _
      aantal As Int, _
        eenheid As String)

Type TZending (zendingid As String, _
        vrachtnr As String, _
        barcode As String, _
        A_naam As String, _
        Zendingregels As List)

Dim arZendingen As List

Sub Process_Globals
    Dim fZending As TZending
    Dim fregel As Tregel
    Dim iTeller As Int
End Sub

Sub PDA_EndElement (Uri As String, Name As String, Text As StringBuilder)
    If Name = "z01" Then
        fZending.BestelLijst = Text.ToString
    End If
    If Name = "z03" Then
   fZending.zendingid = Text.ToString
    End If
    If Name = "z04" Then
   fZending.vrachtnr = Text.ToString
    End If
{and more}
....

    If Name = "r01" Then
        fregel.regel_id = Text.ToString
    End If
    If Name = "r02" Then
   fregel.Aantal = Text.ToString
    End If
    If Name = "r03" Then
          fregel.eenheid_id = Text.ToString
    End If
{and more "r"}
....
    If Name = "r" Then 'if XML-tag = "r" then add fregel to fZending

'*************
   fZending.Zendingregels.Add(fregel) 'The programm gives an error on this line.
'*************
    End If
    
     If (Name = "z") Then 'zending toevoegen
   Main.arZendingen.Add(fZending)
     End If
End Sub

When i execute the last three lines of code i get the following error:
"An error has occurred in sub: lm_ophalenversturen_pda_endelement (java line: 791) java.lang.ClassCastException: lm.mobile.android.main$_tregel Continue?"

At the end i try to add the complete fZending, with (multiple) fregel, to a list ArZendingen. If i remove the line "fZending.Zendingregels.Add(fregel)", it adds the fZending to arZendingen.

Can someone give me a direction?
 
Last edited:

JDS

Active Member
Licensed User
Longtime User
I have not given the complete Type-declaration, its quite long but bestellijst does exist.

I've marked the problematic line.
 
Upvote 0

JDS

Active Member
Licensed User
Longtime User
Hi Erel,

Thanks for your fast replies.

I've added e code-line
B4X:
log(####)
to determine where the error begins.
The next lines follow in the log:

#####
lm_ophalenversturen_pda_endelement (java line: 725)

java.lang.NullPointerException at lm.mobile.android.lm_ophalenversturen._pda_endelement(lm_ophalenversturen.java:725)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:170)
at anywheresoftware.b4a.objects.SaxParser$MyHandler.endElement(SaxParser.java:118)
at org.apache.harmony.xml.ExpatParser.endElement(ExpatParser.java:158)
at org.apache.harmony.xml.ExpatParser.appendBytes(Native Method)
at org.apache.harmony.xml.ExpatParser.parseFragment(ExpatParser.java:518)
at org.apache.harmony.xml.ExpatParser.parseDocument(ExpatParser.java:479)
at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:318)
at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:275)
at anywheresoftware.b4a.objects.SaxParser.parse(SaxParser.java:78)
at anywheresoftware.b4a.objects.SaxParser.Parse(SaxParser.java:71)
at lm.mobile.android.globaal._route_verwerken(globaal.java:383)
at lm.mobile.android.lm_ophalenversturen._jobdone(lm_ophalenversturen.java:377)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:170)
at anywheresoftware.b4a.keywords.Common$4.run(Common.java:881)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
java.lang.NullPointerException
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
but bestellijst does exist.

You have fZending dimmed as TZending. TZending does not include bestellijst.
Another interesting thing is that you don't get a initialization error for fZending.Zendingregels. Where is it? Or perhaps you have a dual definition somewhere, thus leading to the cast error? Only posting your code would help or present the exact debugging information plus the variables, with logging.
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
The nullPointerExc can be due to not initializing the Zendingregels list.
 
Upvote 0

JDS

Active Member
Licensed User
Longtime User
You guys where totally right. I hadn't initialized the list(s). After doing this all went well untill...

B4X:
Sub test
    Dim i As Int
    For i = 0 To arZendingen.Size-1
   fZending= arZendingen.Get(i)
   Log(fZending.ZendingID)
    Next
End Sub

With the above code I try to read the list arZendingen. Except fZending.ZendingID is the same number every time. I'm 100% sure there should be different numbers in the XML.
Filling the array i used:

B4X:
Main.arZendingen.Add(fZending)

It looks like the last add-action replaces all values in arZendingen with the values of fZending.

Is this again something stupid like before?
 
Last edited:
Upvote 0
Top