Firebase字符串项的NullPointer

时间:2016-12-03 22:22:43

标签: android firebase nullpointerexception android-recyclerview

我正在测试两个项目的Firebase。我希望更新一个recyclerview来添加一个项目,firebase onChildAdded()监听器会提示convertlerview进行更新。这是我写给Firebase的代码:

 mSubmitPollCreation.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            //TODO: Need to determine if this is proper epoch - i.e. does it account for time zones
            Calendar c = Calendar.getInstance();
            final String epochTime = String.valueOf(c.getTimeInMillis());

            //TODO: Need to check if poll requirements are added, i.e. Question, Answer, ......
            //check if image has been loaded first
            if (resultImageURL != null){
                mBaseRef.child("Polls").child(epochTime).child("Image_URL").setValue(resultImageURL);
            } else {
                Toast.makeText(getApplicationContext(),getResources().getString(R.string.no_image_selected),Toast.LENGTH_LONG).show();
                return;
            }

            //Add Answer Counter defaulted to 0
            Map<String, Object> pollAnswerCounter = new HashMap<String, Object>();
            pollAnswerCounter.put("Vote_Count", 0);
            mBaseRef.child("Polls").child(epochTime).updateChildren(pollAnswerCounter);

            //Add Question
            Map<String, Object> pollQuestion = new HashMap<String, Object>();
            pollQuestion.put("Question", mCreatePollQuestion.getText().toString());
            mBaseRef.child("Polls").child(epochTime).updateChildren(pollQuestion);

            if (mNumberOfPollAnswersCreatedByUser > 5){
                Toast.makeText(getApplicationContext(),getResources().getText(R.string.poll_answers_greater_than_five),Toast.LENGTH_LONG).show();
                mNumberOfPollAnswersCreatedByUser = 5;
            }
            //add answers to Firebase
            for (int i = 0; i < mNumberOfPollAnswersCreatedByUser; i++){
                EditText editText = (EditText) mEditTextAnswerLayout.findViewWithTag(getResources().getString(R.string.created_answer_editText_id)+String.valueOf(i+1));
                String editTextInputForAnswer = String.valueOf(editText.getText());
                final String answerLabel = "Answer";
                Map<String, Object> answerChoice = new HashMap<String, Object>();
                answerChoice.put(answerLabel, editTextInputForAnswer);
                mBaseRef.child("Polls").child(epochTime).child("Answers").child(String.valueOf(i+1)).updateChildren(pollAnswerCounter);
                mBaseRef.child("Polls").child(epochTime).child("Answers").child(String.valueOf(i+1)).updateChildren(answerChoice);
                Toast.makeText(getApplicationContext(),editTextInputForAnswer,Toast.LENGTH_SHORT).show();
            }

            Intent toHomeActivity = new Intent(CreateActivity.this, HomeActivity.class);
            toHomeActivity.putExtra("viewpager_position", 2);
            startActivity(toHomeActivity);
        }
    });

以下是我用于从Firebase检索数据的单独片段中的代码:

@Override
public void onStart() {
    super.onStart();
    mChildEventListener = new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            String newPollEpoch = dataSnapshot.getKey();
            Log.v("Child_Added", "The new child is " + newPollEpoch);
            String newPollImageURL = dataSnapshot.child(IMAGE_URL).getValue().toString();
            String newPollQuestion = dataSnapshot.child(QUESTION_STRING).getValue().toString();
            mNewPollsAray.add(0, new Poll(newPollQuestion, newPollImageURL, newPollEpoch));
            mNewPollsAdapter.notifyDataSetChanged();
        }


        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {

        }

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    };
    mPollsRef.addChildEventListener(mChildEventListener);

}

@Override
public void onStop() {
    super.onStop();
    mPollsRef.removeEventListener(mChildEventListener);
}

这是错误。这很奇怪,因为错误是在&#34; String newPollQuestion上提示。&#34;我使用完全相同的方法来编写&#34; newPollImageURL&#34;到Firebase,该对象不为null。此外,有2个设备,A和B. A创建项目并将其写入Firebase,然后读取它。 B是显示以下内容的设备:

12-03 16:21:20.255 21043-21043/com.troychuinard.fanpolls E/AndroidRuntime: FATAL EXCEPTION: main
                                                                       Process: com.troychuinard.fanpolls, PID: 21043
                                                                       java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
                                                                           at com.troychuinard.fanpolls.Fragment.NewFragment$1.onChildAdded(NewFragment.java:159)
                                                                           at com.google.android.gms.internal.zzaip.zza(Unknown Source)
                                                                           at com.google.android.gms.internal.zzakp.zzcxi(Unknown Source)
                                                                           at com.google.android.gms.internal.zzaks$1.run(Unknown Source)
                                                                           at android.os.Handler.handleCallback(Handler.java:739)
                                                                           at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                           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)


                                                                       --------- beginning of system

1 个答案:

答案 0 :(得分:0)

我不明白为什么,但改变了:

String newPollQuestion = dataSnapshot.child(QUESTION_STRING).getValue.toString;

为:

String newPollQuestion = dataSnapshot.child(QUESTION_STRING).getValue(String.class);

成功删除了NullPointer。知道为什么吗?