Reputation: 451
I want to send some string to another application, but I've got an Error is Null Pointer when I click buttonSave.
I have set Toast, the result is still an error. Is there another way to get the value in EditText
?
Code :
@Nullable
@Override
public View onCreateView(....) {
View view = ....;
jadwalShubuh = view.findViewById(R.id.shubuhEditText);
buttonSave = view.findViewById(R.id.saveButton);
buttonSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Error : Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
String getShubuh = jadwalShubuh.getText().toString().trim();
Intent moveString = new Intent("com.package.my.application");
moveString.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
moveString.putExtra("getShubuh", getShubuh);
startActivity(moveString);
}
});
actionLoad();
}
private void actionLoad() {
// Call method findLocation when location != null
findCityCountryName(location);
}
private void findCityCountryName(Location location) {
// Another String to setCityName and setCountryName
callingAPI(cityName, countryName);
}
private void callingAPI(String cityName, String countryName) {
// Calling API code
call.enqueue(new Callback<JsonObject>() {
@Override
public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
if (dateJSON.getString("readable").contentEquals(getMonthReadable)) {
String setShubuh = timingsJSON.getString("Fajr");
jadwalShubuh.setText(setShubuh);
}
});
}
Logcat :
java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
at com.package.my.application.pengaturanWaktu.sendData(pengaturanWaktu.java:250)
at com.package.my.application.pengaturanWaktu.access$000(pengaturanWaktu.java:52)
at com.package.my.application.pengaturanWaktu$1.onClick(pengaturanWaktu.java:94)
Upvotes: 0
Views: 91
Reputation: 1257
When you're setting the Extra the value hasn't been set, as you're making the API call later than the initialization.
A quick fix could be to set the getShubuh String assignation after getting the value from the API :
if (dateJSON.getString("readable").contentEquals(getMonthReadable)) {
String setShubuh = timingsJSON.getString("Fajr");
jadwalShubuh.setText(setShubuh);
getShubuh = setShubuh.trim();
}
And declare the getShubuh String as to be accessible to both methods, as you did with jadwalShubuh.
Upvotes: 1
Reputation: 830
your EditText refrence is null , make sure the id in R.id.shubuhEditText
is correct , and you also have to cast it to EditText as jadwalShubuh = (EditText) view.findViewById(R.id.shubuhEditText)
Upvotes: 0