英文:
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();
}
});
}
});
错误截图如下:
英文:
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 "";
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;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论