I am unable to run my program. The app gets crashed. I am trying to make the image move on the screen by touchEvent.
java.lang.NullPointerException:  Attempt to invoke virtual method 'int com.example.swapnil.new1.model.conmponents.Speed.getxDirection()' on a null object reference
    at com.example.swapnil.new1.GamePanel.update(GamePanel.java:89)
    at com.example.swapnil.new1.MainThread.run(MainThread.java:32)
I have added the Speed class which I think is responsible for this exception. Suggest any updates.
Speed class has the getxDirection method which is causing the exception.
package com.example.swapnil.new1.model.conmponents;
public class Speed {
    public static final int DIRECTION_RIGHT = 1;
    public static final int DIRECTION_LEFT = -1;
    public static final int DIRECTION_UP = -1;
    public static final int DIRECTION_DOWN = 1;
    private float xv = 1; // speed in x
    private float yv = 1; // speed in y
    private int xDirection = DIRECTION_RIGHT;
    private int yDirection = DIRECTION_DOWN;
    public Speed() {
        this.xv = 1;
        this.yv = 1;
    }
    public Speed(float xv, float yv) {
        this.xv = xv;
        this.yv = yv;
    }
    public float getXv() {
        return xv;
    }
    public void setXv(float xv) {
        this.xv = xv;
    }
    public float getYv() {
        return yv;
    }
    public void setYv(float yv) {
        this.yv = yv;
    }
    public int getxDirection() {
        return xDirection;
    }
    public void setxDirection(int xDirection) {
        this.xDirection = xDirection;
    }
    public int getyDirection() {
        return yDirection;
    }
    public void setyDirection(int yDirection) {
        this.yDirection = yDirection;
    }
    public void toggleXDirection() {
        xDirection = xDirection * -1;
    }
    public void toggleYDirection() {
        yDirection = yDirection * -1;
    }
}
The MainThread class:
package com.example.swapnil.new1;
import android.graphics.Canvas;
import android.util.Log;
import android.view.SurfaceHolder;
public class MainThread extends Thread {
    private static final String TAG = MainThread.class.getSimpleName();
    private boolean running;
    private SurfaceHolder surfaceHolder;
    private GamePanel gamePanel;
    public MainThread(SurfaceHolder surfaceHolder , GamePanel gamePanel) {
        super();
        this.surfaceHolder = surfaceHolder;
        this.gamePanel = gamePanel;
    }
    public void setRunning(boolean running) {
        this.running = running;
    }
    @Override
    public void run() {
        Canvas canvas;
        Log.d(TAG , "Starting game loop");
        while(running) {
            canvas = null;
            try {
                canvas = this.surfaceHolder.lockCanvas();
                synchronized (surfaceHolder) {
                    this.gamePanel.render(canvas);
                    this.gamePanel.update();
                }
            } finally {
                if(canvas != null) {
                    surfaceHolder.unlockCanvasAndPost(canvas);
                }
            }
        }
    }
}

The GamePanel class:
import android.app.Activity;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import com.example.swapnil.new1.model.Droid;
import com.example.swapnil.new1.model.conmponents.Speed;
public class GamePanel extends SurfaceView implements
        SurfaceHolder.Callback {
    private static final String TAG = GamePanel.class.getSimpleName();
    private MainThread thread;
    private Droid droid;
    public GamePanel(Context context) {
        super(context);
        getHolder().addCallback(this);
        droid = new Droid(BitmapFactory.decodeResource(getResources(),R.drawable.droid_1),50,50);
        thread = new MainThread(getHolder() , this);
        setFocusable(true);
    }
    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        thread.setRunning(true);
        thread.start();
    }
    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    }
    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        boolean retry = true;
        while (retry) {
            try {
                thread.join();
                retry = false;
            }catch (InterruptedException e) {
            }
        }
    }
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            droid.handleActionDown((int)event.getX(),(int)event.getY());
            if (event.getY() > getHeight() - 50) {
             thread.setRunning(false);
                ((Activity)getContext()).finish();
            } else {
                Log.d(TAG , "Coords : x = " + event.getX() + ",y = " + event.getY());
            }
        }
        if (event.getAction() == MotionEvent.ACTION_MOVE) {
            if (droid.isTouched()) {
                droid.setX((int)event.getX());
                droid.setY((int) event.getY());
            }
        }
        if (event.getAction() == MotionEvent.ACTION_UP) {
            if (droid.isTouched()) {
                droid.setTouched(false);
            }
        }
        return true;
    }
    // changed protected to public
    protected void render(Canvas canvas) {
        canvas.drawColor(Color.BLACK);
        droid.draw(canvas);
    }
    public void update() {
        // collision with right
        if (droid.getSpeed().getxDirection() == Speed.DIRECTION_RIGHT &&
                droid.getX() + droid.getBitmap().getWidth()/2 >= getWidth()) {
            droid.getSpeed().toggleXDirection();
        }
        //collision with left
        if (droid.getSpeed().getxDirection() == Speed.DIRECTION_LEFT &&
                droid.getX() - droid.getBitmap().getWidth()/2 <= 0) {
            droid.getSpeed().toggleXDirection();
        }
        // collision with down
        if (droid.getSpeed().getyDirection() == Speed.DIRECTION_DOWN &&
                droid.getY() + droid.getBitmap().getHeight()/2 >= getHeight()) {
            droid.getSpeed().toggleYDirection();
        }
        //collision with up
        if (droid.getSpeed().getyDirection() == Speed.DIRECTION_UP &&
                droid.getY() - droid.getBitmap().getHeight()/2 <= 0) {
            droid.getSpeed().toggleYDirection();
        }
        droid.update();
    }
}
 
     
    