0

I get my data inside my function. However when trying to set the data to my textView it fails. It gives a null pointer exception. But I initialise my textView on top of the page. Anyone has an idea why it gives this error?

Process: com.example.dylangomes.parkingxl, PID: 10078
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.example.dylangomes.parkingxl.DetailFragment$1.onCallBackRecieved(DetailFragment.java:79)
at com.example.dylangomes.parkingxl.Services.ParkingService$3.onResponse(ParkingService.java:113)
at com.example.dylangomes.parkingxl.Services.ParkingService$3.onResponse(ParkingService.java:108)
at com.android.volley.toolbox.JsonRequest.deliverResponse(JsonRequest.java:65)
at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
at android.os.Handler.handleCallback(Handler.java:769)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6540)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

This is my function

public class DetailFragment extends Fragment {

private String titleText, addressText;
private int freePlacesText;
private TextView title, freeplaces, address;
private ParkingService parkserv;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View fragmentView = inflater.inflate(R.layout.fragment_detail, container, false);

    title = fragmentView.findViewById(R.id.title_view);
    freeplaces = fragmentView.findViewById(R.id.freePlaces);
    address = fragmentView.findViewById(R.id.address);

    return fragmentView;

}

public void setId(int id) {
    Log.d("Set id function called", "FUnction called");
    parkserv = new ParkingService(getActivity());

    parkserv.getParkingLotsById(id);

    parkserv.setApiCallBackObject(new APICallBackListenerObject() {

        @Override
        public void onCallBackRecieved(JSONObject jsonObject) {
            Log.d("Getting into OnCallback", "OncallBack");

            final Gson gson = new Gson();
            final Type type = new TypeToken<ParkingLots>() {
            }.getType();
            final ParkingLots parkinglots = gson.fromJson(jsonObject.toString(), type);

            Log.d("ParkingLots", parkinglots.getAddress());
            Log.d("ParkingLots", parkinglots + "");

            titleText = parkinglots.getTitle();
            freePlacesText = parkinglots.getPlacesNow();
            addressText = parkinglots.getAddress();

            title.setText(titleText);
            freeplaces.setText(freePlacesText);
            address.setText(addressText);
        }
    });
}

}

the logs log the data which i expect but it gives an error before i get inside the view and crashes the program

Dylan Gomes
  • 173
  • 12
  • @Marcin when i log Log.d("View", "View Created"); this inside my onCreateView i can see that before i see my error. This means it is created how comes that i get the nullpointerexeption than i really have no clue after reading the entire page – Dylan Gomes Nov 08 '17 at 20:05
  • `DetailFragment.java:79` – Marcin Orlowski Nov 08 '17 at 20:18
  • @MarcinOrlowski i know there is an issue on the title.setText() but i have no clue how to solve it i know the title does not excist here but why? And how can i make sure it does excust – Dylan Gomes Nov 08 '17 at 20:28

0 Answers0