Object always return null on restart of activity and on resume of fragment even when it’s constraints is been set as not null (nn).
Do you use a blocking or non-blocking API?
Blocking
Could you please share the code that makes the API call?
First I had made a custom class to save the current user to a response
// public class connection extends application {
Public static BackendlessUser currentUser;
}
On check if login is a valid login made to response equal to currentuser.
//Backendless.UserService.IsValidLogin(new AsyncCallback()){
@Override
Public void handleResponse(Boolean response){
If (response){
String objectId = UserIdStorageFactory.instance().getStorage().get();
Backendless.Data.of(BackendlessUser.class).findById, new AsyncCallback(){
On handle response
// name Of that custom class I created.
connection.currentuser = response
Some bunch of codes.
}
On handle fault
}
}
}
// now am at a fragment on an activity where user view their profile.
If (isnetworkavailable) {
String CloudUsersName = connection.currentuser.getProperty(“name”).tostring();
String CloudUsersQuote = connection.currentuser.getProperty(“quote”).tostring();
Textview usersName = findViewById(R.id.user_name);
Textview usersQuote = findViewById(R.id.user_quote);
usersName.setText(CloudUsersName);
usersQuote.setText(CloudUsersQuote);
}
On create it works fine. But on resume of fragment it crashs. Because the activity hosting the fragment is using a view pager then the fragments are been placed inside the viewpager so has to have three slides. Meaning I have three fragments. The first which is users profile view runs that code that shows users name and quote.
So when ever I start the app afresh it runs nice but when I slide to the third fragment it crashes saying getting the item is on a null object reference
I was hoping for something smaller. Can you narrow it down to specific line of API code which return null?
This part to be specific.
If (isnetworkavailable) {
String CloudUsersName = connection.currentuser.getProperty(“name”).tostring();
String CloudUsersQuote = connection.currentuser.getProperty(“quote”).tostring();
Textview usersName = findViewById(R.id.user_name);
Textview usersQuote = findViewById(R.id.user_quote);
usersName.setText(CloudUsersName);
usersQuote.setText(CloudUsersQuote);
}
I think u get the picture more if u just try to skim the latter code part.
I do not see any Backendless APIs in that block of code.
Have you tried debugging the code?
Btw, this is non-blocking API call. The response will be delivered to a method in the AsyncCallback object:
So you saying that the reason for the crash?
I do not know what the reason for the crash is. You need to debug the code to understand where things go in an unexpected direction.
If you’re thinking the code is blocking, that is wrong based on the code you shared. A non-blocking API will return result in a separate thread, so if you continue stepping through line-by-line in a debugger, you will not even see where the result comes back (hint: you need to set a breakpoint in the AsyncCallback response handler).
Will get back regarding this later. Thank for the insight by the way.