0

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)
16
  • First print log from value - getShubuh and check. Commented Nov 26, 2019 at 9:02
  • jadwalShubuh -- this edittext is null have you initialize this variable using findviewbyid?? Commented Nov 26, 2019 at 9:04
  • I have set Toast below String getShubuh, the result still an error like that Commented Nov 26, 2019 at 9:04
  • 2
    put your complete code here. That should help in resolving issue. As of now it seems to be you haven't initialized edit text using fineviewbyid. Commented Nov 26, 2019 at 9:06
  • @NehaRathore I have initialize by using findviewbyid, if i still write into the code i got an error the code too long from stackoverflow Commented Nov 26, 2019 at 9:06

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

This is my mistake, i have set wrong findViewById EditText. Thank you for helping me a lot
0

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)

1 Comment

This is my mistake, i have set wrong findViewById EditText. Thank you for helping me a lot

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.