Search code examples
androidnullpointerexceptiondrawbitmap

drawBitmap nullpointerexception


this is my class.. ı want to use it in my simple game's GameEngine.. but ı didn't understand the problem here.. it doesn't work..

public class Droid {

    private Bitmap      bitmap; 
    private int     x;      
    private int     y;      
    private boolean         touched;    
    private Speed       speed;
    private Paint       paint;

    public Droid(Resources resources, Bitmap bitmap, int x, int y)
        {
            this.bitmap = bitmap;
            this.x = x;
            this.y = y;

            // create droid and load bitmap
            bitmap = BitmapFactory.decodeResource(resources,
                    R.drawable.droid_1);

        }

              public void draw(Canvas canvas)
        {

            canvas.drawBitmap(bitmap, x - bitmap.getWidth() / 2,
                    y- bitmap.getHeight() / 2, paint);
        }



     }

when ı run code, ı see a nullpointerexception at draw() method... how can ı solve this? thanks for help...


Solution

  • // ı hope, ı solved this problem.. finally.. :)

    public class Droid {

    private Bitmap      bitmap; 
    private int     x;      
    private int     y;      
    private boolean         touched;    
    private Speed       speed;
    private Paint       paint;
    
    public Droid(Resources resources, Bitmap bitmap, int x, int y)
        {
            paint= new Paint();
    //    this.bitmap = bitmap;   // delete this part, it will work..  :)))
        this.x = x;
        this.y = y;
    
            // create droid and load bitmap
            bitmap = BitmapFactory.decodeResource(resources,
                    R.drawable.droid_1);
    
        }
    
              public void draw(Canvas canvas)
        {
    
            canvas.drawBitmap(bitmap, x - bitmap.getWidth() / 2,
                    y- bitmap.getHeight() / 2, paint);
        }
    
    
    
     }