Title: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.isEmpty()' on a null object reference
Tags: java;runtime-error
Question: This code works fine on android version below 9 but crashes on 9 and ABOVE:

```public class Utils_String {

    public static String prepareContacts(Context ctx,String number) {
        if (number.isEmpty())
            return &quot;&quot;;
        
        String preparednumbers=number.trim();
        preparednumbers=preparednumbers.replace(&quot; &quot;,&quot;&quot;);
        preparednumbers=preparednumbers.replace(&quot;(&quot;,&quot;&quot;);
        preparednumbers=preparednumbers.replace(&quot;)&quot;,&quot;&quot;);
        if(preparednumbers.contains(&quot;+&quot;)) {
            preparednumbers=preparednumbers.replace(preparednumbers.substring(0,3),&quot;&quot;); //to remove country code
        }
        preparednumbers=preparednumbers.replace(&quot;-&quot;,&quot;&quot;);
        return preparednumbers;
    }

}
```
The code where I call ```utils_Strings```:
```recyclerAdapter.setListener(new Adapter_incoming.itemClickListener() {
    @Override
    public void onClick(View v, int position) {
        if(mensu){
            Contacts_data contacts1= (Contacts_data) searchPeople.get(position);
            String records= GetContact.getRecordsList(v.getContext(),recordings,&quot;IN&quot;,contacts1);
            if(Build.VERSION.SDK_INT&gt;18){
                GetContact.openMaterialSheetDialog(getLayoutInflater(),position,records, Utils_String.prepareContacts(ctx,contacts1.getNumber()));
            }else{
                GetContact.showDialog(v.getContext(),records,contacts1);
            }
        }else {
            Contacts_data contacts= (Contacts_data) realrecordingcontacts.get(position);
            String records= GetContact.getRecordsList(v.getContext(),recordings,&quot;IN&quot;,contacts);
            if(Build.VERSION.SDK_INT&gt;18){
                GetContact.openMaterialSheetDialog(getLayoutInflater(),position,records, Utils_String.prepareContacts(ctx,contacts.getNumber()));
            }else{
                GetContact.showDialog(v.getContext(),records,contacts);
            }
        }
        GetContact.setItemrefresh(new GetContact.refresh() {
            @Override
            public void refreshList(boolean var) {
                if(var)
                    recyclerAdapter.notifyDataSetChanged();
            }
        });
    }
});
```

Comment: Where exactly you are calling Utils_String ? and did you check for 
1. NULL value 2. !TextUtils.isEmpty(number)) 3. check for "" or 'null' (if it's from cursor

Comment: How are you calling that method?  What are you using for the parameters?

Here is the accepted answer: You have to check the number is null or not, after that you can check if the value is empty or not.
As @Jon Skeet commmented, the best way approached this is, add new condition using OR for null
```public static String prepareContacts(Context ctx, String number){
   if(number == null || number.isEmpty()){
      return &quot;&quot;;
   }else{
      String preparednumbers=number.trim();
      preparednumbers=preparednumbers.replace(&quot; &quot;,&quot;&quot;);
      preparednumbers=preparednumbers.replace(&quot;(&quot;,&quot;&quot;);
      preparednumbers=preparednumbers.replace(&quot;)&quot;,&quot;&quot;);
      if(preparednumbers.contains(&quot;+&quot;)){
            preparednumbers=preparednumbers.replace(preparednumbers.substring(0,3),&quot;&quot;); //to remove country code
      }
      preparednumbers=preparednumbers.replace(&quot;-&quot;,&quot;&quot;);
      return preparednumbers;
   }
}
```

Comment for this answer: This won't currently compile - it doesn't return anything if `number` *is* null. Rather than indent most of the body further, I'd rewrite it as `if (number == null || number.isEmpty()) { return ""; } and then the rest of it can be all at the "top level" of the method, which will make it easier to read.
Comment for this answer: Feel free to update your answer with my suggestion if you agree with it - no credit necessary.
Comment for this answer: @JonSkeet should I update my answer, and credit to you ?


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 2, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2020:00:08/24/20', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<java><runtime-error>', 'Title': "Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.isEmpty()' on a null object reference", 'ViewCount': 871, 'id': 63561122, 'user_AboutMe': None, 'user_AccountId': 19362277.0, 'user_DisplayName': 'Chaudary Umair', 'user_Reputation': 3.0, 'user_Views': 7.0, 'user_id': 14157306.0}}