Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - null object reference when iterating List

I'm trying to copy a List 'usrs' which is created in an Inner class to a different list 'team_memebers'. After copying I try to iterate 'team_memebers' in the FOR loop, but I get a 'null object reference' error. The 'users' list contains the returned objects, tested via debug prints.

public class ListNodeActivity extends AppCompatActivity
{
    private ParseObject parse_task=null;
    private List<String> team_memebers=null;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_node);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

    query.findInBackground(new FindCallback<ParseObject>() {
        public void done(List<ParseObject> usrs, ParseException e) {
            if (e == null) {

                team_memebers = new ArrayList<String>(usrs.size());
                for (ParseObject prso:usrs) {
                    team_memebers.add(new String(prso.getString("Username")));
                }
            } else {//handle the error
            }
        }
    });

    for (String str:team_memebers)
    {
        empolyeeSpinnerAdapter.add(str);
    }
}

Stack trace

 FATAL EXCEPTION: main
 Process: il.ac.shenkar.david.todolistex2, PID: 14490
 java.lang.RuntimeException: Unable to start activity ComponentInfo{il.ac.shenkar.david.todolistex2/il.ac.shenkar.david.todolistex2.ListNodeActivity}: java.lang.NullPointerException: Attempt to invoke interface method 'java.util.Iterator java.util.List.iterator()' on a null object reference
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
     at android.app.ActivityThread.-wrap11(ActivityThread.java)
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
     at android.os.Handler.dispatchMessage(Handler.java:102)
     at android.os.Looper.loop(Looper.java:148)
     at android.app.ActivityThread.main(ActivityThread.java:5417)
     at java.lang.reflect.Method.invoke(Native Method)
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
  Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'java.util.Iterator java.util.List.iterator()' on a null object reference
     at il.ac.shenkar.david.todolistex2.ListNodeActivity.onCreate(ListNodeActivity.java:116)
     at android.app.Activity.performCreate(Activity.java:6251)
     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
     at android.app.ActivityThread.-wrap11(ActivityThread.java) 
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
     at android.os.Handler.dispatchMessage(Handler.java:102) 
     at android.os.Looper.loop(Looper.java:148) 
     at android.app.ActivityThread.main(ActivityThread.java:5417) 
     at java.lang.reflect.Method.invoke(Native Method) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
like image 436
David Faizulaev Avatar asked Mar 02 '16 20:03

David Faizulaev


People also ask

What is list iterator in Java?

ListIterator is an iterator is a java which is available since the 1.2 version. It allows us to iterate elements one-by-one from a List implemented object. It is used to iterator over a list using while loop.

How to iterate over a list using while loop in Java?

Each element in the list can be accessed using iterator with a while loop. ListIterator is an iterator is a java which is available since the 1.2 version. It allows us to iterate elements one-by-one from a List implemented object. It is used to iterator over a list using while loop.

What happens when ACF array has null value in JSON?

I am creating and an App using Retrofit and json from my wordpress and my some array objects which having acf array has null value.. whenever that null value fetch then the activity close and previous activity opens..

How do you iterate through a list in Python?

Iterating over a list can also be achieved using a while loop. The block of code inside the loop executes until the condition is true. A loop variable can be used as an index to access each element. Syntax: while(variable<list_name.size()) { // Block of code to be executed }


1 Answers

Your problem is that you aren't iterating after copying list. query.findInBackground() is an async callback and therefore, isn't executed immediately. Because your iteration loop is PLACED below that callback, doesn't mean it will be executed after callback executes. Just put your loop inside callback like this:

query.findInBackground(new FindCallback<ParseObject>() {
        public void done(List<ParseObject> usrs, ParseException e) {
            if (e == null) {

            team_memebers = new ArrayList<String>(usrs.size());
            for (ParseObject prso:usrs) {
                team_memebers.add(new String(prso.getString("Username")));
            }
            for (String str:team_memebers)
            {
                empolyeeSpinnerAdapter.add(str);
            }
        } else {//handle the error
        }
    }
});
like image 108
Mirza Brunjadze Avatar answered Oct 01 '22 20:10

Mirza Brunjadze