Error regarding json.lua, only on Device and not on Simulator

I am getting a strange error regarding running my app on a device. I am developing my app for android only

The app works fine on Simulator but throws up the following error on an android device:-

/Users/jenkins/slaveroot/workspace/Templates/label/android/platform/resources/json.lua:26:json.decode called with nil string

I have stripped down my app to just Main.lua with only a image and the following code. This is the piece of code that is causing this error.

display.setStatusBar( display.HiddenStatusBar ) -- require the composer library local composer = require "composer" -- load scene1 local utility = require("lib.utility") -- Add any objects that should appear on all scenes below (e.g. tab bar, hud, etc) local user = utility.loadTable("user.json") if user == nil then user = {} user.lives = 5 user.money = 50 user.currentlevel = 1 user.levels = {} user.levels[1] = {} user.levels[1].firsttime = true user.levels[1].tut = false user.levels[1].locked = false user.levels[1].score = 0 user.levels[1].star = 0 ------------------1 is locked, 2 is open, 3 is 1 star, 4 is 2 star and 5 is 3 star------------- for i=2,10 do user.levels[i] = {} user.levels[i].firsttime = true user.levels[i].locked = true user.levels[i].score = 0 user.levels[i].star = 0 end user.dipper = 1 user.moremoves = 5 user.bee = 5 user.hand = 5 user.hammer = 5 user.musictoggle = 1 ---------o is off and 1 is on----------------- user.soundtoggle = 1 ---------o is off and 1 is on----------------- --user.isPaid = false user.unlockedLevels = 20 user.bestScore = 0 user.firsttime = 0 utility.saveTable(user, "user.json") utility.loadTable(user, "user.json") end print("money: "..user.money) composer.gotoScene( "title")

I am at my wit’s end. What do I do?

P.s utility is the external library that deals with loading and saving json files.

Any time I’ve seen an error like that, it was because I had the actual file name capitalised, but not capitalised when I tried to use it in code. Or vice-versa. Other than that, I don’t know.

Thank you so much for your reply.

I also think it’s something like this. I think I’ll just have to write the code fresh - maybe that will solve the problem.

Is there more to the error in the console log?

Looking at your code, line 47 seems to not be doing anything. You’re loading the file, but since you don’t assign the return data to any value so it’s not doing you any good.

The error would only happen in the middle of the loadTable() call when it tries to decode the string read in from the file. If the file doesn’t exist, and the code isn’t testing to see if the file IO failed, then it’s possible you’re passing an empty/nil string to the decode call.

Rob

@Rob: Thanks for your reply and am so sorry for not getting back sooner. I also think that line 47 was the problem. Don’t know why the simulator didn’t show any error. Anyways, I just rewrote the whole section from scratch without trying to fix the old code and it worked. :slight_smile:

Any time I’ve seen an error like that, it was because I had the actual file name capitalised, but not capitalised when I tried to use it in code. Or vice-versa. Other than that, I don’t know.

Thank you so much for your reply.

I also think it’s something like this. I think I’ll just have to write the code fresh - maybe that will solve the problem.

Is there more to the error in the console log?

Looking at your code, line 47 seems to not be doing anything. You’re loading the file, but since you don’t assign the return data to any value so it’s not doing you any good.

The error would only happen in the middle of the loadTable() call when it tries to decode the string read in from the file. If the file doesn’t exist, and the code isn’t testing to see if the file IO failed, then it’s possible you’re passing an empty/nil string to the decode call.

Rob

@Rob: Thanks for your reply and am so sorry for not getting back sooner. I also think that line 47 was the problem. Don’t know why the simulator didn’t show any error. Anyways, I just rewrote the whole section from scratch without trying to fix the old code and it worked. :slight_smile: