Attempt to invoke virtual method 'void android.graphics.Bitmap.copyPixelsFromBuffer(java.nio.Buffer)' on a null object reference

23,536

Solution 1

btmp is null. There is no way that by using the attached code.Then, the value of btmp would be anything. But it's null!

If you want to clone Bitmap the use create method or any other of that sort.

Bitmap btmp = Bitmap.create(drawable.getBitmap());

Solution 2

"yes, btmp is null"

But, you trying to invoke method call on it:

btmp.copyPixelsFromBuffer(buffer_temp); // <- here

That not going to work. You should initialize btmp before using it.

Update:

Init it like this:

...
System.out.println("Values are "+ bitmap.getAllocationByteCount());

// here's the initialization
Bitmap btmp = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());

buffer_temp.rewind();

// now you can call copyPixelsFromBuffer() on btmp
btmp.copyPixelsFromBuffer(buffer_temp);
...
Share:
23,536
Alexander Zaldostanov
Author by

Alexander Zaldostanov

Updated on October 05, 2020

Comments

  • Alexander Zaldostanov
    Alexander Zaldostanov over 3 years

    I have a ImageView I am getting bitmap out of that, and then use copyPixelstoBuffer and I am copying it to buffer_temp, now I want to use reverse algorithm to again convert it to another bitmap and from that bitmap to ImageView2,

    what exactly I am doing is Copying an Image in ImageView using Buffer and Pasting it onto another Imageview using Buffer, but while Copying copyPixelsFromBuffer always throw

    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.graphics.Bitmap.copyPixelsFromBuffer(java.nio.Buffer)' on a null object reference.
    

    Dont know why, Need help,

    try {
                    Buffer bfr = null;
                    iv1.setImageResource(R.drawable.olx);
                    BitmapDrawable drawable = (BitmapDrawable) iv1.getDrawable();
                    Bitmap bitmap = drawable.getBitmap();
                    int bytes=bitmap.getByteCount();
                    ByteBuffer buffer_temp= ByteBuffer.allocate(bytes);
                    bitmap.copyPixelsToBuffer(buffer_temp);
                    System.out.println("Values are "+ bitmap.getAllocationByteCount());
    
                    Bitmap btmp=null;
    
                    //btmp.copyPixelsFromBuffer(buffer_temp);
    
    
                    if(buffer_temp==null)
                     return;
    
                    buffer_temp.rewind();
    
                    btmp.copyPixelsFromBuffer(buffer_temp);
    
    
                    if(buffer_temp==null)
                    {
                        Toast.makeText(getApplicationContext(), "Null", Toast.LENGTH_SHORT).show();
                    }
                    else
                    {
                        Toast.makeText(getApplicationContext(), "Not Null", Toast.LENGTH_SHORT).show();
                    }
    
    
    
                } catch (NotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }