1

this is the error

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference

I just learned about android, please guide me, I've been looking for a solution on YouTube and I've been serching but haven't found the solution

this is the mainActivity

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private RecyclerView tv_Category;
    private ArrayList<Makanan>list;
    private Button btn_detail;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv_Category = (RecyclerView)findViewById(R.id.rv_category);
        tv_Category.setHasFixedSize(true);
        list = new ArrayList<>();
        list.addAll(DataMakanan.getListData());
        showRecyclerCardView();
        btn_detail= (Button)findViewById(R.id.btn_set_detail);
        btn_detail.setOnClickListener(this);
    }

    private void showRecyclerCardView(){
        tv_Category.setLayoutManager(new LinearLayoutManager(this));
        CardViewMakananAdapter cardViewMakananAdapter = new CardViewMakananAdapter(this);
        cardViewMakananAdapter.setListMakanan(list);
        tv_Category.setAdapter(cardViewMakananAdapter);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_set_detail:
                Intent moveInstent = new Intent(MainActivity.this, MoveData.class);
                startActivity(moveInstent);
                break;
        }
    }
}

Any help would be greatly appreciated.

Rohit5k2
  • 17,948
  • 8
  • 45
  • 57
  • Are you sure `btn_set_detail` is inside `activity_main`? – Rohit5k2 Sep 26 '18 at 09:40
  • 1
    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) – Rohit5k2 Sep 26 '18 at 09:44
  • post your xml file. – Chirag Savsani Sep 26 '18 at 09:46
  • Wrong place to call `btn_detail= (Button)findViewById(R.id.btn_set_detail);`, I think. If it's for list item, index to view lookup might be needed. Also, it may be recycled (and sometimes becomes null) when the list is scrolled. Please see a sample of `getView()` in this page [ListView](https://developer.android.com/reference/android/widget/ListView) – Toris Sep 26 '18 at 12:32

0 Answers0