Async Retrieving User NullPointer Problem


I just implemented Login-Logout functionality and now tryin to handle already logged in user after relunch app.
I have problem with fetching UserData from DB for Auth loggedUser. When i start Activity i’m checking if AccesToken exist. If it’s exist i’m fetching Profile via AccesToken. After that i’m trying to use Async find with dataQuery. Code looks like that :

AccessToken accessToken = AccessToken.getCurrentAccessToken();
if (accessToken != null && !accessToken.equals("") ){
    final ProgressDialog pg = new ProgressDialog(this).show(this, "Loading... ", "Loading..." );
    String whereClause = " name = '" + Profile.getCurrentProfile().getName() + "'";
    BackendlessDataQuery dataQuery = new BackendlessDataQuery().newInstance();
    Backendless.Data.of(BackendlessUser.class).find(dataQuery, new AsyncCallback<BackendlessCollection<BackendlessUser>>() {
        public void handleResponse(BackendlessCollection<BackendlessUser> backendlessUserBackendlessCollection) {
            Iterator<BackendlessUser> iterator = backendlessUserBackendlessCollection.getCurrentPage().iterator();
                BackendlessUser user =;
                Log.i("User fetched", user.getEmail());

        public void handleFault(BackendlessFault backendlessFault) {
            Toast.makeText(LoginActivity.this, backendlessFault.getMessage(), Toast.LENGTH_LONG).show();
    if (Backendless.UserService.CurrentUser() != null ){
        Log.i("User", "Email: " + Backendless.UserService.CurrentUser().getEmail());
    } else {
        Log.e("NullPointer", "CurrentUser is null");

In handleResponse i have fetched user but when i’m checking out of handleResponse CurrentUser it’s null. It’s becouse i’m using async. U also can notice i have one commeted line. I tried to use sync function to fetch this data but it’s throw android.os.NetworkOnMainThreadException error even if i’m doing it in LoginActivity instead MainActivity. Any clue how to handle this problem? Below attached sync method.

public static BackendlessUser getCurrentUserByName(String name ) {
    String whereClause = " name = '" + name + "'";
    Log.i("whereClause", whereClause);
    BackendlessDataQuery dataQuery = new BackendlessDataQuery();
    try {
        BackendlessCollection<BackendlessUser> users = Backendless.Data.of(BackendlessUser.class).find();
        Iterator<BackendlessUser> iterator = users.getCurrentPage().iterator();
        while (iterator.hasNext()) {
            BackendlessUser user =;
            System.out.println("User: " +user.getEmail());
            return user;
    } catch (BackendlessException e){
        Log.e("DBCRUDError", "" + e.getDetail());

    } finally {
        return null;



Hi Łukasz,

The CurrentUser() is not initialized automatically when you login, only the user-token and the user-id do. You can check our Code Generation Login sample which does almost the same you’re trying to accomplish.

As to the android.os.NetworkOnMainThreadException, different activity does not mean different thread, so in your LoginActivity you’re still on the main thread. That’s why you receive an error.

Thank you Sergey.