Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8838069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:53:41+00:00 2026-06-14T09:53:41+00:00

If I have the following code in a Fragment: public void doSomething() { if

  • 0

If I have the following code in a Fragment:

public void doSomething() {
    if (getActivity() == null)
        return; // no null pointer exceptions here!
    // ...
    // do bunch of stuff here
    // ...

    getActivity().setTitle("done something"); // just whatever
}

As you can see, at the beginning of the method I check if getActivity() is null and return if it is. However, if it is not null, the method continues executing. At the end, I call getActivity() again to set title, however I don’t check if getActivity() is null. Theoretically, it’s possible at this point the Fragment has been detached from the Activity so it’s possible getActivity() could be null and therefore the last line will throw a NullPointerException. What is the solution for this? Check if getActivity() is null every time I want to call it?

Edit: Even if we try this, it’s possible to get a NPE – I just called getActivity() each time to simplify the example:

public void doSomething() {
    Activity activity = getActivity();
    if (activity == null)
        return; // no null pointer exceptions here!
    // ...
    // do bunch of stuff here
    // ...

    activity.setTitle("done something"); // just whatever
}

The problem is not that the Activity hasn’t finished loading yet – this can happen minutes after load for instance, as a response to any event. The problem is not the FIRST call to getActivity, it’s that in the 15 lines or so from the time we check if activity is null to the last time we reference it, it can no longer exist.

I don’t have a logcat right now, but this is what is happening. We haven’t really seen this ourselves, but this is for an app where this function is being called thousands of times a day, so it’s possible to happen once in a while to someone.

Edit 2: What I’m currently doing is trying and catching for NPEs and just not doing something if it fails. However it seems like an ugly way to solve the problem – I’m wondering if there’s a better solution!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T09:53:42+00:00Added an answer on June 14, 2026 at 9:53 am

    Call getActivity once at the top and save it in a variable. Why call it 20 times when it won’t change in between (and if it did, using a mix of the old and new could be really bad anyway).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: import android.support.v4.app.FragmentActivity; import android.support.v4.app.Fragment; public void onCreate(Bundle savedInstanceState) {
I have the following code fragment from a project I'm working on: public void
I have following code for updating user's column public void UpdateLastModifiedDate(string username) { using
I have the following C# code fragment: using System; class count { public static
I have to following code fragment, and no matter what I set the font-size
We have the following code fragment: char tab[2][3] = {'1', '2', '\0', '3', '4',
I have the following code fragment: NSString* value = @value; NSString* key = @key;
I have a problem with the following code fragment. It's intended to handle events
I have the following java code fragment while (condition1){ switch (someinteger){ case 1: if(condition2)
I have the following code fragment in an object initializer. However, the third line

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.