XML nil Issue

Ok I am missing something somewhere and stuck. I have an app i’m building for a client that on one of the pages pulls data from an online XML file. The file gets saved to the tmp directory, then parsed. Everything worked wonderful until I tired to add a new bit of data. When the parser hits a child that is empty it throws a nil error like so…

Runtime error

    …ilE/Dropbox/Royal Bliss Mobile - Final/ios7/tour.lua:184: attempt to index field ‘?’ (a nil value)

After hitting the nil child the whole thing crashes. I tried a IF statement like so…

[lua]

for i=1,#event do

local eventVenue = event[i].child[14].child[1].value

local eventCity = event[i].child[5].value

local eventOpener = event[i].child[13].child[2].child[1].value

if (eventOpener == nil) then
eventOpener = “None Specified In Database”
end

[/lua]

this line…

[lua]local eventOpener = event[i].child[13].child[2].child[1].value[/lua]

Is the child that does not always have data in it. I know the problem is with my IF statement and not understanding how to get past the nil value, I’m just not sure of what to try next.

Thanks in advance for any help anyone can give on this :slight_smile:

First off, encase your code in flat brackets rather than using the horrible code button. I really don’t know why that’s not fixable for the web guys here, but there it is. <lua> </lua>, but with square brackets.

The basic problem is that you’re reading so deep that the table itself does not exist. It’s fine to say:

[lua]local myTable = {}

myTable.newvalue = 3[/lua]

Because the table is initialized. But in your sample, you’re trying to say that child[2] has it’s own subtable named child[1], and that child[1] is *also* a table with a .value assigned. 

[lua] – so we’re pretty sure that child[2] exists. but child[1]? who knows?

– local eventOpener = event[i].child[13].child[2].child[1].value

– Instead, let’s make sure it exists

if event[i].child[13].child[2].child[1] then

  event[i].child[13].child[2].child[1].value = 32

else – if you absolutely want a table everytime,

  if not event[i].child[13].child[2].child[1] then 

    event[i].child[13].child[2].child[1] = {}

  end

end[/lua]

The runtime error doesn’t specify which value failed though, so if the last “child” is not found you’ll still have a crash. The basic gist is simply that you’re trying to assign a table value where the table does not exist.

THANK YOU!!!

It did not work exactly, but gave me enough to go on to make it work :slight_smile:

I am face a similar problem. However, all I want is to read data (not set). In my case, because sometimes the last “child” does not exist I get the crash. Is there a way to check if it exists without the crash? I tried to compare it with a nil value but, no matter what, it still crashes…

To read nested data like that, you’ll need to check each step along the way; i.e.:

[lua]if myTable[1][2][3][4] then

  if myTable[1][2][3][4][5] then

    return true

  end

end[/lua]

You can’t check for [5] if [4] doesn’t exist. So for simpler tables where you know for sure that parts of it exist, just add a check first.

[lua]if myBook.chapter[14] then

  if myBook.chapter[14].page[9] then

   – do something

  end

end[/lua]

If you’re not sure of the entire structure then you would want to craft a recursive lua function that keeps calling itself until it finds something that isn’t of type() == “table”.

First off, encase your code in flat brackets rather than using the horrible code button. I really don’t know why that’s not fixable for the web guys here, but there it is. <lua> </lua>, but with square brackets.

The basic problem is that you’re reading so deep that the table itself does not exist. It’s fine to say:

[lua]local myTable = {}

myTable.newvalue = 3[/lua]

Because the table is initialized. But in your sample, you’re trying to say that child[2] has it’s own subtable named child[1], and that child[1] is *also* a table with a .value assigned. 

[lua] – so we’re pretty sure that child[2] exists. but child[1]? who knows?

– local eventOpener = event[i].child[13].child[2].child[1].value

– Instead, let’s make sure it exists

if event[i].child[13].child[2].child[1] then

  event[i].child[13].child[2].child[1].value = 32

else – if you absolutely want a table everytime,

  if not event[i].child[13].child[2].child[1] then 

    event[i].child[13].child[2].child[1] = {}

  end

end[/lua]

The runtime error doesn’t specify which value failed though, so if the last “child” is not found you’ll still have a crash. The basic gist is simply that you’re trying to assign a table value where the table does not exist.

THANK YOU!!!

It did not work exactly, but gave me enough to go on to make it work :slight_smile:

I am face a similar problem. However, all I want is to read data (not set). In my case, because sometimes the last “child” does not exist I get the crash. Is there a way to check if it exists without the crash? I tried to compare it with a nil value but, no matter what, it still crashes…

To read nested data like that, you’ll need to check each step along the way; i.e.:

[lua]if myTable[1][2][3][4] then

  if myTable[1][2][3][4][5] then

    return true

  end

end[/lua]

You can’t check for [5] if [4] doesn’t exist. So for simpler tables where you know for sure that parts of it exist, just add a check first.

[lua]if myBook.chapter[14] then

  if myBook.chapter[14].page[9] then

   – do something

  end

end[/lua]

If you’re not sure of the entire structure then you would want to craft a recursive lua function that keeps calling itself until it finds something that isn’t of type() == “table”.