6

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) 
11
  • Have you tried a normal for loop with counter instead of a foreach for loop? Something like for (int i = 0; i< team_memebers.size(); i++) { employeeSpinnerAdapter.add(team_memebers.get(i); } Commented Mar 2, 2016 at 20:10
  • is query.findInBackground in another thread... Commented Mar 2, 2016 at 20:11
  • @chalarangelo, Yes, it fails on .size(), as it appears team_members is null. Commented Mar 2, 2016 at 21:24
  • @Xoce, no, but I tried query.find() and got the same issue. Commented Mar 2, 2016 at 21:24
  • 1
    You're statement is that the NPE is on the for(ParseObject prso: users)? The way this code is written it should probably NPE on the for(String str: teammembers) as I don't see you waiting for the query.findInBackground() call to finish before doing that interation. Can you highlight what line the NPE actually occurs on? I can't tell which it is based on the stack as you have two for(iter) loops. Commented Mar 3, 2016 at 23:03

1 Answer 1

5

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
        }
    }
});
Sign up to request clarification or add additional context in comments.

6 Comments

I tried placing it in the 'query.findInBackground()' call block, but I get a message - "variable input is accessed within inner class; needs to be declared final"
On which variable do you get this message? str or team_memebers?
Anyways, I suggest you to not assign null to team_memebers, instead declare it final and initialise with empty list: private final List<String> team_memebers = new ArrayList<>(); and remove this line: team_memebers = new ArrayList<String>(usrs.size()); also declare str as final in your loop: for (final String str:team_memebers)
On 'empolyeeSpinnerAdapter'
Then declare str as final as I said.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.