Search code examples
javaandroidlistviewlistadapter

Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference


I've been looking for a similar problem to mine in order to find a solution, but I seriously couldn't find anything like that.

I was trying to download from parse an array of posts with an asynctask class, and after it gets the posts, it suppose to set the posts array in my page, and perform the setAdapter function in order to set my new posts array.

the problem is, after I've initialized listView and listAdapter in my home fragment,and then I perform the postArray taking from parse function, after it finishes taking the posts array from parse, it cannot update listAdapter because it says the listAdapter and my listView "haven't initialized yet", even though they have.

p.s. sorry for not posting my code in a convenient way, I don't tend to post my code problems that often.

here's my code:

my home fragment:

public class HomeFragment extends Fragment {
View root;
ArrayList<PostClass> postsArrayList = new ArrayList<>();

static boolean isPostsArrayUpdated = false;

ListAdapter listAdapter;
PullToRefreshListView listView;

public void updatePostsArrayList(ArrayList<PostClass> postsArrayList){
    if(!isPostsArrayUpdated){
        // First time updating posts array list
        listAdapter = new ListAdapter(getActivity(), root);
        listView = (PullToRefreshListView) root.findViewById(R.id.list_container);

        this.postsArrayList = postsArrayList;
        listView.setAdapter(listAdapter);
        isPostsArrayUpdated = true;
        root.findViewById(R.id.homeFragmentLoadingPanel).setVisibility(View.GONE);
    }else{
        // Have updated posts before
        this.postsArrayList = postsArrayList;
        listAdapter.notifyDataSetChanged();
    }
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    root = inflater.inflate(R.layout.fragment_home, container, false);
    listView = (PullToRefreshListView) root.findViewById(R.id.list_container);
    listAdapter = new ListAdapter(getActivity(), root);

    Home_Model.getInstance().setPostsArrayList();

    return root;
}

public class ListAdapter extends BaseAdapter implements View.OnClickListener{//....}

my home model:

public class Home_Model {

Home_Model(){}

static final Home_Model instance = new Home_Model();

public static Home_Model getInstance() {
    return instance;
}

public void setPostsArrayList(){
    new setHomePostsArray().execute();
}

public class setHomePostsArray extends AsyncTask<Void, ArrayList<PostClass>, Void>{

    ArrayList<String> followersList;
    ArrayList<PostClass> postsArrayList;

    @Override
    protected Void doInBackground(Void... params) {

        // Getting posts from parse
        String userName = Parse_model.getInstance().getUserClass().get_userName();
        followersList = Parse_model.getInstance().getFollowersByUserNameToString(userName);
        followersList.add(userName);


        postsArrayList = Parse_model.getInstance().getAllUsersPostsByFollowings(followersList);

        for (PostClass currPost : postsArrayList) {
            for (PostClass currLocalDBPost : LocalDBPostsArray) {
                if (currPost.getObjectID().equals(currLocalDBPost.getObjectID())) {
                    currPost.set_postPicture(currLocalDBPost.get_postPicture());

                }
            }
        }
        //Updating home page
        onProgressUpdate(postsArrayList);


        // Updating local data base in new posts
        //checking in local DB if there are any new posts from parse and update them
        for (PostClass currPost : postsArrayList) {
            boolean isPostExists = false;
            for (PostClass currLocalPost : LocalDBPostsArray) {
                if (currPost.getObjectID().equals(currLocalPost.getObjectID())) {
                    isPostExists = true;
                }
            }
            if (!isPostExists) {
                ModelSql.getInstance().addPost(currPost);
                Log.e("post not exist", "adding local DB");
            }
        }

        //updating followers list in local DB
        Parse_model.getInstance().getUserClass().setFollowersArray(followersList);
        ModelSql.getInstance().updateFollowersArray(currUser);

        return null;
    }

    @Override
    protected void onProgressUpdate(ArrayList<PostClass>... values) {
        //pass the updated postsArrayList to home fragment
        if(setPostsInHomePageDelegate!= null){
            setPostsInHomePageDelegate.setPosts(values[0]);
        }
    }
}


public interface SetPostsInHomePage {
    public void setPosts(ArrayList<PostClass> postsArrayList);
}

SetPostsInHomePage setPostsInHomePageDelegate;

public void setSetPostsInHomePageDelegate(SetPostsInHomePage setPostsInHomePageDelegate) {
    this.setPostsInHomePageDelegate = setPostsInHomePageDelegate;
}

main activity:

public class MainActivity extends Activity {

static HomeFragment homeFragment = new HomeFragment();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

// the home fragment has already been opened during the app opening 

//...

setPostsImHomePage();
 }

//...

public void setPostsImHomePage(){
    Home_Model.getInstance().setSetPostsInHomePageDelegate(new Home_Model.SetPostsInHomePage() {
        @Override
        public void setPosts(ArrayList<PostClass> postsArrayList) {
            homeFragment.updatePostsArrayList(postsArrayList);
        }
    });
}

}


Solution

  • Try to move your method setPostsImHomePage(...) from MainActivity to HomeFragmentand call it in OnCreateView before return root;.