Corona Runtime Error “attempt to concatenate field '?' (a nil value)”

匿名 (未验证) 提交于 2019-12-03 08:59:04

问题:

i have this 8 question survey in my mobile application handover project. At first the corona did not prompt me any error message and the app works just fine but when i add 2 more question to it i start getting this message error

i am not sure why the error is "a nil value" but my code look something like this.(line 662 to 678)

        function  checkEBASComplete()         local tempScore = 0         for i = 1, 10 do             print("EBAS:"..ebasRating_Arr[i])             tempScore = tempScore + ebasRating_Arr[i]              if (ebasRating_Arr[i] == -1) then                 ebasScore = 0                 ebasScore_text.text = "Test Incomplete"             else                 ebasScore = tempScore                 ebasScore_text.text = tostring(ebasScore)             end          end          tempScore = 0     end      checkEBASComplete() 

and i have something like this at line 110. i just add 2 more "-1" behide

 local ebasRating_Arr = {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1} 

Is anyone able to help me with this? Appreciate for all helps :D

NEW QUESTION------------------------------------------------------

this is my code from line 705 - 711

function saveResults()          local q = [[UPDATE EBAS_DEP SET rating1=']]..ebasRating_Arr[1] .. [[',rating2=']] .. ebasRating_Arr[2] .. [[',rating3=']] .. ebasRating_Arr[3] .. [[',rating4=']] .. ebasRating_Arr[4] .. [[',rating5=']] .. ebasRating_Arr[5] .. [[',rating6=']] .. ebasRating_Arr[6] .. [[',rating7=']] .. ebasRating_Arr[7] .. [[',rating8=']] .. ebasRating_Arr[8] .. [[',rating9=']] .. ebasRating_Arr[9] .. [[',rating10=']] .. ebasRating_Arr[10] .. [[',rating11=']] .. amtRating_Arr[1] .. [[',rating12=']] .. amtRating_Arr[2] .. [[',rating13=']] .. amtRating_Arr[3] .. [[',rating14=']] .. amtRating_Arr[4] .. [[',rating15=']] .. amtRating_Arr[5] .. [[',rating16=']] .. amtRating_Arr[6] .. [[',rating17=']] .. amtRating_Arr[7] .. [[',rating18=']] .. amtRating_Arr[8] .. [[',rating19=']] .. amtRating_Arr[9] .. [[',rating20=']] .. amtRating_Arr[10] .. [[',rating21=']] .. amtRating_Arr[11] .. [[',ebas_score=']] .. ebasScore ..[[',amt_score=']] .. amtScore ..  [['WHERE id=']].. _G.EBAS_ID..[[';]]         db:exec( q )         print(db:errcode(), db:errmsg())      end 

回答1:

The error occurs when your ebasRating_Arr is too short for the for-loop inside the checkEBASComplete

I would recommend changing the fixed for-loop from 1 to 10 to a relative for-loop 1 to the end of the array. This would work with the #array (or table in Lua) operator. Your code would look like this:

function checkEBASComplete()     local tempScore = 0     for i = 1, #ebasRating_Arr do       -- Changed to relative for-loop         print("EBAS:"..ebasRating_Arr[i])         tempScore = tempScore + ebasRating_Arr[i]         if (ebasRating_Arr[i] == -1) then             ebasScore = 0             ebasScore_text.text = "Test Incomplete"         else             ebasScore = tempScore             ebasScore_text.text = tostring(ebasScore)         end     end     tempScore = 0 end 


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!