0

I am trying to FileOutputStream a Bitmap drawn in a canvas, but Logcat gives me :

09-17 14:47:40.203: E/AndroidRuntime(2362): FATAL EXCEPTION: main
09-17 14:47:40.203: E/AndroidRuntime(2362): Process: com.example.drawv2, PID: 2362
09-17 14:47:40.203: E/AndroidRuntime(2362): java.lang.NullPointerException
09-17 14:47:40.203: E/AndroidRuntime(2362):     at android.graphics.Bitmap.compress(Bitmap.java:1002)
09-17 14:47:40.203: E/AndroidRuntime(2362):     at com.example.drawv2.Scribbler$1.onClick(Scribbler.java:65)
09-17 14:47:40.203: E/AndroidRuntime(2362):     at android.view.View.performClick(View.java:4780)
09-17 14:47:40.203: E/AndroidRuntime(2362):     at android.view.View$PerformClick.run(View.java:19866)
09-17 14:47:40.203: E/AndroidRuntime(2362):     at android.os.Handler.handleCallback(Handler.java:739)
09-17 14:47:40.203: E/AndroidRuntime(2362):     at android.os.Handler.dispatchMessage(Handler.java:95)
09-17 14:47:40.203: E/AndroidRuntime(2362):     at android.os.Looper.loop(Looper.java:135)
09-17 14:47:40.203: E/AndroidRuntime(2362):     at android.app.ActivityThread.main(ActivityThread.java:5257)
09-17 14:47:40.203: E/AndroidRuntime(2362):     at java.lang.reflect.Method.invoke(Native Method)
09-17 14:47:40.203: E/AndroidRuntime(2362):     at java.lang.reflect.Method.invoke(Method.java:372)
09-17 14:47:40.203: E/AndroidRuntime(2362):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
09-17 14:47:40.203: E/AndroidRuntime(2362):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

Here is my Scribbler class :

public class Scribbler extends Activity {

DrawView drawView;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linearLayout);

    LayoutInflater inflater = (LayoutInflater)getApplicationContext().getSystemService
              (Context.LAYOUT_INFLATER_SERVICE);

    drawView = (DrawView) inflater.inflate(R.layout.layoutview, null);

    drawView = new DrawView(this);
    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(30, 30);
    drawView.setLayoutParams(layoutParams);
    drawView.setBackgroundColor(Color.WHITE);
    linearLayout.addView(drawView);

    drawView.requestFocus();

    drawView.setDrawingCacheEnabled(true);

    Button button1 = (Button) findViewById(R.id.button1);
    button1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            Bitmap b = Scribbler.this.drawView.getDrawingCache();

            FileOutputStream fos = null;
            try {
                fos = new FileOutputStream(getFilesDir());
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }

            b.compress(CompressFormat.PNG, 95, fos);
        }
    });

}

}

I'm a bit confused since generally Logcat says which variable is the cause of NullPointerException, but here I don't get anything.

For information, I had the Cannot refer to the non-final local variable button defined in an enclosing scope error on the drawView, because I only needed its value when I clicked the button1. I assume the exception is still due to the fact that my drawView = null but I have no idea how to fix this.

Thank you in advance.

Stone Edge
  • 52
  • 11

3 Answers3

1

It is clear from the logcat that the bitmap you are getting from the line Scribbler.this.drawView.getDrawingCache() is null. As it is showing null pointer exception at the line where you re trying to compress bitmap but bitmap is null already.

Aakash
  • 5,181
  • 5
  • 20
  • 37
  • why is it null then? I launch the program, draw by hand in my canvas, click the button, and then I get the error – Stone Edge Sep 17 '15 at 15:19
1

I think your drawView is null. You can put if condition in your code before use drawView, like this.

if(drawView!=null){your code}

or you can print view value on your console to check, what is in it.

Devendra Dagur
  • 840
  • 1
  • 14
  • 35
0

I don't get this one:

drawView = (DrawView) inflater.inflate(R.layout.layoutview, null);
drawView = new DrawView(this);

Please take a look at this: Scribbler

Community
  • 1
  • 1
David
  • 306
  • 6
  • 20
  • That's already part of the information I used to do my code, and I had to inflate my view into my layout, which is another issue (now working) so I don't believe the NullPointerException has anything to do with the drawView inflation – Stone Edge Sep 17 '15 at 15:27