Android 在单元测试中使用libphonenumber时引发空指针异常

Android 在单元测试中使用libphonenumber时引发空指针异常,android,unit-testing,nullpointerexception,mockito,libphonenumber,Android,Unit Testing,Nullpointerexception,Mockito,Libphonenumber,我正在使用“michaelrocks.libphonenumber.android”格式化和验证电话号码。当我运行应用程序时,它工作正常 我写了两个测试,但都失败了,出现空指针异常。无法找出出现异常的原因 private static Phonenumber.PhoneNumber getPhoneNumberObj(final String phone, final String code, PhoneNumberUtil phoneNumberUtil){ Phonenum

我正在使用“michaelrocks.libphonenumber.android”格式化和验证电话号码。当我运行应用程序时,它工作正常

我写了两个测试,但都失败了,出现空指针异常。无法找出出现异常的原因

 private static Phonenumber.PhoneNumber getPhoneNumberObj(final String phone, final String code, PhoneNumberUtil phoneNumberUtil){
        Phonenumber.PhoneNumber phoneNumberObj = null;
        try {
            String isoCode = phoneNumberUtil.getRegionCodeForCountryCode(Integer.parseInt(code));
            /**THIS IS WHERE THERE IS A NULL POINTER EXCEPTION**/
            phoneNumberObj = phoneNumberUtil.parse(phone, isoCode);         
        } catch (Exception e) {
            Log.e(TAG, "Phone parsing error", e);
        }
        return phoneNumberObj;
    }
我在这里犯了什么根本性的错误吗?。非常感谢您的帮助和建议

这是处理空指针异常的方法

 private static Phonenumber.PhoneNumber getPhoneNumberObj(final String phone, final String code, PhoneNumberUtil phoneNumberUtil){
        Phonenumber.PhoneNumber phoneNumberObj = null;
        try {
            String isoCode = phoneNumberUtil.getRegionCodeForCountryCode(Integer.parseInt(code));
            /**THIS IS WHERE THERE IS A NULL POINTER EXCEPTION**/
            phoneNumberObj = phoneNumberUtil.parse(phone, isoCode);         
        } catch (Exception e) {
            Log.e(TAG, "Phone parsing error", e);
        }
        return phoneNumberObj;
    }
这是完整的代码

验证程序类

进口和方法

import io.michaelrocks.libphonenumber.android.PhoneNumberUtil;
import io.michaelrocks.libphonenumber.android.Phonenumber;

public static boolean validatePhone(final String phone, final String code, Context context) {
        PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.createInstance(context);
        Phonenumber.PhoneNumber phoneNumber = null;
        try{
            phoneNumber = getPhoneNumberObj(phone, code, phoneNumberUtil);
        }
        catch (Exception e){
            Log.d(TAG, e.getMessage());
        }
        return phoneNumber != null && phoneNumberUtil.isValidNumber(phoneNumber);
    }

    public static String getFormattedPhoneNumber(final String phone, final String code, Context context) {
        String formatedPhoneNumber = "";
        PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.createInstance(context);
        Phonenumber.PhoneNumber phoneNumber = null;
        try {
            phoneNumber = getPhoneNumberObj(phone, code, phoneNumberUtil);
            if(phoneNumber != null && phoneNumberUtil.isValidNumber(phoneNumber)){
                formatedPhoneNumber = phoneNumberUtil.format(phoneNumber, PhoneNumberUtil.PhoneNumberFormat.E164);
            }
        } catch (Exception e) {
            Log.e(TAG, "Phone parsing error", e);
        }
        return formatedPhoneNumber;
    }

    private static Phonenumber.PhoneNumber getPhoneNumberObj(final String phone, final String code, PhoneNumberUtil phoneNumberUtil){
        Phonenumber.PhoneNumber phoneNumberObj = null;
        try {
            String isoCode = phoneNumberUtil.getRegionCodeForCountryCode(Integer.parseInt(code));
            phoneNumberObj = phoneNumberUtil.parse(phone, isoCode);
        } catch (Exception e) {
            Log.e(TAG, "Phone parsing error", e);
        }
        return phoneNumberObj;
    }
我的单元测试

@RunWith(MockitoJUnitRunner.class)
public class ValidatorTest {

    @Mock
    Context mMockContext;

    private final String VALID_PHONE_NUMBER = "07455658798";
    private final String VALID_CODE = "+44";
    private final String VALID_FORMATTED_PHONE_NUMBER = "+447455658798";

    @Test
    public void validatePhone() throws Exception {
        assertThat(Validator.validatePhone(VALID_PHONE_NUMBER, VALID_CODE, mMockContext), is(true));
    }

    @Test
    public void getFormattedPhoneNumber() throws Exception {
        String result = Validator.getFormattedPhoneNumber(VALID_PHONE_NUMBER, VALID_CODE, mMockContext);
        assertThat(result, is(VALID_FORMATTED_PHONE_NUMBER));
    }
}

似乎您需要显示这个实现:
PhoneNumberUtil.createInstance(上下文)也是。嗨,Bartosz,我在validatePhone和getFormattedPhoneNumber方法中都有它,它可能返回
null
。我看了一下,它不是空的。堆栈跟踪没有告诉你什么是空的吗?