-1

Trying learn googleSceneform for AR and my app crashes every time,any thougts?

My error from logcat and application crahses not on emulator and on the phone too.

    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ar/com.example.ar.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6494)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
        at com.example.ar.MainActivity.SetClickListener(MainActivity.java:213)
        at com.example.ar.MainActivity.onCreate(MainActivity.java:63)
        at android.app.Activity.performCreate(Activity.java:7009)
        at android.app.Activity.performCreate(Activity.java:7000)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) 
        at android.app.ActivityThread.-wrap11(Unknown Source:0) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:164) 
        at android.app.ActivityThread.main(ActivityThread.java:6494) 
        at java.lang.reflect.Method.invoke(Native Method) 

This is my MainActivty.java application crashes imediately and it is even not open

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    ArFragment arFragment;
    private ModelRenderable bearRenderable;

    ImageView bear, cat, cow, dog, elephant, ferret, hippopotamus, horse, koala, lion, reindeer, wolverine;

    View arrayView[];
    ViewRenderable name_animal;
    int selected = 1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        arFragment = (ArFragment) getSupportFragmentManager().findFragmentById(R.id.sceneform_ux_fragment);
        //View
        bear = (ImageView) findViewById(R.id.bear);
        cat = (ImageView) findViewById(R.id.cat);

        SetArrayView();
        SetClickListener();
        setupModel();
        arFragment.setOnTapArPlaneListener(new BaseArFragment.OnTapArPlaneListener() {
            @Override
            public void onTapPlane(HitResult hitResult, Plane plane, MotionEvent motionEvent) {          
                if (selected == 1) {
                    Anchor anchor = hitResult.createAnchor();
                    AnchorNode anchorNode = new AnchorNode(anchor);
                anchorNode.setParent(arFragment.getArSceneView().getScene());
                    createModel(anchorNode, selected);
                }
            }
        });
    }
    private void setupModel() {
        ModelRenderable.builder()
                .setSource(this, R.raw.bear)
                .build().thenAccept(renderable -> bearRenderable = renderable)
                .exceptionally(
                        throwable -> {
                            Toast.makeText(this, "Unnable to load bear model", Toast.LENGTH_SHORT).show();
                            return null;
                        }
                );
    private void createModel(AnchorNode anchorNode, int selected) {
        if (selected == 1) {
            TransformableNode bear = new TransformableNode(arFragment.getTransformationSystem());
            bear.setParent(anchorNode);
            bear.setRenderable(bearRenderable);
            bear.select();
        }
    }
    private void SetClickListener() {
        for (int i=0;i<arrayView.length;i++)
            arrayView[i].setOnClickListener(this);
    }

    private void SetArrayView() {
        arrayView = new View[]{
                bear,cat,cow,dog,elephant,ferret,hippopotamus,horse,koala,lion,reindeer,wolverine
        };
    }
    @Override
    public void onClick(View v) {

    }
}

I am a beginner so i just try to find my mistakes. I thinks it is about View and something about ClickListener

Tamir Abutbul
  • 7,301
  • 7
  • 25
  • 53
  • 2
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Tamir Abutbul Apr 01 '19 at 14:14
  • 2
    Most of your `ImageView` objects are `null` (e.g cow, dog, elephant..), and you are calling `setOnClickListener` on them, so it crashes . – Arnaud Apr 01 '19 at 14:14

3 Answers3

0

To prevent null pointer exception you can do this in your SetClickListener method :

 private void SetClickListener() {
        for (int i=0;i<arrayView.length;i++)
        {
           if( arrayView[i] != null ) 
            arrayView[i].setOnClickListener(this);
        }    
    }
Kévin Giacomino
  • 487
  • 4
  • 11
0

In your code only bear and cat aren't null, so you can either bind other image views either you apply a click listener on the not null objects like this

private void SetClickListener() {
    for (int i=0;i<arrayView.length;i++)
      if (arrayView[i] != null)
        arrayView[i].setOnClickListener(this);
}
Amine
  • 2,241
  • 2
  • 19
  • 41
0

In your onCreate method, you are initializing only 2 ImageView components, bear and cat. The others, cow, dog, elephant... are null. To manage this, you should find all other ImageView components.

cow = (ImageView) findViewById(R.id.cow);
dog = (ImageView) findViewById(R.id.dog);

or test the null value in setOnClickListener

 private void SetClickListener() {
        for (int i=0;i<arrayView.length;i++)
          if (arrayView[i] != null)
            arrayView[i].setOnClickListener(this);
    }
Aleksandar G
  • 1,163
  • 2
  • 20
  • 25
Ferran
  • 1,442
  • 1
  • 6
  • 10