CarinaMj CarinaMj - 1 year ago 76
Android Question

null object reference when I post the data to sever

I have created the serialized class for post the data to server using 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;

}


}

Then I tried to post the data using retrofit2 like below

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");

}
});


then I am getting this 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$1(RegistrationActivity.java:38)
at com.edenred.shell.ui.registerdsr.RegistrationActivity$$Lambda$4.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)


I need to set the data to send it to the server using the TradePreRegistrationRequest class.

Please advise me for this error
Thanks in advance.

Avi Avi
Answer Source

The problem with your code is you creating a reference of 'TradePreRegistrationRequest' but not the reference of its filed before using them, either you can create the reference of each field before using them or you can initialize them by default like this:-

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//
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download