Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.isEmpty()' on a null object reference

huangapple go评论72阅读模式
英文:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.isEmpty()' on a null object reference

问题

这段代码在 Android 版本 9 及以上的系统上会崩溃,而在版本 9 以下的系统上正常运行。以下是代码示例:

public class Utils_String {

    public static String prepareContacts(Context ctx, String number) {
        if (number.isEmpty())
            return "";

        String preparedNumbers = number.trim();
        preparedNumbers = preparedNumbers.replace(" ", "");
        preparedNumbers = preparedNumbers.replace("(", "");
        preparedNumbers = preparedNumbers.replace(")", "");
        if (preparedNumbers.contains("+")) {
            preparedNumbers = preparedNumbers.replace(preparedNumbers.substring(0, 3), ""); //去除国家代码
        }
        preparedNumbers = preparedNumbers.replace("-", "");
        return preparedNumbers;
    }

}

代码中调用 Utils_String 的部分如下:

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, "IN", contacts1);
            if (Build.VERSION.SDK_INT > 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, "IN", contacts);
            if (Build.VERSION.SDK_INT > 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();
            }
        });
    }
});

错误截图如下:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.isEmpty()' on a null object reference

英文:

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

public class Utils_String {

    public static String prepareContacts(Context ctx,String number) {
        if (number.isEmpty())
            return "";
        
        String preparednumbers=number.trim();
        preparednumbers=preparednumbers.replace(" ","");
        preparednumbers=preparednumbers.replace("(","");
        preparednumbers=preparednumbers.replace(")","");
        if(preparednumbers.contains("+")) {
            preparednumbers=preparednumbers.replace(preparednumbers.substring(0,3),""); //to remove country code
        }
        preparednumbers=preparednumbers.replace("-","");
        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,"IN",contacts1);
            if(Build.VERSION.SDK_INT>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,"IN",contacts);
            if(Build.VERSION.SDK_INT>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();
            }
        });
    }
});

答案1

得分: 0

你必须检查该数字是否为 null,然后再检查该值是否为空。

正如 @Jon Skeet 所评论的,最佳方法是添加新的条件,使用 OR 条件来检查是否为 null。

public static String prepareContacts(Context ctx, String number){
   if(number == null || number.isEmpty()){
      return "";
   }else{
      String preparedNumbers = number.trim();
      preparedNumbers = preparedNumbers.replace(" ", "");
      preparedNumbers = preparedNumbers.replace("(", "");
      preparedNumbers = preparedNumbers.replace(")", "");
      if(preparedNumbers.contains("+")){
            preparedNumbers = preparedNumbers.replace(preparedNumbers.substring(0, 3), ""); //去除国家代码
      }
      preparedNumbers = preparedNumbers.replace("-", "");
      return preparedNumbers;
   }
}
英文:

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 "";
}else{
String preparednumbers=number.trim();
preparednumbers=preparednumbers.replace(" ","");
preparednumbers=preparednumbers.replace("(","");
preparednumbers=preparednumbers.replace(")","");
if(preparednumbers.contains("+")){
preparednumbers=preparednumbers.replace(preparednumbers.substring(0,3),""); //to remove country code
}
preparednumbers=preparednumbers.replace("-","");
return preparednumbers;
}
}

huangapple
  • 本文由 发表于 2020年8月24日 20:31:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/63561122.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定