-2

I tried to update the data, the API was running in Postman, but when I ran the application in Android Studio, the message "Unfortunately, driverApplication has stopped" appears. Does anyone know the solution? Thank you.

Error:

W/art: Before Android 4.1, method int androidx.appcompat.widget.DropDownListView.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
E/RecyclerView: No adapter attached; skipping layout
I/ListPopupWindow: Could not find method setEpicenterBounds(Rect) on PopupWindow. Oh well.
V/RenderScript: 0xbe134c00 Launching thread(s), CPUs 2
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.driverapplication, PID: 5607
    java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.example.driverapplication.model.changeStatus.ChangeStatus.getMessage()' on a null object reference
        at com.example.driverapplication.activity.driver.ChangeStatusActivityDriver$2.onResponse(ChangeStatusActivityDriver.java:105)
        at retrofit2.DefaultCallAdapterFactory$ExecutorCallbackCall$1$1.run(DefaultCallAdapterFactory.java:83)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5307)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
I/Process: Sending signal. PID: 5607 SIG: 9

ApiInterface :

@FormUrlEncoded
    @POST("changeStatus")
    Call<ChangeStatus> changeStatusResponse(
            @Field("id_order") String id_order,
            @Field("id_trip") String id_trip,
            @Field("id_seat") String id_seat,
            @Field("status") Integer status
    );

Model (ChangeStatus.java)

public class ChangeStatus{

    @SerializedName("data")
    private ChangeStatusData changeStatusData;

    @SerializedName("message")
    private String message;

    @SerializedName("status")
    private boolean status;

    public void setChangeStatusData(ChangeStatusData changeStatusData){
        this.changeStatusData = changeStatusData;
    }

    public ChangeStatusData getChangeStatusData(){
        return changeStatusData;
    }

    public void setMessage(String message){
        this.message = message;
    }

    public String getMessage(){
        return message;
    }

    public void setStatus(boolean status){
        this.status = status;
    }

    public boolean isStatus(){
        return status;
    }
}

Model (ChangeStatusData.java)

public class ChangeStatusData {

    @SerializedName("id_seat")
    private String idSeat;

    @SerializedName("id_trip")
    private String idTrip;

    @SerializedName("id_order")
    private String idOrder;

    @SerializedName("status")
    private Integer status;

    public void setIdSeat(String idSeat){
        this.idSeat = idSeat;
    }

    public String getIdSeat(){
        return idSeat;
    }

    public void setIdTrip(String idTrip){
        this.idTrip = idTrip;
    }

    public String getIdTrip(){
        return idTrip;
    }

    public void setStatus(Integer status){
        this.status = status;
    }

    public Integer getStatus(){
        return status;
    }
}

Activity (ChangeStatusActivityDriver.java):

public class ChangeStatusActivityDriver extends AppCompatActivity {
    public static final String EXTRA_CHANGE_STATUS_DRIVER = "extra_change_status_driver";

    TextView tvName;
    Spinner spinnerStatus;
    Button btnUpdate;
    String Name, Selected, idOrder, idTrip, idSeat, statusString;
    Integer Status;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_status_driver);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        getSupportActionBar().setTitle("Edit Status by Driver");

        DetailTripDriverData detailTripDriverData = getIntent().getParcelableExtra(EXTRA_CHANGE_STATUS_DRIVER);
        Name = detailTripDriverData.getPassengerName();
        statusString = detailTripDriverData.getStatus();
        idOrder = detailTripDriverData.getIdOrder();
        idTrip = detailTripDriverData.getIdTrip();
        idSeat = detailTripDriverData.getIdSeat();

        tvName = findViewById(R.id.tv_name_driver);
        tvName.setText(Name);

        spinnerStatus = findViewById(R.id.spinner_status_driver);
        btnUpdate = findViewById(R.id.btn_update_driver);

        btnUpdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Selected = spinnerStatus.getSelectedItem().toString();
                switch (Selected){
                    case "Booking":
                        Status = 1;
                        break;
                    case "Picked Up":
                        Status = 2;
                        break;
                    case "On Going":
                        Status = 3;
                        break;
                    case "Arrived":
                        Status = 4;
                        break;
                    case "Cancelled":
                        Status = 5;
                        break;
                }
                Toast.makeText(ChangeStatusActivityDriver.this, "Status = " +Selected+ " | Code " +Status, Toast.LENGTH_SHORT).show();
                changeStatus();
            }
        });

    }

    @Override
    public boolean onSupportNavigateUp() {
        onBackPressed();
        return true;
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

    private void changeStatus() {
        ApiInterface apiInterface = ApiClient.getClient().create(ApiInterface.class);
        Call<ChangeStatus> changeStatusCall = apiInterface.changeStatusResponse(idOrder, idTrip, idSeat, Status);
        changeStatusCall.enqueue(new Callback<ChangeStatus>() {
            @Override
            public void onResponse(Call<ChangeStatus> call, Response<ChangeStatus> response) {
                if (response.body() != null && response.isSuccessful() && response.body().isStatus()){
                    Toast.makeText(ChangeStatusActivityDriver.this, response.body().getMessage(), Toast.LENGTH_SHORT).show();
                    Intent intentBackDriver = new Intent(ChangeStatusActivityDriver.this, DetailTripDriver.class);
                    startActivity(intentBackDriver);
                    finish();
                } else {
                    Toast.makeText(ChangeStatusActivityDriver.this, response.body().getMessage(), Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<ChangeStatus> call, Throwable t) {
                Toast.makeText(ChangeStatusActivityDriver.this, t.getLocalizedMessage(), Toast.LENGTH_SHORT).show();

            }
        });
    }
}

Layout (activitychangestatus_driver.xml)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".activity.driver.ChangeStatusActivityDriver">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white"
        android:layout_below="@+id/title">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="30dp"
            android:layout_marginRight="30dp"
            android:layout_marginTop="30dp"
            android:orientation="vertical">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textColor="@color/headings"
                    android:text="Name"
                    android:textSize="13sp"
                    android:layout_marginBottom="10dp"/>

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/tv_name_driver"
                    android:fontFamily="sans-serif-light"
                    android:text="Passenger One"
                    android:textColor="#000000"
                    android:textSize="18sp" />

                <View
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:layout_marginTop="5dp"
                    android:background="@color/grey" />

            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="15dp"
                android:orientation="vertical">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Status"
                    android:textColor="@color/headings"
                    android:textSize="13sp"
                    android:layout_marginBottom="10dp"/>

                <Spinner
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:fontFamily="sans-serif-light"
                    android:id="@+id/spinner_status_driver"
                    android:textSize="18sp"
                    android:entries="@array/array_status"/>

            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="15dp"
                android:orientation="vertical">

                <Button
                    android:id="@+id/btn_update_driver"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="5dp"
                    android:background="@drawable/button_rounded"
                    android:fontFamily="sans-serif-light"
                    android:layout_gravity="center"
                    android:paddingLeft="10dp"
                    android:paddingRight="10dp"
                    android:text="Update"
                    android:textColor="@color/white"
                    android:textStyle="bold" />

            </LinearLayout>

        </LinearLayout>

    </RelativeLayout>


</RelativeLayout>
beginner
  • 33
  • 1
  • 11
  • `if (response.body() != null ...) {...} else { ... response.body().getMessage() ... }` seriously you are asking why? – Selvin Feb 04 '21 at 15:19

1 Answers1

0

You're trying to get a message while the body is null. Add null check before access that

if (response.body() != null) {
    Toast.makeText(ChangeStatusActivityDriver.this, response.body().getMessage(),Toast.LENGTH_SHORT).show();
}
M.Muzammil
  • 643
  • 9
  • 18