方法调用“ toString”可能会产生“ java.lang.NullPointerException”错误

时间:2018-11-26 19:14:06

标签: android firebase-realtime-database nullpointerexception

当我尝试从Firebase删除数据时遇到此错误Method invocation 'toString' may produce 'java.lang.NullPointerException,这也使我的应用程序崩溃。

这是麻烦所在的行:

String notat = dataSnapshot1.child("notat").getValue().toString();

我做错了什么吗?

这是我的logcat:

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
                                                                       at com.example.server.almu.Notater$2$1.onDataChange(Notater.java:145)
                                                                       at com.google.android.gms.internal.to.zza(Unknown Source:13)
                                                                       at com.google.android.gms.internal.vj.zzHX(Unknown Source:2)
                                                                       at com.google.android.gms.internal.vp.run(Unknown Source:71)
                                                                       at android.os.Handler.handleCallback(Handler.java:789)
                                                                       at android.os.Handler.dispatchMessage(Handler.java:98)
                                                                       at android.os.Looper.loop(Looper.java:164)
                                                                       at android.app.ActivityThread.main(ActivityThread.java:6944)
                                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                                       at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)

MainActivty:

private void loaddata(){
    Query query = databaseNotater.orderByValue();
    final FirebaseRecyclerAdapter<Notat, NoteViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Notat, NoteViewHolder>(

            Notat.class,
            R.layout.single_note_layout,
            NoteViewHolder.class,
            query
    ) {
        @Override
        protected void populateViewHolder(final NoteViewHolder viewHolder, Notat model, int position) {
            final String notatId = getRef(position).getKey();

            databaseNotater.child(notatId).addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot1) {
                    String notat = dataSnapshot1.child("notat").getValue().toString();
                    viewHolder.setNoteTitle(notat);
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });

        }
    };

    mNoteslist.setAdapter(firebaseRecyclerAdapter);

    firebaseRecyclerAdapter.notifyDataSetChanged();
}

删除方法代码:

 private void testendredialog(final String jobbnavn, final String jobbId) {
    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
    LayoutInflater inflater = getLayoutInflater();
    final View dialogView = inflater.inflate(R.layout.test_slettdialog, null);

    dialogBuilder.setView(dialogView);

    final ImageView ivslettjobb = (ImageView) dialogView.findViewById(R.id.ivslett1);


    dialogBuilder.setTitle(jobbnavn);

    final AlertDialog alertDialog = dialogBuilder.create();
    alertDialog.show();

    ivslettjobb.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            deleteJobb(jobbId);
            alertDialog.dismiss();
        }

    });
}

private void deleteJobb(String id) {

    DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("Jobb").child(id);
    DatabaseReference databaseReference1= FirebaseDatabase.getInstance().getReference("Notater").child(id);
    DatabaseReference databaseReference2= FirebaseDatabase.getInstance().getReference("Oppgaver").child(id);

    databaseReference2.removeValue();
    databaseReference1.removeValue();
    databaseReference.removeValue();

    Toast.makeText(this, "Ansatt slettet", Toast.LENGTH_LONG).show();

    final listJobber adapter = new listJobber(Jobbliste.this, listjobb);
    listjobber.setAdapter(adapter);

    adapter.notifyDataSetChanged();
}

1 个答案:

答案 0 :(得分:1)

您可以像这样解决问题,但是您需要做进一步的调试才能知道为什么值返回为def dfs_to_sheet(dflist, bookname, sheetname, startrow = 0): writer = pd.ExcelWriter('{}.xlsx'.format(bookname), engine='openpyxl') numdfs = 0 startrow = 0 for i in dflist: if numdfs == 0: i.to_excel(writer, sheet_name=sheetname, startrow = startrow) elif numdfs != 0: i.to_excel(writer, sheet_name = sheetname, startrow = startrow) startrow = startrow + len(i) + 2 numdfs += 1 writer.save()

null