0

I have updated my PaintView code because according to my android studio error log, it says that I need to extends to android.app.activity but now I have a null reference error in my Bitmap getwidth(). I am new to android app but from my understanding, it is not returning the object because it can't find bitmap? Here is my PaintView class:

public class PaintView extends android.app.Activity{

    LayoutParams params;
    Path path = new Path();
    Paint br = new Paint();
    Bitmap background;
    Bitmap background2;


  public PaintView(Context context) {


      super();

        int maxHeight = 2000;
        int maxWidth = 1000;

       float scale = Math.min(((float) maxHeight / background.getWidth()), ((float) maxWidth / background.getHeight()));

        Matrix matrix = new Matrix();
        matrix.postScale(scale, scale);

        background = Bitmap.createBitmap(background, 0, 0, background.getWidth(), background.getHeight(), matrix, true);
        background2 = Bitmap.createBitmap(background2, 0, 0, background2.getWidth(), background2.getHeight(), matrix, true);



        br.setColor(Color.GREEN);
        br.setStyle(Paint.Style.STROKE);
        br.setStrokeJoin(Paint.Join.ROUND);
        br.setStrokeWidth(10f);

        params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);

    }

    private float getHeight() {
     int height = 5;
     return height;
    }

    private float getWidth() {
      int width = 5;
      return width;
    }


    //@Override
    public void onClick(View view) {
    Log.i("Hello", "Hello you!");
    }
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Mark Rotteveel Sep 05 '20 at 06:43
  • Not really because I am new to android development.... –  Sep 05 '20 at 07:02
  • Post the error log from logcat. – TRK P Sep 05 '20 at 07:14
  • Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference at com.example.paint.PaintView.(PaintView.java:53) at com.example.paint.MainActivity.onCreate(MainActivity.java:24) at android.app.Activity.performCreate(Activity.java:7009) at android.app.Activity.performCreate(Activity.java:7000) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214) –  Sep 05 '20 at 07:22

1 Answers1

0

You're using the bitmap in the constructor of the class before you assign it.

float scale = ... background.getWidth() ...
...
background = Bitmap.createBitmap(...)
Jean
  • 10,545
  • 2
  • 31
  • 31
  • I thought that I had done that earlier as in: Bitmap background; Bitmap background2; –  Sep 05 '20 at 07:12
  • It has told me to do something like this but am still getting the same error –  Sep 05 '20 at 07:16
  • float scale = 0; matrix.postScale(scale, scale); scale = Math.min(((float) maxHeight / background.getWidth()), ((float) maxWidth / background.getHeight())); –  Sep 05 '20 at 07:16
  • I am now using the following code instead but still getting the same error –  Sep 05 '20 at 09:21
  • background = BitmapFactory.decodeResource(getResources(), R.drawable.worksheetblank2); –  Sep 05 '20 at 09:21
  • I am still waiting for some help here –  Sep 05 '20 at 12:22
  • Declaring a variable is not the same as initializing it. When you write "Bitmap background;" you declare a variable. It's null until the first assignment. – Jean Sep 06 '20 at 06:06
  • Also impatiently writing "I am waiting for some help" is not going to get you a lot of anwsers. StackOverflow is a community site where volunteers answer your question. I am under no obligation to spend my own time helping you at all, much less to watch this page constantly to make sure you get your help within the same day. – Jean Sep 06 '20 at 06:10