Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

java.lang.NullPointerException, 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference

2022-09-03 17:02:35.870 14558-14558/com.example.myapplication_study E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myapplication_study, PID: 14558
java.lang.NullPointerException: Attempt to invoke virtual method ‘void android.widget.ListView.setAdapter(android.widget.ListAdapter)’ on a null object reference
at com.example.myapplication_study.ManagerUserman.onCreateView(ManagerUserman.java:40)
at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2995)
at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:523)
at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:261)
at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1840)
at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1758)
at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1701)
at androidx.fragment.app.FragmentManager$4.run(FragmentManager.java:488)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

If you get this error, the app won’t run don’t you know what’s the problem?
my code is here please help me tt

ManagerUserman.class

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import java.util.ArrayList;
import java.util.List;


public class ManagerUserman extends Fragment {
    private View view;

    private ListView listView;
    private ManagerUsermanListAdapter managerUsermanListAdapter;
    private List<ManagerUsermanList> usermanList;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.activity_manageruserman_list, container, false);
        Intent intent = getActivity().getIntent();


        listView = getActivity().findViewById(R.id.listView);
        usermanList = new ArrayList<ManagerUsermanList>();

        usermanList.add(new ManagerUsermanList("name", "ID", "Password"));
        managerUsermanListAdapter = new ManagerUsermanListAdapter(getActivity().getApplicationContext(), usermanList);
        listView.setAdapter(managerUsermanListAdapter);
        return view;
    }
}

ManagerUsermanListAdapter.class

public class ManagerUsermanListAdapter extends BaseAdapter {
    private Context context;
    private List<ManagerUsermanList> usermanList;

    public ManagerUsermanListAdapter(Context context, List<ManagerUsermanList> usermanList) {
        this.context = context;
        this.usermanList = usermanList;
    }

    @Override
    public int getCount() {
        return usermanList.size();
    }

    @Override
    public Object getItem(int i) {
        return usermanList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View convertView, ViewGroup parent) {
       View v = View.inflate(context, R.layout.activity_manageruserman_list, null);

       TextView userName = (TextView) v.findViewById(R.id.userName);
       TextView userID = (TextView) v.findViewById(R.id.userID);
       TextView userPassword = (TextView) v.findViewById(R.id.userPassword);

       userName.setText(usermanList.get(i).getName());
       userID.setText(usermanList.get(i).getUser_id());
       userPassword.setText(usermanList.get(i).getUser_pass());

       v.setTag(usermanList.get(i).getUser_id());
        return v;
    }
}

ManagerUsermanList.class

public class ManagerUsermanList {

    String name;
    String user_id;
    String user_pass;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getUser_id() {
        return user_id;
    }

    public void setUser_id(String user_id) {
        this.user_id = user_id;
    }

    public String getUser_pass() {
        return user_pass;
    }

    public void setUser_pass(String user_pass) {
        this.user_pass = user_pass;
    }


    public ManagerUsermanList(String name, String user_id, String user_pass) {
        this.name = name;
        this.user_id = user_id;
        this.user_pass = user_pass;
    }
}

mangeruserman.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".ManagerUserman">

        <ListView
            android:id="@+id/listView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="10dp"
            android:layout_marginRight="15dp"
            android:layout_marginLeft="15dp"
            android:dividerHeight="10dp"/>
</LinearLayout>

mangeruserman_list.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <ScrollView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">

            </ScrollView>

            <TextView
                android:id="@+id/userName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="name"
                android:textColor="#000"
                android:textSize="20dp" />

            <TextView
                android:id="@+id/userID"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="id"
                android:textColor="#000"
                android:textSize="15dp" />

            <TextView
                android:id="@+id/userPassword"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="pass"
                android:textColor="#000"
                android:textSize="15dp" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="38dp"
                android:orientation="horizontal">

                <Button
                    android:id="@+id/manager_user_delete_btn"
                    android:layout_width="100dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="del" />
            </LinearLayout>
        </LinearLayout>
    </ScrollView>
</LinearLayout>

>Solution :

first: The layout file is wrong in your fragment. should right is like "mangeruserman" but yours "activity_manageruserman_list".

second: You don’t need to use "getActivity().findViewById()", should use "view.findViewById()".

sorry, my English is not well, hope help for you.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading