Runtime Error with Canvas SaveAs -- null object reference

I get this error when I use Canvas.SaveAs
Attempt to invoke virtual method 'boolean java.lang.Boolean.booleanValue()' on a null object reference

The project is a simple canvas and a save button on an Android device. The DefaultFileScope is "Shared".

canvas_save_as.aia (1.6 KB)

Try to put name in jpg/jpeg or leave extension blank

12345.png to 12345

I gave that a try and the problem persists. :face_with_diagonal_mouth:

The App Inventor Reference says: fileName must end with one of “.jpg”, “.jpeg”, or “.png” (which determines the file type: JPEG, or PNG).

You shall put an image inside the canvas.
I did it and it works, for me.
I just added an image to your code ( .aia attached afterwards).

The following image is the jpeg image file "12345.jpg" created by your code after the 207.png image has been loaded into the assets


Give it a try.

canvas_save_as.aia (333.8 KB)

You should not need to add an image, setting filescope to Shared will make no difference to where canvasSaveas saves, which will be the ASD when android version > 10. Will probably work on versions before that.

This looks like a bug!

If default filescope is set to Shared in project Properties it generates the above error.

1 Like

@uskiara The AIA you shared still has the null object reference error when I try to save. :frowning_face:

Ok sorry, as I said, it works for me, but probably it depends on my Android version (9) which allows me to store files also out of ASD when the filescope is set to Shared.
Please follow Tim's hints. Also @Anke has written a lot about file storage on newer Androids.

You cannot save a file in the root dir of the external storage on Android 11+, you must save it in one of the Shared folders.

See also here: Some basics on Android storage system

2 Likes

Note: Saving a file via Canvas to one of the Shared folders by setting DefaultFileScope = Shared or Legacy only works on Screen1.

image
(that is a self-deprecating "doh!", by the way...)

image

Yes, but this works only on Screen1.

In truth, i did not know Canvas.SaveAs did that, I understood it to be hardwired to the ASD, regardless of android version / filescope.

That is two things I have learned today :smiley:

In the words of Albert Einstein, “once you stop learning, you start dying”.

1 Like

For once I don't agree with Einstein. It should say: "once you stop loving, you start dying".

1 Like

That would explain the cause of the error. Thank you! And, thanks for keeping the Android Storage thread up-to-date!

Wouldn't this still be considered a bug? App Inventor shouldn't be attempting to invoke a method on a null object.