[Java for Android App Development] kevdog100:
So I'm trying to something fairly simple. Have a very basic screen that has 4 blocks (Bitmaps 0-3 in my code) which are different colors that cycle from the bottom to the top of the screen. When the bitmap reaches the top (goes offscreen), I want it to change to a different bitmap randomly and then restart itself from the beginning. I'll attach my GameView class below (which an instance of is sent into the main layout).

public class GameView extends View {

private static final int Y_VELOCITY = 8;
int x=0,y=0; //Enemy positions
int p=0; //onDraw Counter

Bitmap a,b,c,d;
Bitmap[] bitmapArray = {a,b,c,d};//0,1,2,3
Bitmap currentBitmap;

private Canvas canvas;

public GameView(Context context) {
super(context);

loadResources(currentBitmap);
}

private void loadResources(Bitmap mBitmap) {
bitmapArray[0] = BitmapFactory.decodeResource(getResources(), R.drawable.box_one);
bitmapArray[1] = BitmapFactory.decodeResource(getResources(), R.drawable.box_two);
bitmapArray[2] = BitmapFactory.decodeResource(getResources(), R.drawable.box_three);
bitmapArray[3] = BitmapFactory.decodeResource(getResources(), R.drawable.box_four);

mBitmap = bitmapArray[0];
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);

//Draw canvas to put paint on
Rect canvasRect = new Rect();

Paint canvasBG = new Paint();
canvasBG.setColor(Color.WHITE);
canvasRect.set(0, 0, canvas.getWidth(), canvas.getHeight());
canvas.drawRect(canvasRect, canvasBG);

//Draw bitmaps- First make vertical loop with 1 block
if(y < -100) {
currentBitmap= bitmapArray[randomNumGenerator(3,0)];
p++; //Counter to initialize first enemy OFFSCREEN, (see else rule)
Thread startTimer = new Thread();
try { //If restarting, sleep for .5 seconds then respawn in random X w/ Y offscreen
y=canvas.getHeight()+50;
x= randomNumGenerator(canvas.getWidth(), 0); //Generate next X in random position as respawns
startTimer.sleep(randomNumGenerator(5000,100)); //Sleeps for random time between .1-5s
} catch (InterruptedException e) {
e.printStackTrace();
}
} else { //If first time enemy spawns, start him offscreen (-500) else, shoot him up screen
if(p==0) {
y=-500;
} else {
y-=Y_VELOCITY;
}
}

canvas.drawBitmap(currentBitmap, x, y, null);

invalidate();

}

private static int randomNumGenerator(int max, int min) {
//Set min = 0 for nextInt()
Random randomNum = new Random();
int randomNumVal = randomNum.nextInt(max - min) + min ;
return randomNumVal;
}
}

I receive no errors in eclipse when running this but when I port it to my phone it closes the the error I get is a nullpointerexception [logcat below] for cannot draw bitmap (currentBitmap). If I change currentBitmap in canvas.drawBitmap section to say bitmapArray[2] it works perfectly fine but obviously the bitmaps wont cycle randomly then. If I initialize currentBitmap in my ondraw it won't cycle as well- it just keeps resetting to whatever I have my currentBitmap set to everytime it re-draws itself... Where am I going wrong here?

11-13 23:39:11.189: E/AndroidRuntime(17736): FATAL EXCEPTION: main
11-13 23:39:11.189: E/AndroidRuntime(17736): Process: com.twigmandesigns.veritcalautoscrollertest, PID: 17736
11-13 23:39:11.189: E/AndroidRuntime(17736): java.lang.NullPointerException
11-13 23:39:11.189: E/AndroidRuntime(17736):	at android.graphics.Canvas.throwIfCannotDraw(Canvas.java:1083)
11-13 23:39:11.189: E/AndroidRuntime(17736):	at android.view.GLES20Canvas.drawBitmap(GLES20Canvas.java:816)
11-13 23:39:11.189: E/AndroidRuntime(17736):	at com.twigmandesigns.veritcalautoscrollertest.GameView.onDraw(GameView.java:74)
11-13 23:39:11.189: E/AndroidRuntime(17736):	at android.view.View.draw(View.java:15508)
11-13 23:39:11.189: E/AndroidRuntime(17736):	at android.view.View.getDisplayList(View.java:14402)
11-13 23:39:11.189: E/AndroidRuntime(17736):	at android.view.View.getDisplayList(View.java:14444)
11-13 23:39:11.189: E/AndroidRuntime(17736):	at android.view.View.draw(View.java:15222)
11-13 23:39:11.189: E/AndroidRuntime(17736):	at android.view.ViewGroup.drawChild(ViewGroup.java:3340)
11-13 23:39:11.189: E/AndroidRuntime(17736):	at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3176)
11-13 23:39:11.189: E/AndroidRuntime(17736):	at android.view.View.getDisplayList(View.java:14397)
11-13 23:39:11.189: E/AndroidRuntime(17736):	at android.view.View.getDisplayList(View.java:14444)
11-13 23:39:11.189: E/AndroidRuntime(17736):	at android.view.View.draw(View.java:15222)
11-13 23:39:11.189: E/AndroidRuntime(17736):	at android.view.ViewGroup.drawChild(ViewGroup.java:3340)
11-13 23:39:11.189: E/AndroidRuntime(17736):	at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3176)
11-13 23:39:11.189: E/AndroidRuntime(17736):	at android.view.View.draw(View.java:15511)
11-13 23:39:11.189: E/AndroidRuntime(17736):	at com.android.internal.widget.ActionBarOverlayLayout.draw(ActionBarOverlayLayout.java:468)
11-13 23:39:11.189: E/AndroidRuntime(17736):	at android.view.View.getDisplayList(View.java:14402)
11-13 23:39:11.189: E/AndroidRuntime(17736):	at android.view.View.getDisplayList(View.java:14444)
11-13 23:39:11.189: E/AndroidRuntime(17736):	at android.view.View.draw(View.java:15222)
11-13 23:39:11.189: E/AndroidRuntime(17736):	at android.view.ViewGroup.drawChild(ViewGroup.java:3340)
11-13 23:39:11.189: E/AndroidRuntime(17736):	at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3176)
11-13 23:39:11.189: E/AndroidRuntime(17736):	at android.view.View.draw(View.java:15511)
11-13 23:39:11.189: E/AndroidRuntime(17736):	at android.widget.FrameLayout.draw(FrameLayout.java:472)
11-13 23:39:11.189: E/AndroidRuntime(17736):	at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2623)
11-13 23:39:11.189: E/AndroidRuntime(17736):	at android.view.View.getDisplayList(View.java:14402)
11-13 23:39:11.189: E/AndroidRuntime(17736):	at android.view.View.getDisplayList(View.java:14444)
11-13 23:39:11.189: E/AndroidRuntime(17736):	at android.view.HardwareRenderer$GlRenderer.buildDisplayList(HardwareRenderer.java:1597)
11-13 23:39:11.189: E/AndroidRuntime(17736):	at android.view.HardwareRenderer$GlRenderer.draw(HardwareRenderer.java:1469)
11-13 23:39:11.189: E/AndroidRuntime(17736):	at android.view.ViewRootImpl.draw(ViewRootImpl.java:2800)
11-13 23:39:11.189: E/AndroidRuntime(17736):	at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2666)
11-13 23:39:11.189: E/AndroidRuntime(17736):	at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2234)
11-13 23:39:11.189: E/AndroidRuntime(17736):	at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1267)
11-13 23:39:11.189: E/AndroidRuntime(17736):	at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6638)
11-13 23:39:11.189: E/AndroidRuntime(17736):	at android.view.Choreographer$CallbackRecord.run(Choreographer.java:813)
11-13 23:39:11.189: E/AndroidRuntime(17736):	at android.view.Choreographer.doCallbacks(Choreographer.java:613)
11-13 23:39:11.189: E/AndroidRuntime(17736):	at android.view.Choreographer.doFrame(Choreographer.java:583)
11-13 23:39:11.189: E/AndroidRuntime(17736):	at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:799)
11-13 23:39:11.189: E/AndroidRuntime(17736):	at android.os.Handler.handleCallback(Handler.java:733)
11-13 23:39:11.189: E/AndroidRuntime(17736):	at android.os.Handler.dispatchMessage(Handler.java:95)
11-13 23:39:11.189: E/AndroidRuntime(17736):	at android.os.Looper.loop(Looper.java:146)
11-13 23:39:11.189: E/AndroidRuntime(17736):	at android.app.ActivityThread.main(ActivityThread.java:5635)
11-13 23:39:11.189: E/AndroidRuntime(17736):	at java.lang.reflect.Method.invokeNative(Native Method)
11-13 23:39:11.189: E/AndroidRuntime(17736):	at java.lang.reflect.Method.invoke(Method.java:515)
11-13 23:39:11.189: E/AndroidRuntime(17736):	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
11-13 23:39:11.189: E/AndroidRuntime(17736):	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
11-13 23:39:11.189: E/AndroidRuntime(17736):	at dalvik.system.NativeStart.main(Native Method)