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:

LogCat connected to: 0123456789ABCDEF
--------- beginning of /dev/log/system


--------- beginning of /dev/log/main


** Activity (main) Create, isFirst = true **


** Activity (main) Resume **


XOMDocument successfully created

<result>
<year>2013</year>
<date>19th January 2013</date>
<compName>Butlins Mineworkers</compName>
<section>Third</section>
<bandName>Heyl Town</bandName>
<position>11</position>
<conductor>Stuart Chappell</conductor>
</result>
<result>
<year>2013</year>
<date>19th January 2013</date>
<compName>Butlins Mineworkers</compName>
<section>Third</section>
<bandName>Hemel Hempstead</bandName>
<position>W</position>
</result>

(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
Top