-2

The NullPointeerException pointed towards this line

    import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Typeface;

public class Painter {
    private Canvas canvas;
    private Paint paint;
    private Rect srcRect;
    private Rect dstRect;
    private RectF dstRectF;

public Painter(Canvas canvas) {
    this.canvas = canvas;
    paint = new Paint();
    srcRect = new Rect();
    dstRect = new Rect();
    dstRectF = new RectF();
}

public void setColor(int color) {
    paint.setColor(color);
}

public void setFont(Typeface typeface, float textSize) {
    paint.setTypeface(typeface);
    paint.setTextSize(textSize);
}

public void drawString(String str, int x, int y) {
    canvas.drawText(str, x, y, paint);
}

public void fillRect(int x, int y, int width, int height) {
    dstRect.set(x, y, x + width, y + height);
    paint.setStyle(Paint.Style.FILL);
    canvas.drawRect(dstRect, paint);
}

public void drawImage(Bitmap bitmap, int x, int y) {
// NullPointerException here        canvas.drawBitmap(bitmap, x, y, paint);
}

public void drawImage(Bitmap bitmap, int x, int y, int width, int height) {
    srcRect.set(0, 0, bitmap.getWidth(), bitmap.getHeight());
    dstRect.set(x, y, x + width, y + height);
    canvas.drawBitmap(bitmap, srcRect, dstRect, paint);
}

public void fillOval(int x, int y, int width, int height) {
    paint.setStyle(Paint.Style.FILL);
    dstRectF.set(x, y, x + width, y + height);
    canvas.drawOval(dstRectF, paint);
}

It shouldn't be null since the file is stored in the project. The only way I can see where a problem may be is because the initial framework I copied from kept all the pictures in a seperate folder named assets which clearly isn't a default folder. Instead I placed all resources in the drawable folder. What exactly is the problem?

Logcat
11-06 10:21:55.114  25146-25168/rect.draw.gametest E/AndroidRuntime﹕ FATAL EXCEPTION: Game Thread

 Process: rect.draw.gametest, PID: 25146
    java.lang.NullPointerException
            at android.graphics.Canvas.throwIfCannotDraw(Canvas.java:1083)
            at android.graphics.Canvas.drawBitmap(Canvas.java:1139)
            at rect.draw.gametest.model.util.Painter.drawImage(Painter.java:45)
            at rect.draw.gametest.model.state.MenuState.render(MenuState.java:28)
            at rect.draw.gametest.GameView.updateAndRender(GameView.java:100)
            at rect.draw.gametest.GameView.run(GameView.java:119)
            at java.lang.Thread.run(Thread.java:841)
That Thatson
  • 309
  • 1
  • 2
  • 16
  • Plaese, show more code and print logcat – ArtKorchagin Nov 06 '15 at 15:19
  • If the NPE is being thrown from a line in your code, you could try setting a breakpoint on that line to see which thing is null and then trace back from there. I'm guessing maybe canvas here -- but as Arthur said, we really need to see more code. – Mike Paxton Nov 06 '15 at 15:21
  • @ArthurKorchagin I added more code. It was more nullpointers, but I figured that I could solve the rest if I figured the main one pointed out. – That Thatson Nov 06 '15 at 15:24

1 Answers1

1

No.Assets is a default folder for image if you try to load it as bitmap.Drawable folder contain the image you want to use by R.drawable.xxx.

tiny sunlight
  • 6,231
  • 3
  • 21
  • 42