Bad argument #1 to 'newSprite' (ImageSheet expected, got nil)

I have this annoying error where this message appears during game. My game has selectable characters which I store in a variable called ‘currentBird’. Now I can change characters in a characterSelect.lua page and the data stores well and runs perfect. However the default character (for first play through) doesn’t seem to store inside the database and thus I get the error.

I’ve added some screenshots of my database values and shows one storing the data fine and the other showing a 0

local storyboard = require "storyboard" local M = {} local sqlite3 = require("sqlite3") M.currentBird = "images/birdGreenAnim.png" -- default character if none purchased/first play local function setupDatabaseB() local dbPath = system.pathForFile("birdData.db3", system.DocumentsDirectory) local db = sqlite3.open( dbPath ) local tablesetupB = [[CREATE TABLE player (id INTEGER PRIMARY KEY, currentBird); INSERT INTO player VALUES (NULL, '0');]] db:exec( tablesetupB ) db:close() end setupDatabaseB() M.loadInfoB = function() local dbPath = system.pathForFile("birdData.db3", system.DocumentsDirectory) local db = sqlite3.open(dbPath) for row in db:nrows("SELECT \* FROM player WHERE id = 1") do M.currentBird = tostring(row.currentBird) end db:close() end M.saveInfoB = function() local dbPath = system.pathForFile("birdData.db3", system.DocumentsDirectory) local db = sqlite3.open(dbPath) local update = "UPDATE player SET currentBird='" .. M.currentBird .."' WHERE id=1" db:exec(update) db:close() end return M

Then in menu.lua I attempt to call the value of currentBird when creating my sprite sheet. 

local storyboard = require( "storyboard" ) local scene = storyboard.newScene() local utils = require("helpers.globals") sheet1 = graphics.newImageSheet(utils.currentBird, {width=45, height=35, numFrames=2, sheetContentWidth=90, sheetContentHeight=35}) function scene:createScene( event ) local group = self.view utils.loadInfoB() bird = display.newSprite(sheet1 ) (positioning stuff here)

The game loads up fine but if I go from one scene and then back to menu, i get the error message. Ive checked the database and its showing a ‘0’ value. However on the character select screen, I can change the player and the database will show “images/birdBlueAnim.png” and the game runs perfectly.

charSelect.lua 

local storyboard = require( "storyboard" ) local scene = storyboard.newScene() local utils = require("helpers.globals") function buttonPressed2(event) local t = event.target local id = t.id if utils.gems \>= t.cost then local function alertPressed(event) if event.index == 1 then utils.gems = utils.gems - t.cost utils.currentBird = t.location utils.saveInfoB() end end local alert = native.showAlert("Alert", "Would you like to buy "..t.id.." for "..t.cost.." gems?", {"OK", "Cancel"}, alertPressed) else local alert = native.showAlert("Alert", "We're sorry, but you don't have enough gems to buy "..t.id.." ...", {"OK"}) end end function scene:createScene( event ) local group = self.view blueBird = display.newImageRect("images/char1Profile.png", 168,73) blueBird.anchorX = 0.5 blueBird.anchorY = 0.5 blueBird.id = "Blue bird" blueBird.location = "images/birdBlueAnim.png" blueBird.cost = 5 end

I am at a lost right now and would love any help. Thanks 

I have some screens for the database values:  

Hi Dipesh,

In your setupDatabaseB() function your doing this:

INSERT INTO player VALUES (NULL, ‘0’);

That is setting the “currentBird” value to 0. If you changed that to the image path like this:

INSERT INTO player VALUES (NULL, ‘images/birdGreenAnim.png’);

It should fix your problem. The 0 your seeing is the initial value you set the database up with. Let me know if the above helps! 

Best Regards,

Jamie Trinder

Ah yes! that makes sense. I totally forgot about that and just pasted it from highscore database. Thank you so much!

Dip,

 Just thought it might help to mention, that before you get to far along in your project, you might want to use Composer instead of Storyboard. Composer is the newer and supported version of storyboard.  Storyboard apparently still works and such, but you might be better in the long run converting to composer if you are not to far along in the project.

Good Luck

Hi Dipesh,

In your setupDatabaseB() function your doing this:

INSERT INTO player VALUES (NULL, ‘0’);

That is setting the “currentBird” value to 0. If you changed that to the image path like this:

INSERT INTO player VALUES (NULL, ‘images/birdGreenAnim.png’);

It should fix your problem. The 0 your seeing is the initial value you set the database up with. Let me know if the above helps! 

Best Regards,

Jamie Trinder

Ah yes! that makes sense. I totally forgot about that and just pasted it from highscore database. Thank you so much!

Dip,

 Just thought it might help to mention, that before you get to far along in your project, you might want to use Composer instead of Storyboard. Composer is the newer and supported version of storyboard.  Storyboard apparently still works and such, but you might be better in the long run converting to composer if you are not to far along in the project.

Good Luck