Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - NullPointerException from creating an adapter

I am creating an array adapter for a list view, everything works ok, I have 2 fragments, and 2 buttons at the top of the action bar that changes between this 2 fragments. my problem is that I get crashes if I move too fast between those frags, when I open fragOne, switch to fragTwo, and then quickly move back to fragOne.. fragOne throws a NPE from the getActivity context..

that's the line that crashes:

adapter = new MainFragmentDocumentAdapter(getActivity(), docsList, DocumentsFragment.this, page);

the log report :

E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.bbb.app, PID: 17438
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
        at android.widget.ArrayAdapter.init(ArrayAdapter.java:310)
        at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:104)
        at com.bbb.app.UI.adapters.MainFragmentDocumentAdapter.<init>(MainFragmentDocumentAdapter.java:51)

any idea how can I solve this issue?

like image 922
JozeRi Avatar asked Feb 09 '15 16:02

JozeRi


People also ask

How do I fix NullPointerException in Android?

How to fix the NullPointerException? To avoid NullPointerException we have to initialize the Textview component with the help of findviewbyid( ) method as shown below. The findViewbyId( ) takes the “id” value of the component as the parameter. This method helps locate the component present in the app.

How do I fix NullPointerException in processing?

In Java, the java. lang. NullPointerException is thrown when a reference variable is accessed (or de-referenced) and is not pointing to any object. This error can be resolved by using a try-catch block or an if-else condition to check if a reference variable is null before dereferencing it.

What is NullPointerException in Android?

java.lang.NullPointerException. Thrown when an application attempts to use null in a case where an object is required. These include: Calling the instance method of a null object. Accessing or modifying the field of a null object.

What could be the reason if you are getting NullPointerException?

What Causes NullPointerException. The NullPointerException occurs due to a situation in application code where an uninitialized object is attempted to be accessed or modified. Essentially, this means the object reference does not point anywhere and has a null value.


Video Answer


3 Answers

So basically after lots of check I found out that the problem was that I was returning to that fragment while inside a different fragment because I had a listener pointing out there and trying to open that method.

basically I just wrapped it in a

if (getActivity() != null) {
    // Code goes here.
}

and problem solved.

thanks a lot though for all the help guys !

like image 131
JozeRi Avatar answered Oct 17 '22 08:10

JozeRi


if you are on a Fragment check if the fragment is added.

{
    ... 

    if (!this.isAdded()) { //this = current fragment
        return;
    }
    LocationManager locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
    ...
}
like image 2
Pedro Romão Avatar answered Oct 17 '22 10:10

Pedro Romão


you can do like this.

inside activity

public static UsageRecommendationTabActivity getInstance() {
        return activityInstance;
    }
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
activityInstance=this;
}

inside fragment

 @Override
    public void onAttach(Activity activity) {
        // TODO Auto-generated method stub
        super.onAttach(activity);
        if (activity instanceof UsageRecommendationTabActivity)
            mParentActivity = (UsageRecommendationTabActivity) activity;
        if (mParentActivity == null)
            mParentActivity = UsageRecommendationTabActivity.getInstance();
    }

and then call your adapter

adapter = new MainFragmentDocumentAdapter(mParentActivity, docsList, DocumentsFragment.this, page);
like image 1
Srishti Roy Avatar answered Oct 17 '22 09:10

Srishti Roy