I am trying to write program that transforms a picture to black-white picture. I encountered with a problem and searched for the same issues but i didn't find a proper solution for me. Here is the code:
public class MainActivity extends AppCompatActivity {
ImageView image;
Drawable drawable;
Bitmap bitmap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image = (ImageView) findViewById(R.id.imageView);
bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap(); **// Here is line that error occur**
image.setImageBitmap(converter(bitmap));
}
public Bitmap converter(Bitmap first) {
Bitmap end = Bitmap.createBitmap(first.getWidth(),
first.getHeight(),
first.getConfig());
Stacktrace:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.graphics.Bitmap android.graphics.drawable.BitmapDrawable.getBitmap()' on a null object reference
at com.example.murat.giveeffect.MainActivity.onCreate(MainActivity.java:26)
image.setDrawingCacheEnabled(true); bitmap = image.getDrawingCache();insteadbitmap = ((BitmapDrawable)image.getDrawable()).getBitmap();