NullPointerException when trying to retrieve ParseObject

I am using Parse to retrieve data from the cloud, but I am running into errors. I have two classes, ParseApplication and UserScreen. In ParseApplication I initialize the ParseObject “events” and add data to the cloud in a method called addEvent. In UserScreen, I want to retrieve that data to then put into a spinner with a method called retrieveEvents. Right now, if I do not call the addEvent method then the retrieveEvents does not work (ie. a NullPointerException is thrown referencing line starting with String name = myEvents.getString(“Name”) where Name is the name of column in the cloud). If I do call the addEvent method, the objects are retrieved successfully. I think it has something to do with the ParseObject events not being intitialized in the correct location.

I cannot find this error. Any help would be appreciated.

UserScreen class code with retrieveEvents

package com.parse.starter;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;

import com.parse.FindCallback;
import com.parse.GetCallback;
import com.parse.ParseAnalytics;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import com.parse.ParseUser;

import java.util.ArrayList;
import java.util.List;


public class UserScreen extends Activity
{
    ParseObject myEvents = ParseApplication.events;
    Spinner eventsList;
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.userscreen);
        ParseAnalytics.trackAppOpenedInBackground(getIntent());

        retrieveEvents();

        Button logOutBtn = (Button) findViewById(R.id.logOutBtn);
        logOutBtn.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View v)
            {
                ParseUser.logOut();
                Intent i = new Intent(getApplicationContext(), ParseStarterProjectActivity.class);
                startActivity(i);
            }
        });
    }

    private void retrieveEvents()
    {
        myEvents = new ParseObject("Events");
        eventsList = (Spinner) findViewById(R.id.eventSpinner);
        final List<String> list = new ArrayList<String>();

        ParseQuery<ParseObject> query = ParseQuery.getQuery("Events");
        query.whereEqualTo("Name", "Hello");
        query.findInBackground(new FindCallback<ParseObject>()
        {
            public void done(List<ParseObject> gameScore, ParseException e)
            {
                if (e == null)
                {
                    Log.d("It gets here", "It gets here into the good one");
                    String name = myEvents.getString("Name");
                    String date = myEvents.getString("Date");
                    String time = myEvents.getString("Time");
                    list.add(name);
                    list.add(date);
                    list.add(time);

                } else {
                    // something went wrong
                    Log.d("It gets here", "It gets here into the else");
                }
            }
        });

        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, list);
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        eventsList.setAdapter(dataAdapter);
    }


}

ParseApplication class with addEvent

package com.parse.starter;

import android.app.Application;
import android.content.Intent;
import android.util.Log;
import android.widget.Button;
import android.widget.Toast;

import com.parse.GetCallback;
import com.parse.Parse;
import com.parse.ParseACL;
import com.parse.ParseCrashReporting;
import com.parse.ParseException;
import com.parse.ParseInstallation;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import com.parse.ParseUser;
import com.parse.SignUpCallback;

public class ParseApplication extends Application
{

    public static ParseUser User;
    public static ParseObject events;
   // public static ParseObject events;
  @Override
  public void onCreate() {
      super.onCreate();
      // Initialize Crash Reporting.
      ParseCrashReporting.enable(this);

      // Enable Local Datastore.
      Parse.enableLocalDatastore(this);

      // Add your initialization code here
      Parse.initialize(this);

      ParseInstallation.getCurrentInstallation().saveInBackground();

      ParseUser.enableAutomaticUser();
      ParseACL defaultACL = new ParseACL();
      // Optionally enable public read access.
      // defaultACL.setPublicReadAccess(true);
      ParseACL.setDefaultACL(defaultACL, true);
      //Parse.initialize(this, "gseLUTQuDmqDquV3JnoVm5lvYpgcBMEshDr5RGVB", "XCsq30YAIJsQPYrjjLnqpZGRDfNGySUkMwIzRcbX");
      Parse.initialize(this, "nHRnejo301qPbnJs4dcF4bOV4JEyjLRJlNZ1Arjl", "hrj9vTGsXIPtUQglCZbPtFxxX1zMNZkinMYL4X0P");
  }

     public void doParse(String name, String password, String email, String phone)
     {

         User = new ParseUser();
         User.setUsername(name);
         User.setPassword(password);
         User.setEmail(email);

         // other fields can be set just like with ParseObject
         User.put("phone", phone);

         User.signUpInBackground(new SignUpCallback() {
             @Override
             public void done(ParseException e)
             {
                 if (e == null)
                 {
                     Intent i = new Intent(getApplicationContext(), UserScreen.class);
                     startActivity(i);
                     Toast toast = Toast.makeText(getApplicationContext(), "User signed in!", Toast.LENGTH_LONG);
                     toast.show();
                 }
                 else
                 {

                     Toast toast = Toast.makeText(getApplicationContext(), "Username or Email already taken/password too long", Toast.LENGTH_LONG);
                     toast.show();
                 }
             }
         });
     }
    public void addEvent(String Name, String Date, String Time)
    {

        events = new ParseObject("Events");
        events.put("Name", Name);
        events.put("Date", Date);
        events.put("Time", Time);
        events.saveInBackground();
        Log.d("It gets here", "It gets here");
    }



  }


Source: java

Leave a Reply