0

I'm experiencing a nullpointerexception in my android java code in the following code. This is inside AndroidGraphics.java.

public void drawPixmap(Pixmap pixmap, int x, int y) {
    canvas.drawBitmap(((AndroidPixmap)pixmap).bitmap, x, y, null);
}

AndroidPixmap.java is here:

package com.badlogic.androidgames.framework.impl;

import android.graphics.Bitmap;

import com.badlogic.androidgames.framework.Graphics.PixmapFormat;
import com.badlogic.androidgames.framework.Pixmap;

public class AndroidPixmap implements Pixmap{
    Bitmap bitmap;
    PixmapFormat format;

    public AndroidPixmap(Bitmap bitmap, PixmapFormat format){
        this.bitmap = bitmap;
        this.format = format;
    }

    @Override
    public int getWidth(){
        return bitmap.getWidth();
    }

    @Override
    public int getHeight(){
        return bitmap.getHeight();
    }

    @Override
    public PixmapFormat getFormat(){
        return format;
    }

    @Override
    public void dispose(){
        bitmap.recycle();
    }
}

Is there something wrong with the casting? Any help would be great!

EDIT: This is the pixmap class:

package com.badlogic.androidgames.framework;

import com.badlogic.androidgames.framework.Graphics.PixmapFormat;

public interface Pixmap {
    public int getWidth();

    public int getHeight();

    public PixmapFormat getFormat();

    public void dispose();
}

2 Answers2

0

Your AndroidPixmap implements Pixmap, it is not inherited/extended from Pixmap. If you cast to Pixmap, you get the Implementation only, which I assume has bitmap=null. As you didn't add the Pixmap class to the question, it's quite hard to answer in more detail.

Oliver
  • 1,269
  • 13
  • 21
  • Thanks. I've added the Pixmap class – user1570204 Aug 02 '12 at 11:17
  • @user1570204, ok thats not gonna work what you want to do. Best would be to add a function getBitmap() to your (Android)Pixmap class and use that, instead (non-working) casting. I guess you not want to use Pixmap extends Bitmap, but rather wrap the Bitmap inside. Please refer to the answer of ScouseChris above, but not use the public "bitmap" element but a getter getBitmap(). – Oliver Aug 02 '12 at 16:10
0

Try this:

public void drawPixmap(AndroidPixmap pixmap, int x, int y) {
    canvas.drawBitmap(pixmap.bitmap, x, y, null);
}

Alternatively if you want to keep Pixmap in the signature you can make it an abstract class and extend it instead of an interface.

ScouseChris
  • 4,377
  • 32
  • 38