XML reading as Integer instead of String

bishmedia

Member
Licensed User
Longtime User
Im reading from an XML file which has a record called 'position' and has an input like
1
2
3
4
5
6
7
W
The 'W' means withdrawn but when the file is read it throws an error because i think its reading it as an integer instead of string, i'm getting...

An error has occured in sub:main_buildlistview(java line:305) java.lang.RuntimeException: Object should first be initialized (XOMElement)

How can i make sure ResultElement.GetFirstChildElementByName("position").value is read as a string value?????

B4X:
Sub BuildListView
   Dim i As Int
   Dim ListTitle As String
   Dim ResultElement As XOMElement
   Dim yearString As String
   Dim competitionString As String
   Dim sectionString As String
   
   yearString = "2013"
   competitionString = "Butlins Mineworkers"
   sectionString = "Third"
   
   For i=0 To ResultElements.Size-1   
      ResultElement=ResultElements.GetElement(i)
      If ResultElement.GetFirstChildElementByName("compName").value = competitionString _
      AND ResultElement.GetFirstChildElementByName("year").value = yearString _
      AND ResultElement.GetFirstChildElementByName("section").value = sectionString Then
         Table1.AddRow(Array As String(ResultElement.GetFirstChildElementByName("position").value, _
         ResultElement.GetFirstChildElementByName("bandName").value, _
         ResultElement.GetFirstChildElementByName("conductor").value))
      End If
   Next
   
End Sub
 

warwound

Expert
Licensed User
Longtime User
The problem is that at least one of your XML result elements has no conductor child element.

I modified your code:

B4X:
Sub BuildListView
   Dim i As Int
   Dim ListTitle As String
   Dim ResultElement As XOMElement
   Dim yearString As String
   Dim competitionString As String
   Dim sectionString As String
   
   yearString = "2013"
   competitionString = "Butlins Mineworkers"
   sectionString = "Third"
   
   For i=0 To ResultElements.Size-1   
      ResultElement=ResultElements.GetElement(i)
      If ResultElement.GetFirstChildElementByName("compName").value = competitionString _
      AND ResultElement.GetFirstChildElementByName("year").value = yearString _
      AND ResultElement.GetFirstChildElementByName("section").value = sectionString Then
         
         Log(ResultElement.ToXML)
      
         Table1.AddRow(Array As String(ResultElement.GetFirstChildElementByName("position").value, _
         ResultElement.GetFirstChildElementByName("bandName").value, _
         ResultElement.GetFirstChildElementByName("conductor").value))
         
      End If
   Next
   
End Sub

And the log shows:


(I removed some log statements to avoid cluterring this post).

So the exception is caused by ResultElement.GetFirstChildElementByName("conductor").value, no such element exists and so the XOMElement is not initialized.

Martin.
 
Upvote 0

bishmedia

Member
Licensed User
Longtime User
Thankyou so much, the 'conductor' record was blank because the band was withdrawn. I have added 'N/A' to the XML file in each case and it now works fine.

Thankyou for helping, i can stop banging my head against the wall nmow :sign0060:
 
Upvote 0