-2

So i have a Main Activity that has by default the MainPage Fragment , When navigating to LoginPage Fragment using Navigation Drawer and promoting a valid username and password for Parse and then clicking a button to return to the MainPage Fragment ;gives this error :

java.lang.NullPointerException: Attempt to invoke interface method 'java.util.Iterator java.util.List.iterator()' on a null object reference

this following code when clicking the Button to submit values:

MainPage mainPage = new MainPage();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.FragmentContainer, mainPage);
fragmentTransaction.commit();

So it gives me an Error on these lines of codes that it ran before when first launching the MainPage Fragment:

**(Gives Error Here)**query.findInBackground(new FindCallback<ParseObject>() {
        @Override
        public void done(List<ParseObject> objects, ParseException e) {
            ArrayList<String> names = new ArrayList<String>();
            if (e != null) {
                Toast.makeText(getContext(), "Error", Toast.LENGTH_SHORT).show();
            }
           **(Gives Error Here)**  for (ParseObject Obj1 : objects) {
                names.add(Obj1.get("Name").toString());
            }

            ArrayAdapter<String> arrayadapter = new ArrayAdapter<String>(getContext(), R.layout.items, names);
            ListView listview = (ListView) getActivity().findViewById(R.id.mainPageListView);
            listview.setAdapter(arrayadapter);

        }
    });
Flava
  • 77
  • 1
  • 8
  • Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Mohammed Aouf Zouag Nov 29 '15 at 20:14
  • But why it's null ? And i tried to check if it was null using if statement , but it wasn't . – Flava Nov 29 '15 at 20:19

2 Answers2

1

Ok Solved by adding this line in the main fragment page :

View view = inflator.inflate(R.layout.fragmentMain,container,false);

Changed the current return to : return view;

Damian Kozlak
  • 7,065
  • 10
  • 45
  • 51
Flava
  • 77
  • 1
  • 8
0

You can retrieve a Fragment by tag.

Fragment fragment = getFragmentManager().findFragmentByTag("stringtag");

But you have to define it when you call the replace:

fragmentTransaction.replace(R.id.FragmentContainer, mainPage, "stringtag");
MiguelCatalan
  • 916
  • 10
  • 26