Check table entry is nil...

Hi!

I have a table that is supose to be empty first. When I add stuff to it it should look like this for example:

table = { {startX = 1, startY = 3, hasDot = true} {startX = 2, startY = 2, hasDot = true} }   --[[so   table[2].hasDot == false --]]  

This is what I did:

1. lines = {} -- I have this. 2. 3.  4. 5.    if(lines[id] == nil) then 6.     lines[id].startX = grp.parent.x + x1 7.    lines[id].startY = grp.parent.y + height\_s - y1 8.    lines[id].hasDot = true

Line 6 here is says “attempt to index field ‘?’ (a nil value)”

And I guess line 7 and 8 are wrong too.  

How can I add to the table to look like the example above?

I thought this had an obvious answer but I didn’t find anything on google:S

Regards!

check it with table[i]~=nil should work for example:

if(table[1]~=nil)then print("initiated") else --handle it end  

I’m sorry I edited the post while you wrote. But thank you it works better now though the problem now is I cant add stuff to the table so its a table with tables like the example above.

Hi!

I have a table that is supose to be empty first. When I add stuff to it it should look like this for example:

table = { {startX = 1, startY = 3, hasDot = true} {startX = 2, startY = 2, hasDot = true} }   --[[so   table[2].hasDot == false --]]  

This is what I did:

  1. lines = {} – I have this. 2. 3.  4. 5.    if(lines[id] == nil) then 6.     lines[id].startX = grp.parent.x + x1 7.    lines[id].startY = grp.parent.y + height_s - y1 8.    lines[id].hasDot = true

Line 6 here is says “attempt to index field ‘?’ (a nil value)”

And I guess line 7 and 8 are wrong too.  

How can I add to the table to look like the example above?

I thought this had an obvious answer but I didn’t find anything on google:S

Regards!

do something like:

if(lines[id] == nil) then lines[id]={startX = grp.parent.x + x1 ,startY = grp.parent.y + height\_s - y1 ,hasDot = true} end

Ah, of course! Thats how you add a table to a table. Thank you!

Hi!

I have a table that is supose to be empty first. When I add stuff to it it should look like this for example:

table = { {startX = 1, startY = 3, hasDot = true} {startX = 2, startY = 2, hasDot = true} }   --[[so   table[2].hasDot == false --]]  

This is what I did:

  1. lines = {} – I have this. 2. 3.  4. 5.    if(lines[id] == nil) then 6.     lines[id].startX = grp.parent.x + x1 7.    lines[id].startY = grp.parent.y + height_s - y1 8.    lines[id].hasDot = true

Line 6 here is says “attempt to index field ‘?’ (a nil value)”

And I guess line 7 and 8 are wrong too.  

How can I add to the table to look like the example above?

I thought this had an obvious answer but I didn’t find anything on google:S

Regards!

Just for clarity – You couldn’t set the fields startX, startY etc because the table element lines[id] did not exist yet (was nil).

Adding the statement lines[id] = {} after your nil test (line 5) would allow the rest of the code to work as written.

Without it, the .startX .startY references would be trying to access a non-existent table element (which is different than one that exists, but is empty, as created with the lines[id] = {} statement).

check it with table[i]~=nil should work for example:

if(table[1]~=nil)then print("initiated") else --handle it end  

I’m sorry I edited the post while you wrote. But thank you it works better now though the problem now is I cant add stuff to the table so its a table with tables like the example above.

Hi!

I have a table that is supose to be empty first. When I add stuff to it it should look like this for example:

table = { {startX = 1, startY = 3, hasDot = true} {startX = 2, startY = 2, hasDot = true} }   --[[so   table[2].hasDot == false --]]  

This is what I did:

  1. lines = {} – I have this. 2. 3.  4. 5.    if(lines[id] == nil) then 6.     lines[id].startX = grp.parent.x + x1 7.    lines[id].startY = grp.parent.y + height_s - y1 8.    lines[id].hasDot = true

Line 6 here is says “attempt to index field ‘?’ (a nil value)”

And I guess line 7 and 8 are wrong too.  

How can I add to the table to look like the example above?

I thought this had an obvious answer but I didn’t find anything on google:S

Regards!

do something like:

if(lines[id] == nil) then lines[id]={startX = grp.parent.x + x1 ,startY = grp.parent.y + height\_s - y1 ,hasDot = true} end

Ah, of course! Thats how you add a table to a table. Thank you!

Hi!

I have a table that is supose to be empty first. When I add stuff to it it should look like this for example:

table = { {startX = 1, startY = 3, hasDot = true} {startX = 2, startY = 2, hasDot = true} }   --[[so   table[2].hasDot == false --]]  

This is what I did:

  1. lines = {} – I have this. 2. 3.  4. 5.    if(lines[id] == nil) then 6.     lines[id].startX = grp.parent.x + x1 7.    lines[id].startY = grp.parent.y + height_s - y1 8.    lines[id].hasDot = true

Line 6 here is says “attempt to index field ‘?’ (a nil value)”

And I guess line 7 and 8 are wrong too.  

How can I add to the table to look like the example above?

I thought this had an obvious answer but I didn’t find anything on google:S

Regards!

Just for clarity – You couldn’t set the fields startX, startY etc because the table element lines[id] did not exist yet (was nil).

Adding the statement lines[id] = {} after your nil test (line 5) would allow the rest of the code to work as written.

Without it, the .startX .startY references would be trying to access a non-existent table element (which is different than one that exists, but is empty, as created with the lines[id] = {} statement).