Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Getting A ParseObject From Another Class

So in an app i'm making I have a ParseObject with other ParseObjects inside it. I want to move that object over to another class where I can still use ParseObject methods etc. The problem is this error:

07-30 18:12:48.954: E/AndroidRuntime(1403): FATAL EXCEPTION: main 07-30 18:12:48.954: E/AndroidRuntime(1403): Process: com.jared.quizle, PID: 1403 07-30 18:12:48.954: E/AndroidRuntime(1403): java.lang.NullPointerException 07-30 18:12:48.954: E/AndroidRuntime(1403): at com.jared.quizle.FinishCreateQuizActivity$2.onClick(FinishCreateQuizActivity.java:54) 07-30 18:12:48.954: E/AndroidRuntime(1403): at android.view.View.performClick(View.java:4438) 07-30 18:12:48.954: E/AndroidRuntime(1403): at android.view.View$PerformClick.run(View.java:18422) 07-30 18:12:48.954: E/AndroidRuntime(1403): at android.os.Handler.handleCallback(Handler.java:733) 07-30 18:12:48.954: E/AndroidRuntime(1403): at android.os.Handler.dispatchMessage(Handler.java:95) 07-30 18:12:48.954: E/AndroidRuntime(1403): at android.os.Looper.loop(Looper.java:136) 07-30 18:12:48.954: E/AndroidRuntime(1403): at android.app.ActivityThread.main(ActivityThread.java:5017) 07-30 18:12:48.954: E/AndroidRuntime(1403): at java.lang.reflect.Method.invokeNative(Native Method) 07-30 18:12:48.954: E/AndroidRuntime(1403): at java.lang.reflect.Method.invoke(Method.java:515) 07-30 18:12:48.954: E/AndroidRuntime(1403): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 07-30 18:12:48.954: E/AndroidRuntime(1403): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 07-30 18:12:48.954: E/AndroidRuntime(1403): at dalvik.system.NativeStart.main(Native Method)

This is where my Parse Object is from....

for(int i = 0; i < questionNumber; i++){ ParseObject quizQuestion = new ParseObject("QuizQuestion" + (i+1)); quizQuestion.put("question", question.getText().toString()); quizQuestion.put("choiceA", A); quizQuestion.put("choiceB", B); quizQuestion.put("choiceC", C); quizQuestion.put("choiceD", D); quizQuestion.put("choiceACheckBox", CheckA); quizQuestion.put("choiceBCheckBox", CheckB); quizQuestion.put("choiceCCheckBox", CheckC); quizQuestion.put("choiceDCheckBox", CheckD); quizObject.add("QuizQuestion" + (i + 1), quizQuestion); }

                questionNumber++;

                Log.i(TAG, "QuestionNumber:" + questionNumber + ", NumberOfQuestions: " + getNumberOfQuestions());

                if(questionNumber < getNumberOfQuestions() + 1){
                    resetActivity();
                }
                else{
                    navigateToQuizFinish();

                }
            }
            else{
                //display warning Toast
                Toast.makeText(CreateQuizActivity.this, R.string.quiz_not_correctly_filled_out_warning, Toast.LENGTH_LONG).show();
            }

        }
    });


}

private void navigateToQuizFinish() {
    Intent intent = new Intent(this, FinishCreateQuizActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
    startActivity(intent);
}

public ParseObject getQuizObject(){
    return quizObject;
}

And this is where I want to use it...

//quizObject is initialized above (private ParseObject quizObject;)

(Line 54)quizObject = createQuizActivity.getQuizObject(); quizObject.put("QuizTitle", quizTitle.getText().toString()); quizObject.put("Public", publicQuiz); quizObject.put("Private", privateQuiz);

                quizObject.saveInBackground();

                navigateToMain();

Help is greatly appreciated! :)

Harry James
Harry James
14,780 Points

Could you perhaps fix the code boxes? I can't quite understand the code squashed together...

Remember to make sure there's a space above and below your code box start and end.

1 Answer

I fixed it! All I needed was a simple get() method inside the 2nd activity.

Harry James
Harry James
14,780 Points

Awesome!

Glad to hear it's fixed!