2

I'm trying to retrieve a information from a parse object but the strings I store them in keep equaling null.

Here is how I saved the object

        // get current user
        ParseObject studentClasses = new ParseObject("StudentClasses");

        // register their periods into database
        studentClasses.put("student_id", ParseUser.getCurrentUser());
        studentClasses.put("first_period", ClassSelected_Period[PERIOD1]);
        studentClasses.put("second_period", ClassSelected_Period[PERIOD2]);
        studentClasses.put("third_period", ClassSelected_Period[PERIOD3]);
        studentClasses.put("fourth_period", ClassSelected_Period[PERIOD4]);
        studentClasses.put("fifth_period", ClassSelected_Period[PERIOD5]);
        studentClasses.put("sixth_period", ClassSelected_Period[PERIOD6]);
        studentClasses.put("seventh_period", ClassSelected_Period[PERIOD7]);

        // save the information into database
        studentClasses.saveInBackground();

It saves it perfectly fine my database. The student_id is a pointer to the user and the rest of the columns are strings.

I want to retrieve all those strings and put them in an array when I query parse for them it doesn't work

Here is my query

        // check if a user is not cached
        ParseUser currentUser = ParseUser.getCurrentUser();
        if (currentUser == null)
        {
            // prompt user to Register screen
            // create intent to start activity
            Intent intent = new Intent(MainActivity.this, RegisterActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

            // start new activity
            startActivity(intent);

            // stop current activity
            finish();
        }

        // query database for user's classes
        ParseQuery<ParseObject> query = ParseQuery.getQuery("StudentClasses");
        query.whereEqualTo("student_id", ParseUser.getCurrentUser());

        query.getFirstInBackground(new GetCallback<ParseObject>()
        {
            @Override
            public void done (ParseObject parseObject, ParseException e)
            {
                if (e == null)
                {
                    // retrieved the object
                    userClasses[PERIOD1] = parseObject.getString("first_period");
                    userClasses[PERIOD2] = parseObject.getString("second_period");
                    userClasses[PERIOD3] = parseObject.getString("third_period");
                    userClasses[PERIOD4] = parseObject.getString("fourth_period");
                    userClasses[PERIOD5] = parseObject.getString("fifth_period");
                    userClasses[PERIOD6] = parseObject.getString("sixth_period");
                    userClasses[PERIOD7] = parseObject.getString("seventh_period");
                }
                else
                {
                    // failed lookup. Do something here
                    Toast.makeText(getApplicationContext(),"Exception Thrown" ,Toast.LENGTH_SHORT).show();
                }
            }
        });

I looked at the parse docs and it looks like it should work but it doesn't save the strings

Any help or comments is appreciated thanks!

EDIT: I showed more of my code for the query part to show there is a current user

3
  • What happens when you step into it with your debugger? Commented Jul 2, 2015 at 0:45
  • imgur.com/X70KOVG Commented Jul 2, 2015 at 0:50
  • What? You have if (parseObject == null) and then you all methods on parseObject in the body. You should be getting an NPE there. Commented Jul 2, 2015 at 2:31

4 Answers 4

2

Here i am assuming that, there is data stored in your parse database.. and it works fine.. so to retrieve it try following..

 ParseQuery<ParseObject> query = ParseQuery.getQuery("StudentClasses");
    query.whereEqualTo("student_id", ParseUser.getCurrentUser());

    query.findInBackground(new FindCallback<ParseObject>()
    {
        @Override
        public void done (List<ParseObject> list, ParseException e)
        {
            if (e == null)
            {
               for(int i=0; i < list.size(); i++) {
                userClasses[PERIOD1] = list.get(i).getString("first_period");
                userClasses[PERIOD2] = list.get(i).getString("second_period");
                userClasses[PERIOD3] = list.get(i).getString("third_period");
                userClasses[PERIOD4] = list.get(i).getString("fourth_period");
                userClasses[PERIOD5] = list.get(i).getString("fifth_period");
                userClasses[PERIOD6] = list.get(i).getString("sixth_period");
                userClasses[PERIOD7] = list.get(i).getString("seventh_period");
               }
            }
            else
            {
              Toast.makeText(getApplicationContext(),"Exception Thrown" ,Toast.LENGTH_SHORT).show();
            }
        }
    });

hope it helps!

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for the help and the only changes I see is the toast after the else. I tried that and it did indeed show execption thrown. Now what that exception is, is still a mystery to me. As you see I edited my original post to show that I have a user that is cached and that user has a class that is StudentClasses
Its not the only change .. try to look more closely.. and try !! and if there is exception then let me know what is that exception..i edited it after ..
In your question, your query specifies query.getFirstInBackground ... which gets only one result.. which is wrong.. i changed it..
In the de-bugger I have the program stopped at line 152 and the userClasses variable is still null for all parts in the array. i.imgur.com/kBenbnJ.png
you actually need to understand.. this is the query you need.. .. i am not sure.. what is this userClasses[period] comes from and what type of it.. .. the logic is simple.. parseObject is row.. in one row im getting getString(" ") value.. it works fine.. you just need to store it somewhere and then transfer.. hope you understand.... also log in your parse app.. and see data that.. data is there or not .. and during transfer of data .. it should have same type.
|
0

Well try when getting content to use the function

parseObject.get("the_name_of_the_column")

Which actually should work and works fine for me

13 Comments

I replaced the original userClasses[PERIOD1] = parseObject.getString("first_period"); with userClasses[PERIOD1] = parseObject.get("first_period").getString(); It still doesn't work
Dont add getString in the end
Add insted toString() function
This will change the data inside the object to string and hopefully will work
But the problem is that get() returns a Parseobject. I still need to convert it into a string
|
0

Here what you need to understand is in your code studentClasses.saveInBackground(); is async call and you need to query inside SaveCallback. Then you can assure that the saved data is in the database when your query runs. You got null data because you query before the data is saved in parse.

 ParseObject studentClasses = new ParseObject("StudentClasses");
            // register their periods into database
            studentClasses.put("student_id", ParseUser.getCurrentUser());
            studentClasses.put("first_period", ClassSelected_Period[PERIOD1]);
            studentClasses.put("second_period", ClassSelected_Period[PERIOD2]);
            studentClasses.put("third_period", ClassSelected_Period[PERIOD3]);
            studentClasses.put("fourth_period", ClassSelected_Period[PERIOD4]);
            studentClasses.put("fifth_period", ClassSelected_Period[PERIOD5]);
            studentClasses.put("sixth_period", ClassSelected_Period[PERIOD6]);
            studentClasses.put("seventh_period", ClassSelected_Period[PERIOD7]);

            // save the information into database
           studentClasses.saveInBackground(new SaveCallback() {
       public void done(ParseException e) {
         if (e == null) {
           ParseQuery<ParseObject> query = ParseQuery.getQuery("StudentClasses");
           query.whereEqualTo("student_id", ParseUser.getCurrentUser());
           query.getFirstInBackground(new GetCallback<ParseObject>()
            {
            @Override
            public void done (ParseObject parseObject, ParseException e)
            {
                if (e == null)
                {
                    // retrieved the object
                    userClasses[PERIOD1] = parseObject.getString("first_period");
                    userClasses[PERIOD2] = parseObject.getString("second_period");
                    userClasses[PERIOD3] = parseObject.getString("third_period");
                    userClasses[PERIOD4] = parseObject.getString("fourth_period");
                    userClasses[PERIOD5] = parseObject.getString("fifth_period");
                    userClasses[PERIOD6] = parseObject.getString("sixth_period");
                    userClasses[PERIOD7] = parseObject.getString("seventh_period");
                }
                else
                {
                    // failed lookup. Do something here
                    Toast.makeText(getApplicationContext(),"Exception Thrown" ,Toast.LENGTH_SHORT).show();
                }
            }
        });
         } else {
          // myObjectSaveDidNotSucceed();
         }
       }
     });

Hope this helps

2 Comments

I tried what you did and made a toast to see if it it saved the data. Surprisingly it did but I tried something else. I went back to my main activity where I originally query the data and I made another toast to see if the data is store. IT IS! But the userClasses is still null!! I even made a new string called newname and set it equal to userClasses. It is null but the toast still displays the string it is supposed to!?!? Look here to to see i.imgur.com/iGJKJpU.png
Add a toast ` Toast.makeText(getApplicationContext(),parseObject.getString("first_period"),Toast.LENGTH_SHORT).show();` instead of your Toast.makeText(getApplicationContext(),userClasses[PERIOD1],Toast.LENGTH_SHORT).show();. If this shows results it means data is saved and queried properly. Then the question would be in your userClasses array. show me how you have initialize it. i would recommend using List<String> or Map<String, List<String>> map = new HashMap<String, List<String>>(); if you are not comfortable using arrays.
0

After getting some sleep then doing more research and tinkering I found the problem. The problem isn't in my code; it's in my network... well not entirely. You see my code runs ... at well run-time (As fast as my phone can process it); however, the method

query.findInBackground(new FindCallback<ParseObject>()

runs asynchronously meaning that it does not run at the speed of my other code. Which makes sense if you think about it because it has to send and wait for a response from a database. That's why when I made a toast inside it, the data was there in the string but when I tried to make a toast a few lines later, outside of the method, the data was not there. It was null.

Here's an example. I make a toast at the end of on create with this code

@Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // initialise and set toolbar as actionbar
        toolbar = (Toolbar) findViewById(R.id.tool_bar);
        setSupportActionBar(toolbar);

        // initialize nav bars
        initNavBars();

        // initialize drawer layout
        NavigationView navView = (NavigationView) findViewById(R.id.navigation_view);

        // initialize nav drawer
        navDrawer = (DrawerLayout) findViewById(R.id.drawer);
        initNavDrawer(navDrawer);

        // initialize layout manager for recycler view
        RecyclerView.LayoutManager mainLayoutManager = new LinearLayoutManager(this);

        // initialize data for all classes before setting adapter
        initClassData();  // <---- MY PARSE QUERY IS IN THIS METHOD

        // set the adapter for recycler view
        RecyclerView.Adapter mainAdapter = new MainRecyclerAdapter(classrooms);

        // initialize recycler view elements
        RecyclerView mainRecyclerView = (RecyclerView) findViewById(R.id.main_recycler_view);

        // add layout manager to recycler view
        mainRecyclerView.setLayoutManager(mainLayoutManager);

        // add adapter to recycler view
        mainRecyclerView.setAdapter(mainAdapter);

        Toast.makeText(getApplicationContext(), userClasses[PERIOD1], Toast.LENGTH_SHORT).show(); // <----- HERE IS MY TOAST
    }

When I run it, the toast is empty because the string is still null, but if I run it with this code

 @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // initialise and set toolbar as actionbar
        toolbar = (Toolbar) findViewById(R.id.tool_bar);
        setSupportActionBar(toolbar);

        // initialize nav bars
        initNavBars();

        // initialize drawer layout
        NavigationView navView = (NavigationView) findViewById(R.id.navigation_view);

        // initialize nav drawer
        navDrawer = (DrawerLayout) findViewById(R.id.drawer);
        initNavDrawer(navDrawer);

        // initialize layout manager for recycler view
        RecyclerView.LayoutManager mainLayoutManager = new LinearLayoutManager(this);

        // initialize data for all classes before setting adapter
        initClassData(); // <---- PARSE QUERY IS STILL IN THIS METHOD

        // set the adapter for recycler view
        RecyclerView.Adapter mainAdapter = new MainRecyclerAdapter(classrooms);

        // initialize recycler view elements
        RecyclerView mainRecyclerView = (RecyclerView) findViewById(R.id.main_recycler_view);

        // add layout manager to recycler view
        mainRecyclerView.setLayoutManager(mainLayoutManager);

        // add adapter to recycler view
        mainRecyclerView.setAdapter(mainAdapter);

        final Handler handler = new Handler();
        handler.postDelayed(new Runnable()
        {
            @Override
            public void run()
            {
                // Do something after 5s = 5000ms
                Toast.makeText(getApplicationContext(), userClasses[PERIOD1], Toast.LENGTH_SHORT).show();
            }
        }, 5000);
    }

In this example I forced the toast to wait five seconds to allow

query.getFirstInBackground(new GetCallback<ParseObject>()

to finish querying the database and when I run it, the toast displays the string correctly.

So if your program relies on a parse query to get important data, you have to structure your code in a way to allow for a second or two to pass to let the parse query return.

Or you could alternatively store the data locally and then you can use the database as a backup for the data and check to make sure it is the same every time the user wants to switch it or somethings.

Thank you Ajay and Rasika I would still be trying to figure why it was not working if it weren't you.

Comments

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.