App crashes when button click due to "Caused by: java.lang.NullPointerException" [duplicate]

  android, java, nullpointerexception

I am making an app with Admin and User login for food delivery, User and Admin will share the same menu layout(Constraint) but Admin will have an extra Linear Layout to be able to add Image, Name, Desc of Meals. Whereas Users will have the same layout(Constraint) except the Linear Layout for adding Image, Name, Desc of Meals will be GONE. Whenever I click on Login, it crashes.

loginpage.java

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.loginpage);
    LoginEMail = findViewById(R.id.LoginEMail);
    LoginPassword = findViewById(R.id.LoginPassword);
    LoginBtn = findViewById(R.id.LoginBtn);
    myDB = new DatabaseHelper(this);
}

public void Login(View view) {
    Intent intent = new Intent(loginpage.this, MenuSelection.class);
    if (LoginEMail.getText().toString().equals("admin") && (LoginPassword.getText().toString().equals("admin"))) {
        startActivity(intent);
        //menu.linearlayoutvisible(VISIBLE);
    }
        else if(LoginEMail.getText().toString().equals("user") &&  (LoginPassword.getText().toString().equals("user"))) {
        menu.layoutsetgone();
        startActivity(intent);
    }
            else
        Toast.makeText(loginpage.this, "Incorrect E-mail or Password.", Toast.LENGTH_SHORT).show();
}

MenuSelection.java (Please ignore the array adapter parts.) ( The necessary declaration have been done as well.)

    @Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.menu_selection);

    db = new MenuDatabaseHelper(MenuSelection.this);

    listItem =new ArrayList<>();

    add_data = findViewById(R.id.add_data);
    add_image = findViewById(R.id.add_image);
    add_name = findViewById(R.id.add_name);
    add_desc = findViewById(R.id.add_desc);
    menu_list = findViewById(R.id.menu_list);
    linearLayout = findViewById(R.id.linearLayout);


    // Create data
    ArrayList<Menu> arrayList = new ArrayList<>();

    //MenuAdapter menuAdapter = new MenuAdapter(this, R.layout.list_row);

    viewData();
    
    menu_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            String text = menu_list.getItemAtPosition(i).toString();
            Toast.makeText(MenuSelection.this, ""+text, Toast.LENGTH_SHORT).show();
        }
    });

    add_data.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //String image = add_image.get
            String name = add_name.getText().toString();
            String desc = add_desc.getText().toString();
            if (!name.equals("") && db.insertMenuData(name, desc)){
                Toast.makeText(MenuSelection.this, "Data added.", Toast.LENGTH_SHORT).show();
                add_name.setText("");
                add_desc.setText("");
                listItem.clear();
                viewData();
            }   else {
                Toast.makeText(MenuSelection.this, "Data not added.", Toast.LENGTH_SHORT).show();
            }
        }
    });
}

private void viewData() {
    Cursor cursor = db.viewData();

    if (cursor.getCount() == 0){
        Toast.makeText(this, "No data to show", Toast.LENGTH_SHORT).show();
    } else{
        while (cursor.moveToNext()){
            listItem.add(cursor.getString(1)); // index 1 is name, index 0 is ID
        }

        //adapter = new ArrayAdapter<>(this, android.R.layout.list_row, listItem);
        //menu_list.setAdapter(adpater);
    }
}
public void layoutsetgone() {
    linearLayout.setVisibility(View.GONE);
}

menu_selection.xml

<ListView
    android:id="@+id/menu_list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.027" />

<LinearLayout
    android:id="@+id/linearLayout"
    android:layout_width="316dp"
    android:layout_height="51dp"
    android:orientation="horizontal"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="1.0">

    <ImageView
        android:id="@+id/add_image"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:src="@color/black" />

    <EditText
        android:id="@+id/add_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="Name" />

    <EditText
        android:id="@+id/add_desc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="Description" />

    <Button
        android:id="@+id/add_data"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Add" />
</LinearLayout>

Source: Android Questions

LEAVE A COMMENT