当我 post 要切断数据时的空对象引用
null object reference when I post the data to sever
我已经为 post 创建了序列化的 class 数据到服务器使用 retrofit
public class TradePreRegistrationRequest {
@SerializedName("REG_Company")
public REGCompany regCompany;
@SerializedName("REG_CompanyAddress")
public REGCompanyAddress regCompanyAddress;
@SerializedName("REG_CompanyService")
public List<REGCompanyService> regCompanyServices;
@SerializedName("REG_CompanyHours")
public List<REGCompanyHours> regCompanyHourses;
@SerializedName("REG_Member")
public REGMember regMember;
public static class REGMember{
@SerializedName("UserID")
public String userID;
@SerializedName("UserPassword")
public String userPassword;
@SerializedName("Salutation")
public String salutation;
@SerializedName("FirstName")
public String firstName;
@SerializedName("LastName")
public String lastName;
@SerializedName("MobileNumber")
public String mobileNumber;
@SerializedName("EmailAddress")
public String emailAddress;
@SerializedName("Gender")
public String gender;
@SerializedName("ContactPreference")
public String contactPreference;
@SerializedName("RegistrationType")
public String registrationType;
@SerializedName("RegistrationMode")
public String registrationMode;
}
public static class REGCompanyService {
@SerializedName("ServiceID")
public Integer serviceID;
}
public static class REGCompanyHours {
@SerializedName("WeekDay")
public Integer weekDay;
@SerializedName("IsOpen")
public Integer isOpen;
@SerializedName("StartTime")
public Integer startTime;
@SerializedName("CloseTime")
public Integer closeTime;
}
public static class REGCompanyAddress {
@SerializedName("AddressType")
public String addressType;
@SerializedName("Address1")
public String address1;
@SerializedName("Address2")
public String address2;
@SerializedName("StreetName")
public String streetName;
@SerializedName("CompanyCity")
public String companyCity;
@SerializedName("CompanyState")
public String companyState;
@SerializedName("PostalCode")
public String postalCode;
@SerializedName("Longitude")
public String longitude;
@SerializedName("Latitude")
public String latitude;
}
public static class REGCompany {
@SerializedName("ReferenceCode")
public String referenceCode;
@SerializedName("CompanyType")
public String companyType;
@SerializedName("CompanyName")
public String companyName;
@SerializedName("CompanyEmailAddress")
public String companyEmailAddress;
@SerializedName("CompanyTier")
public String companyTier;
@SerializedName("ContactNumber")
public String contactNumber;
}
}
然后我尝试 post 使用 retrofit2 的数据,如下所示
final TradePreRegistrationRequest registrationRequest = new TradePreRegistrationRequest();
registrationRequest.regMember.userID = "Text";
registrationRequest.regMember.userPassword = mBinding.password.getText().toString();
registrationRequest.regMember.salutation = mBinding.salutation.getText().toString();
registrationRequest.regMember.emailAddress = mBinding.email.getText().toString();
registrationRequest.regMember.firstName = mBinding.fullName.getText().toString();
registrationRequest.regMember.lastName = mBinding.fullName.getText().toString();
registrationRequest.regMember.mobileNumber = mBinding.mobileNumber.getText().toString();
registrationRequest.regMember.gender = "1";
registrationRequest.regMember.contactPreference = "1";
registrationRequest.regMember.registrationType = "1";
registrationRequest.regMember.registrationMode = "1";
registrationRequest.regCompany.companyType = "1";
registrationRequest.regCompany.companyName = mBinding.registeredName.getText().toString();
registrationRequest.regCompany.companyTier = "";
registrationRequest.regCompany.companyEmailAddress = mBinding.emailWork.getText().toString();
registrationRequest.regCompany.contactNumber = mBinding.workshopNumber.getText().toString();
registrationRequest.regCompany.referenceCode = mBinding.docRefCode.getText().toString();
registrationRequest.regCompanyAddress.address1 = mBinding.address1.getText().toString();
registrationRequest.regCompanyAddress.address2 = mBinding.address2.getText().toString();
registrationRequest.regCompanyAddress.addressType = "1";
registrationRequest.regCompanyAddress.companyCity = mBinding.town.getText().toString();
registrationRequest.regCompanyAddress.companyState = mBinding.state.getText().toString();
registrationRequest.regCompanyAddress.streetName = mBinding.address2.getText().toString();
registrationRequest.regCompanyAddress.postalCode = mBinding.postalCode.getText().toString();
registrationRequest.regCompanyAddress.latitude = "1";
registrationRequest.regCompanyAddress.longitude = "103";
Repository.apiService().dsrRegistration(registrationRequest).enqueue(new BaseCallback<CommonResponse>() {
@Override
public void onSuccess(CommonResponse body) {
super.onSuccess(body);
mBinding.progressBar.setVisibility(View.GONE);
if (body.responseCode != CommonResponse.Code.SUCCESS) {
PopupErrorDialog.newInstance(body.responseMessage.header, body.responseMessage.message, body.responseMessage.btnText1, null, null, null).show(getSupportFragmentManager(), "popup_error");
} else {
Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();
}
}
@Override
public void onFail(String title, String msg) {
super.onFail(title, msg);
mBinding.progressBar.setVisibility(View.GONE);
PopupErrorDialog.newInstance(title, title, "Try Again", null, null, null).show(getSupportFragmentManager(), "popup_error");
}
});
然后我收到这个错误
FATAL EXCEPTION: main
Process: com.edenred.shell, PID: 22666
java.lang.NullPointerException: Attempt to write to field 'java.lang.String .data.request.TradePreRegistrationRequest$REGMember.userID' on a null object reference
at com.edenred.shell.ui.registerdsr.RegistrationActivity.registration(RegistrationActivity.java:54)
at com.edenred.shell.ui.registerdsr.RegistrationActivity.lambda$onCreate(RegistrationActivity.java:38)
at com.edenred.shell.ui.registerdsr.RegistrationActivity$$Lambda.onClick(Unknown Source)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22429)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6121)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)
我需要设置数据以使用 TradePreRegistrationRequest class 将其发送到服务器。
这个错误请指教
提前致谢。
您的代码的问题是您在使用它们之前创建了 'TradePreRegistrationRequest' 的引用,而不是其字段的引用,您可以在使用它们之前创建每个字段的引用,或者您可以通过以下方式初始化它们默认是这样的:-
public class TradePreRegistrationRequest {
@SerializedName("REG_Company")
public REGCompany regCompany = new REGCompany();
@SerializedName("REG_CompanyAddress")
public REGCompanyAddress regCompanyAddress = new REGCompanyAddress();
@SerializedName("REG_CompanyService")
public List<REGCompanyService> regCompanyServices = new ArrayList<>();
@SerializedName("REG_CompanyHours")
public List<REGCompanyHours> regCompanyHourses = new ArrayList<>();
@SerializedName("REG_Member")
public REGMember regMember = new REGMember();
//**** your further code//
}
我已经为 post 创建了序列化的 class 数据到服务器使用 retrofit
public class TradePreRegistrationRequest {
@SerializedName("REG_Company")
public REGCompany regCompany;
@SerializedName("REG_CompanyAddress")
public REGCompanyAddress regCompanyAddress;
@SerializedName("REG_CompanyService")
public List<REGCompanyService> regCompanyServices;
@SerializedName("REG_CompanyHours")
public List<REGCompanyHours> regCompanyHourses;
@SerializedName("REG_Member")
public REGMember regMember;
public static class REGMember{
@SerializedName("UserID")
public String userID;
@SerializedName("UserPassword")
public String userPassword;
@SerializedName("Salutation")
public String salutation;
@SerializedName("FirstName")
public String firstName;
@SerializedName("LastName")
public String lastName;
@SerializedName("MobileNumber")
public String mobileNumber;
@SerializedName("EmailAddress")
public String emailAddress;
@SerializedName("Gender")
public String gender;
@SerializedName("ContactPreference")
public String contactPreference;
@SerializedName("RegistrationType")
public String registrationType;
@SerializedName("RegistrationMode")
public String registrationMode;
}
public static class REGCompanyService {
@SerializedName("ServiceID")
public Integer serviceID;
}
public static class REGCompanyHours {
@SerializedName("WeekDay")
public Integer weekDay;
@SerializedName("IsOpen")
public Integer isOpen;
@SerializedName("StartTime")
public Integer startTime;
@SerializedName("CloseTime")
public Integer closeTime;
}
public static class REGCompanyAddress {
@SerializedName("AddressType")
public String addressType;
@SerializedName("Address1")
public String address1;
@SerializedName("Address2")
public String address2;
@SerializedName("StreetName")
public String streetName;
@SerializedName("CompanyCity")
public String companyCity;
@SerializedName("CompanyState")
public String companyState;
@SerializedName("PostalCode")
public String postalCode;
@SerializedName("Longitude")
public String longitude;
@SerializedName("Latitude")
public String latitude;
}
public static class REGCompany {
@SerializedName("ReferenceCode")
public String referenceCode;
@SerializedName("CompanyType")
public String companyType;
@SerializedName("CompanyName")
public String companyName;
@SerializedName("CompanyEmailAddress")
public String companyEmailAddress;
@SerializedName("CompanyTier")
public String companyTier;
@SerializedName("ContactNumber")
public String contactNumber;
}
}
然后我尝试 post 使用 retrofit2 的数据,如下所示
final TradePreRegistrationRequest registrationRequest = new TradePreRegistrationRequest();
registrationRequest.regMember.userID = "Text";
registrationRequest.regMember.userPassword = mBinding.password.getText().toString();
registrationRequest.regMember.salutation = mBinding.salutation.getText().toString();
registrationRequest.regMember.emailAddress = mBinding.email.getText().toString();
registrationRequest.regMember.firstName = mBinding.fullName.getText().toString();
registrationRequest.regMember.lastName = mBinding.fullName.getText().toString();
registrationRequest.regMember.mobileNumber = mBinding.mobileNumber.getText().toString();
registrationRequest.regMember.gender = "1";
registrationRequest.regMember.contactPreference = "1";
registrationRequest.regMember.registrationType = "1";
registrationRequest.regMember.registrationMode = "1";
registrationRequest.regCompany.companyType = "1";
registrationRequest.regCompany.companyName = mBinding.registeredName.getText().toString();
registrationRequest.regCompany.companyTier = "";
registrationRequest.regCompany.companyEmailAddress = mBinding.emailWork.getText().toString();
registrationRequest.regCompany.contactNumber = mBinding.workshopNumber.getText().toString();
registrationRequest.regCompany.referenceCode = mBinding.docRefCode.getText().toString();
registrationRequest.regCompanyAddress.address1 = mBinding.address1.getText().toString();
registrationRequest.regCompanyAddress.address2 = mBinding.address2.getText().toString();
registrationRequest.regCompanyAddress.addressType = "1";
registrationRequest.regCompanyAddress.companyCity = mBinding.town.getText().toString();
registrationRequest.regCompanyAddress.companyState = mBinding.state.getText().toString();
registrationRequest.regCompanyAddress.streetName = mBinding.address2.getText().toString();
registrationRequest.regCompanyAddress.postalCode = mBinding.postalCode.getText().toString();
registrationRequest.regCompanyAddress.latitude = "1";
registrationRequest.regCompanyAddress.longitude = "103";
Repository.apiService().dsrRegistration(registrationRequest).enqueue(new BaseCallback<CommonResponse>() {
@Override
public void onSuccess(CommonResponse body) {
super.onSuccess(body);
mBinding.progressBar.setVisibility(View.GONE);
if (body.responseCode != CommonResponse.Code.SUCCESS) {
PopupErrorDialog.newInstance(body.responseMessage.header, body.responseMessage.message, body.responseMessage.btnText1, null, null, null).show(getSupportFragmentManager(), "popup_error");
} else {
Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();
}
}
@Override
public void onFail(String title, String msg) {
super.onFail(title, msg);
mBinding.progressBar.setVisibility(View.GONE);
PopupErrorDialog.newInstance(title, title, "Try Again", null, null, null).show(getSupportFragmentManager(), "popup_error");
}
});
然后我收到这个错误
FATAL EXCEPTION: main
Process: com.edenred.shell, PID: 22666
java.lang.NullPointerException: Attempt to write to field 'java.lang.String .data.request.TradePreRegistrationRequest$REGMember.userID' on a null object reference
at com.edenred.shell.ui.registerdsr.RegistrationActivity.registration(RegistrationActivity.java:54)
at com.edenred.shell.ui.registerdsr.RegistrationActivity.lambda$onCreate(RegistrationActivity.java:38)
at com.edenred.shell.ui.registerdsr.RegistrationActivity$$Lambda.onClick(Unknown Source)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22429)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6121)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)
我需要设置数据以使用 TradePreRegistrationRequest class 将其发送到服务器。
这个错误请指教 提前致谢。
您的代码的问题是您在使用它们之前创建了 'TradePreRegistrationRequest' 的引用,而不是其字段的引用,您可以在使用它们之前创建每个字段的引用,或者您可以通过以下方式初始化它们默认是这样的:-
public class TradePreRegistrationRequest {
@SerializedName("REG_Company")
public REGCompany regCompany = new REGCompany();
@SerializedName("REG_CompanyAddress")
public REGCompanyAddress regCompanyAddress = new REGCompanyAddress();
@SerializedName("REG_CompanyService")
public List<REGCompanyService> regCompanyServices = new ArrayList<>();
@SerializedName("REG_CompanyHours")
public List<REGCompanyHours> regCompanyHourses = new ArrayList<>();
@SerializedName("REG_Member")
public REGMember regMember = new REGMember();
//**** your further code//
}