I am attempting on creating an app on android studio for college, but I've been finding too many issues that don't make much sense to me.
I have a static object that I want to be called throughout the application. If I turn on my laptop and run the app, it works just fine. I fetch the data and set the text view with it.
So, If I run the app for the first time, I get this result (which is the intended):
However, If I rerun the app, I get this error:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.cct.group010.finalproject, PID: 8003
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.cct.group010.finalproject/com.cct.group010.finalproject.ui.MainMenu}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.time.LocalDate.toString()' on a null object reference
It works again if I wipe the data of the emulator and run the app after, though.
Also, If I set the textview text to be set after the user clicks on the "click here for more info", it works just fine. I just have this recurrent issue when I do it outside of this clickListener.
My code:
public class MainMenu extends AppCompatActivity {
private TextView reservationMoreInfo, reservationStatus,reservationDates;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE); //will hide the title
setContentView(R.layout.activity_main_menu);
reservationMoreInfo = (TextView) findViewById(R.id.reservationMoreInfo);
reservationStatus = (TextView) findViewById(R.id.reservationStatus);
reservationDates = (TextView) findViewById(R.id.reservationDates);
reservationStatus.setText(ImportantObjects.guest.getNextReservation().getReservationStatus());
String date = ImportantObjects.guest.getNextReservation().getCheckin().toString() + " -\n "
+ ImportantObjects.guest.getNextReservation().getCheckout().toString();
reservationDates.setText(date);
reservationMoreInfo.setOnClickListener(view -> {
});
}
}
I am creating these static objects from api calls after the user logs in. The data is there, I tested before intenting to the new screen. And also, as I said before, sometimes it fetches the data, sometimes it doesnt and it always does if it's within the clickListener.
Does anyone have any idea of what might've been causing it?
(Nevermind the ugly interface. I'm doing it as simple as possible so I can focus on the logic).