0

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):

enter image description here

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).

Mvfm
  • 31
  • 4
  • 2
    See https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this ... Android gives you ways to understand what is happening. So you probably want to add logging code to your app, to better understand where exactly breaks. – GhostCat Mar 26 '22 at 12:05
  • 1
    Having said that, this is rather clear: `ImportantObjects.guest.getNextReservation().getCheckin()` in the context you are describing, that method returns null, thus to subsequent call to toString gives you that NPE. So, guessing: your code makes certain assumptions about the presence of this or data object/data structure. And your assumptions do not match to what happens when your app is restarted, or maybe resumed after suspended ... sth like that. – GhostCat Mar 26 '22 at 12:06
  • 1
    use try catch on the methods generating this error and handle it accordingly – Haseeb Hassan Asif Mar 26 '22 at 21:17

0 Answers0