0

I am loading a bitmap from the assets to do some classification with it. The bitmap is loaded from the asset folder, no problem with that. When I run on debug mode I can view my Bitmap image. But when I try to pass it to my method "classify" I have this error :

FATAL EXCEPTION: main
              Process: com.example.anne_maelle.cifar10_final, PID: 10046
              java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.example.anne_maelle.cifar10_final.ImageClassifier.classifyFrame(android.graphics.Bitmap)' on a null object reference
                  at com.example.anne_maelle.cifar10_final.ClassifyActivity.classify(ClassifyActivity.java:61)
                  at com.example.anne_maelle.cifar10_final.ClassifyActivity.run_test(ClassifyActivity.java:54)
                  at com.example.anne_maelle.cifar10_final.ClassifyActivity$1.onClick(ClassifyActivity.java:37)
                  at android.view.View.performClick(View.java:5198)
                  at android.view.View$PerformClick.run(View.java:21147)
                  at android.os.Handler.handleCallback(Handler.java:739)
                  at android.os.Handler.dispatchMessage(Handler.java:95)
                  at android.os.Looper.loop(Looper.java:148)
                  at android.app.ActivityThread.main(ActivityThread.java:5417)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

I tried to display the bitmap, but it doesn't do anything : no error but no image either. My code is :

protected void run_test() throws IOException{
    AssetManager assets = this.getAssets();
    String imgs[] = assets.list("images");
    System.out.println("run test apres getAsset: "+imgs.toString());
    for(int i=0;i<imgs.length;i++){
        if(imgs[i].contains(".bmp")){
            System.out.println("loop, iteration "+i);
            Bitmap image = imageViaAssets(imgs[i],assets);
            if(image==null){
                Log.d(TAG,"null image");
            }
            else{
                Log.d(TAG,"image not null");
            }
            classify(image);
        }
    }
}

protected Bitmap imageViaAssets(String filename,AssetManager assets)throws IOException{
    InputStream ims = assets.open("images/"+filename);
    Bitmap image = BitmapFactory.decodeStream(ims);
    ims.close();
    ImageView img = (ImageView) findViewById(R.id.imageView);
    img.setImageBitmap(image);
    return image;
}
pRaNaY
  • 24,642
  • 24
  • 96
  • 146
AnnaBar
  • 29
  • 7
  • That Exception message is saying that your `ImageClassifier` reference in the `classify()` method is null. It's not about the `Bitmap`. – Mike M. Mar 20 '18 at 04:30
  • Thank you for your prompt reply ! It was indeed the problem. – AnnaBar Mar 20 '18 at 06:04

0 Answers0