Why ByteArrayOutputStream sometimes gives me null

2019-08-05 07:19发布

问题:

When loading a picture into byte[] in smartphone app sometimes ByteArrayOutputStream gives me nullpointerexception any explanation?

Bitmap bm = BitmapFactory.decodeFile(path);
System.out.println("BITMAP: "+bm != null);
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
bm.compress(CompressFormat.JPEG, 100, buffer);

回答1:

Are you sure it's ByteArrayOutputStream that's giving you the NullPointerException?

Or is it happening at bm.compress? bm can be null - likely due to you passing in an invalid path.



回答2:

The problem wasn't the code it was the thread that entered there first.