Search code examples
javaandroidnullpointerexceptionimagebuttonfindviewbyid

Initialised ImageButton in other Activity throws nullPointerException when referenced in MainActivity


I'm very new to Android and Java coding in general, so bear with me if I don't understand basic concepts.. This is just a test to see if something works, so It might not initially make any sense.

I have two activities, Main and Other. the Other Activity has an imageButton with visibility initially set to invisible. When MainActivity is created, it should look for that imageButton in the Other activity, and set it's view to visible.

Though when debugging, all I get is a nullPointerException, because the button has null as value. How can I make it reference the button?

Part of Main Activity:

public class MainActivity extends AppCompatActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    pstep();
}


public void pstep() {
    int pstep = 0;
    ImageButton panfav = (ImageButton) findViewById(R.id.favpancake);
    panfav.setVisibility(View.VISIBLE);
}

Part of Other Activity:

public class navfav extends AppCompatActivity {
ImageButton panfav = (ImageButton) findViewById(R.id.favpancake);
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.navfav);
}
    public void fab(View v){
    ImageButton panfav = (ImageButton) findViewById(R.id.favpancake);
    panfav.setVisibility(View.VISIBLE);
}

XML linked to Other Activity:

    <ImageButton
    android:id="@+id/favpancake"
    android:layout_width="wrap_content"
    android:layout_height="137dp"
    android:layout_alignParentStart="true"
    android:layout_below="@+id/imageView3"
    android:background="@android:color/background_dark"
    android:contentDescription="@string/nav_cook_dish"
    android:onClick="fab"
    android:scaleType="centerCrop"
    android:src="@drawable/dishpancake"
    android:visibility="invisible" />

Log:

java.lang.NullPointerException: Attempt to invoke virtual method 'void 
android.widget.ImageButton.setVisibility(int)' on a null object reference

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 
'void android.widget.ImageButton.setVisibility(int)' on a null object reference
at com.p2.rookie.MainActivity.pstep(MainActivity.java:23)
at com.p2.rookie.MainActivity.onCreate(MainActivity.java:16)

Shoot if I'm missing anything you need to use, I'll provide as much as i can :) Best


Solution

  • you are getting nullPointerException because ImageButton with id favpancake is not present in MainActivity layout activity_main

    public class navfav extends AppCompatActivity {
    
    public static List<String> recipes;
    
    // other code here
    
    }
    

    and then when you want to add recipe to shown in Other Activity listView,

    recipes = new ArrayList<String>(); 
    
    recipes.add(recipe);
    

    Now when you open the other Activity all those recipes will be available in list name "recipes". which you can use to show in your listView.