java - AndEngine: Getting a NullPointerException on UpdateThread when adding a Sprite -
getting nullpointerexception on updatethread when adding sprite
i'm creating first game in andengine , trying add sprite gamescene class. i've been tinkering hours application kept crashing after splash screen, supposed load gamescene afterwards. i've narrowed down these of code in drawpanel() method:
selectpanel[0] = new sprite(0, 0, resourcemanager.balltextureregionarray[0], vbom) { @override protected void predraw(glstate pglstate, camera pcamera) { super.predraw(pglstate, pcamera); pglstate.enabledither(); } }; selectpanel[0].setposition(240, 400); attachchild(selectpanel[0]);
here resourcemanager class loaded ball texture:
balltexture = new bitmaptextureatlas(act.gettexturemanager(), 64, 576); balltextureregionarray[0] = bitmaptextureatlastextureregionfactory .createfromasset(balltexture, act, "red_ball.png", 0, 0); balltextureregionarray[1] = bitmaptextureatlastextureregionfactory .createfromasset(balltexture, act, "blue_ball.png", 0, 64); balltextureregionarray[2] = bitmaptextureatlastextureregionfactory .createfromasset(balltexture, act, "green_ball.png", 0, 128); balltextureregionarray[3] = bitmaptextureatlastextureregionfactory .createfromasset(balltexture, act, "purple_ball.png", 0, 192); balltextureregionarray[4] = bitmaptextureatlastextureregionfactory .createfromasset(balltexture, act, "yellow_ball.png", 0, 256); balltextureregionarray[5] = bitmaptextureatlastextureregionfactory .createfromasset(balltexture, act, "orange_ball.png", 0, 320); balltextureregionarray[6] = bitmaptextureatlastextureregionfactory .createfromasset(balltexture, act, "black_ball.png", 0, 384); balltextureregionarray[7] = bitmaptextureatlastextureregionfactory .createfromasset(balltexture, act, "white_ball.png", 0, 448); balltextureregionarray[8] = bitmaptextureatlastextureregionfactory .createfromasset(balltexture, act, "select_ball.png", 0, 512); balltexture.load(); // load balltexture scene
... , of course error.
11-28 17:44:44.750: e/androidruntime(2119): fatal exception: updatethread 11-28 17:44:44.750: e/androidruntime(2119): java.lang.nullpointerexception 11-28 17:44:44.750: e/androidruntime(2119): @ com.eklypze.android.mastermdhd.gamescene.drawpanel(gamescene.java:188) 11-28 17:44:44.750: e/androidruntime(2119): @ com.eklypze.android.mastermdhd.gamescene.createscene(gamescene.java:68) 11-28 17:44:44.750: e/androidruntime(2119): @ com.eklypze.android.mastermdhd.basescene.<init>(basescene.java:38) 11-28 17:44:44.750: e/androidruntime(2119): @ com.eklypze.android.mastermdhd.gamescene.<init>(gamescene.java:28) 11-28 17:44:44.750: e/androidruntime(2119): @ com.eklypze.android.mastermdhd.scenemanager.creategamescene(scenemanager.java:120) 11-28 17:44:44.750: e/androidruntime(2119): @ com.eklypze.android.mastermdhd.baseactivity$1.ontimepassed(baseactivity.java:84) 11-28 17:44:44.750: e/androidruntime(2119): @ org.andengine.engine.handler.timer.timerhandler.onupdate(timerhandler.java:94) 11-28 17:44:44.750: e/androidruntime(2119): @ org.andengine.engine.handler.updatehandlerlist.onupdate(updatehandlerlist.java:47) 11-28 17:44:44.750: e/androidruntime(2119): @ org.andengine.engine.engine.onupdateupdatehandlers(engine.java:618) 11-28 17:44:44.750: e/androidruntime(2119): @ org.andengine.engine.engine.onupdate(engine.java:605) 11-28 17:44:44.750: e/androidruntime(2119): @ org.andengine.engine.engine.ontickupdate(engine.java:568) 11-28 17:44:44.750: e/androidruntime(2119): @ org.andengine.engine.engine$updatethread.run(engine.java:858)
here gamescene.java class:
package com.eklypze.android.mastermdhd; import java.util.random; import javax.microedition.khronos.opengles.gl10; import org.andengine.engine.camera.camera; import org.andengine.entity.modifier.alphamodifier; import org.andengine.entity.modifier.loopentitymodifier; import org.andengine.entity.scene.background.spritebackground; import org.andengine.entity.sprite.sprite; import org.andengine.input.touch.touchevent; import org.andengine.opengl.util.glstate; import android.util.log; import com.eklypze.android.mastermdhd.scenemanager.scenetype; public class gamescene extends basescene { /*** declarations ***/ /* settings */ protected static final int camera_width = 480; protected static final int camera_height = 800; /* sprites */ sprite[] selectpanel = new sprite[8]; sprite boardpieces[] = new sprite[40]; sprite bwpegs[] = new sprite[10]; sprite nextspot; // "next spot" cursor sprite gameoverlose, gameoverwin; // replace scenes /* game options */ private int turn = 0; private int turncounter = 0; // per line turn counter private int currentx = 1; private int currenty = 13; private int[] code = new int[4]; // array store generated code private int[] codecopy = new int[4]; // black&white peg use private int blackpegs = 0, whitepegs = 0; // remember take currentx-1 array indexes private int[] currentxvalues = new int[4]; // dummy variable when drawing selectpanel touch *don't delete* private int z = 0; boolean gameover = false; boolean doublesallowed = false; /************************************ * ------------inherited------------ ************************************/ @override /******************************** * createscene() ********************************/ public void createscene() { // create scene bgsprite background setbackground(new spritebackground(resourcemanager.bgsprite)); // step 1: start new game newgame(); // step 2: draw selectpanel drawpanel(); // debugging: game seems work until point } @override /******************************** * onbackkeypressed() ********************************/ public void onbackkeypressed() { system.exit(0); } @override /******************************** * getscenetype() ********************************/ public scenetype getscenetype() { return scenetype.scene_game; } @override /******************************** * disposescene() ********************************/ public void disposescene() { this.detachself(); this.dispose(); } /************************************ * -----------game methods----------- ************************************/ /******************************** * newgame() * description: initialize game * settings new session. ********************************/ private void newgame() { /* [start] generate new code combination (int x = 0; x < 4; x++) { random r = new random(); // if doubles not allowed check if new generated number // double, if yes, generate number. note: doubles // defaulted 'off' until feature added. int randomnumber = r.nextint(8); // why (7-0)+0? log.v("randomr", "number generated " + randomnumber); code[x] = randomnumber; // write log (debugging) log.v("thecode", "number generated " + x + " is: " + code[x] + " (" + resourcemanager.ballcolours[randomnumber] + ")"); // if doubles not allowed check if new generated number // double, if yes, generate number. note: doubles // defaulted 'off' until feature added. if (!doublesallowed && x > 0) { (int y = x - 1; y >= 0; y--) { // log.v("they", "y "+y); if (code[y] == randomnumber) { x--; } } } } [end] generate new code combination */ code = new int[] { 7, 1, 2, 3 }; log.v("thecode", "the code is: " + code[0] + "," + code[1] + "," + code[2] + "," + code[3] + "."); codecopy = code.clone(); // copies code array white/black peg use } /******************************** * drawpanel() * description: draw panels * required user selection. ********************************/ private void drawpanel() { int column = 7; // constant? int rowstart = 2; /* [start] draw selection selectpanel (int = 0; < 8; i++) { final int j = i; selectpanel[i] = new sprite(grid_xpixel(column), grid_ypixel(rowstart), resourcemanager.balltextureregionarray[i], vbom) { @override /* [start] touch detection public boolean onareatouched(touchevent pscenetouchevent, float ptoucharealocalx, float ptoucharealocaly) { switch (pscenetouchevent.getaction()) { // on touch case touchevent.action_down: this.setscale(2.0f); // enlarge effect z = j; log.v("thisisz", "z: " + z); break; // on move/drag case touchevent.action_move: { /* implemented in future // this.setposition(pscenetouchevent.getx() - // this.getwidth()/2, pscenetouchevent.gety() - // this.getheight()/2); break; } // on release case touchevent.action_up: // z = j; // not needed makemove(z); this.setscale(1.0f); // normal size break; } return super.onareatouched(pscenetouchevent, ptoucharealocalx, ptoucharealocaly); } [end] touch detection }; */ // selectpanel[0] = new sprite(200, 400, // resourcemanager.balltextureregionarray[0], vbom); //attachchild(selectpanel[0]); //registertoucharea(selectpanel[i]); selectpanel[0] = new sprite(0, 0, resourcemanager.balltextureregionarray[0], vbom) { @override protected void predraw(glstate pglstate, camera pcamera) { super.predraw(pglstate, pcamera); pglstate.enabledither(); } }; selectpanel[0].setposition(240, 400); attachchild(selectpanel[0]); //} /* [end] draw selection selectpanel */ // settouchareabindingonactiondownenabled(true); } /******************************** * makemove() * description: allow player * make selection , * display pegs result. ********************************/ private void makemove(int incolor) { boardpieces[turn] = new sprite(grid_xpixel(currentx), grid_ypixelboard(currenty), resourcemanager.balltextureregionarray[incolor], vbom); boardpieces[turn].setscale(0.75f); // set 75% size on board // store current line, compare values code , generate b/w pegs currentxvalues[currentx - 1] = incolor; if (currentxvalues[currentx - 1] == codecopy[currentx - 1]) { blackpegs++; // dummy variable isn't counted again white peg codecopy[currentx - 1] = 999; } (int = 0; < 4; i++) { if ((currentxvalues[currentx - 1] == codecopy[i])) { whitepegs++; // dummy variable isn't counted again white peg codecopy[i] = 999; } } /* log debugging */ log.v("pegs", "blackpegs: " + blackpegs); log.v("pegs", "whitepegs: " + whitepegs); // draw pieces scene , advance next turn & column attachchild(boardpieces[turn]); currentx++; turn++; // advance next row, draw b/w pegs if (currentx > 4) { currentx = 1; currenty--; // draw pegs drawbwpegs(blackpegs, whitepegs); turncounter++; // reset pegs next line blackpegs = 0; whitepegs = 0; // codecopy used counting black , white // pegs per line ensure cases work codecopy = code.clone(); } /* [start] draw blinking cursor in next spot */ nextspot = new sprite(grid_xpixel(currentx), grid_ypixelboard(currenty), resourcemanager.balltextureregionarray[8], vbom); nextspot.setscale(0.75f); nextspot.setblendfunction(gl10.gl_src_alpha, gl10.gl_one_minus_src_alpha); nextspot.registerentitymodifier(new loopentitymodifier( new alphamodifier(2, 0f, 1.0f))); attachchild(nextspot); /* [end] draw blinking cursor in next spot */ /* * * game on (lose) * if player reaches turn 40 , still has not received * correct code, go game on (lose) scene. * */ if (turn == 40) { // note: replace game on scene. gameoverwin(false); log.v("game over", "you lose"); gameoverlose = new sprite(camera_width / 2 - 256, camera_height / 2 - 64, resourcemanager.losetextureregion, vbom); attachchild(gameoverlose); } } /******************************** * gameoverwin() * description: display gameover * image result of user * winning game. ********************************/ private void gameoverwin(boolean win) { // clear game detachchildren(); turn = 0; } /******************************** * drawbwpegs() * description: draw black * , white pegs scene * based on game results. ********************************/ private void drawbwpegs(int numblack, int numwhite) { /* [start] if */ // not display if no pegs counted if (numblack > 0 || numwhite > 0) { int pegscore = 0; // determine pegscore if (numblack == 1 && numwhite == 0) { pegscore = 0; } else if (numblack == 1 && numwhite == 2) { pegscore = 1; } else if (numblack == 1 && numwhite == 3) { pegscore = 2; } else if (numblack == 0 && numwhite == 1) { pegscore = 3; } else if (numblack == 2 && numwhite == 0) { pegscore = 4; } else if (numblack == 2 && numwhite == 2) { pegscore = 5; } else if (numblack == 0 && numwhite == 2) { pegscore = 6; } else if (numblack == 3 && numwhite == 0) { pegscore = 7; } else if (numblack == 3 && numwhite == 1) { pegscore = 8; } else if (numblack == 0 && numwhite == 3) { pegscore = 9; } else if (numblack == 4 && numwhite == 0) { pegscore = 10; } else if (numblack == 0 && numwhite == 4) { pegscore = 11; } /* [end] if */ // use pegscore display corresponding image bwpegs[turncounter] = new sprite(grid_xpixel(5), grid_ypixelboard(currenty + 1), resourcemanager.pegtextureregionarray[pegscore], vbom); bwpegs[turncounter].setscale(0.80f); attachchild(bwpegs[turncounter]); } } /******************************** * ---------grid system--------- ********************************/ /************************************ * grid_xpixel() * description: converts grid * coordinates pixel coordinates * based on 480 800 resolution * screen. needs updated. ************************************/ private int grid_xpixel(int x) { int pixel = 0; pixel = x * (camera_width / 8 + 2) - 32; return pixel; } /************************************ * grid_ypixel() * description: y-grid user * selection panel. ************************************/ private int grid_ypixel(int y) { int pixel = 0; pixel = y * (camera_height / 10) - 32; return pixel; } /************************************ * grid_ypixelboard() * description: y-grid main * game board. ************************************/ private int grid_ypixelboard(int y) { int pixel = 0; pixel = y * (camera_height / 15) - 32; return pixel; } }
i apologize i'm new stackoverflow , don't quite know how format code properly. also, if i'm missing important areas of code relevant question please let me know have many lines of code , don't quite know start.
thanks in advance!
looking @ code i'd balltextureregionarray[0] null pointer, because wasn't loaded in resourcemanager initialisation.
if .png files resources i.e. in "res" directory not "asset" directory need load .createfromresource not .createfromasset.
Comments
Post a Comment