Android OnDataChange方法被调用并报告空对象

Android OnDataChange方法被调用并报告空对象,android,firebase,firebase-realtime-database,nullpointerexception,Android,Firebase,Firebase Realtime Database,Nullpointerexception,我有一个应用程序,我使用firebase来存储我的数据 我的数据使用onOnDataChange方法以片段形式检索,我在单独的活动中将数据添加到我的firebase中 片段作为一个单独的实体来检索数据是完美的。但是,当我立即在活动中添加数据时,会自动调用此片段方法,并报告my object->datasnapshotobject.child(“列名称”)为空。 我编写了一个条件来检查是否datasnapshotobject.exists(),然后只执行进一步的步骤。但是它绕过了上面的所有行,直接

我有一个应用程序,我使用
firebase
来存储我的数据

我的数据使用on
OnDataChange
方法以片段形式检索,我在单独的活动中将数据添加到我的firebase中

片段作为一个单独的实体来检索数据是完美的。但是,当我立即在活动中添加数据时,会自动调用此片段方法,并报告
my object->datasnapshotobject.child(“列名称”)为空。

我编写了一个条件来检查是否
datasnapshotobject.exists()
,然后只执行进一步的步骤。但是它绕过了上面的所有行,直接跳到
datasnapshotobject。child(“column\u name”)
为空,给出了错误。我如何解决这个问题

检索数据的代码

 Firebase mref=new Firebase("https://robocon-89a7c.firebaseio.com/Competitions/");
mref.addValueEventListener(new ValueEventListener() {
  @Override
  public void onDataChange(DataSnapshot dataSnapshot) {

    for(DataSnapshot dsp:dataSnapshot.getChildren() )
    {
            if(dsp.exists() && dsp.child("city").getValue(String.class).equalsIgnoreCase("Pune"))
            {
                String start_date=dsp.child("start_date").getValue(String.class);
                SimpleDateFormat curFormater = new SimpleDateFormat("dd-MM-yyyy");


                try {
                    Date chkDate = curFormater.parse(start_date);
                    Calendar cal = Calendar.getInstance();
                    String date1 = cal.get(Calendar.DATE)+"-"+(cal.get(Calendar.MONTH)+1) +"-"+cal.get(Calendar.YEAR);

                    Date currentDate = curFormater.parse(date1);

                    if(chkDate.compareTo(currentDate)>=0){
                        myList.add("\n"+dsp.child("competitionName").getValue(String.class)+"\n"+dsp.child("institutionName").getValue(String.class)+"\n"+dsp.child("start_date").getValue(String.class)+" at "+dsp.child("start_time").getValue(String.class));


                        //System.out.println("The entered date is within the range of six months");
                    }
                    else{
                        System.out.println("The entered date is before than the current system date");
                    }

                }
                catch (ParseException e) {

                    e.printStackTrace();
                }



            }


    }
    String values[]=new String[myList.size()];
    values=myList.toArray(values);


    ArrayAdapter<String> adapter=new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,values);
    mylistView.setAdapter(adapter);
    mylistView.setVisibility(View.VISIBLE);
    // value=dataSnapshot.child("city").getValue(String.class);
    //   Toast.makeText(getApplicationContext(),"its "+value,Toast.LENGTH_SHORT).show();
    //chck.setText(ansList.get(0)+ansList.get(1)+ansList.get(2));
  }

  @Override
  public void onCancelled(FirebaseError firebaseError) {

  }
});

通过这种方式,我也添加了其他列和数据,这里只提供了一些。

我认为您的datasnapshot存在,并且确实不是空的,但是节点结构与添加数据的方法不同,因此它没有您要查找的字段

您在数据库上写道:

|--变量“unid”中的某些内容

|----竞赛名称

|------竞争

|----机构名称

|------机构

并尝试检索:

|--城市

|--开始日期

|--竞赛名称


如果“城市”或“开始日期”是“unid”变量中的值,则没有问题。但至少对于competitionName,您需要向下输入一个节点。

我使用了Firebase mref=new Firebase(“”); 所以通过这些子项,我得到了uid,它是主父项,到目前为止我提到的所有列

我找到了解决办法。 如果您的数据库每次都将有新值,而不会更改现有值,请使用addListenerForSingleValueEvent而不是addValueEventListener,因为这将帮助我们不侦听更改,并且仅在再次连接时才会重新连接


现在,这两个需求都独立工作,并且不会影响彼此的执行。

检查“datasnapshot!=null”而不是datasnapshot.isExist(),所有可能的方式都像exists、=null尝试过该片段曾经被执行过,然后我们打算转到“活动”添加数据,以便它直接跳转到找到的null对象所在的行。您可以在此处共享您的代码吗。添加数据后立即共享@MayurPatel dsp.child(“city”)。getV为null在您的代码中没有
datasnapshotobject.child(“列名”)
。你是不是想再要一张支票?
Firebase firebase=new Firebase("https://robocon-89a7c.firebaseio.com/Competitions/");
   Firebase mainEle=firebase.child(unid);
   Firebase firebasechild=mainEle.child("competitionName");
   firebasechild.setValue(competition);
   Firebase firebasechild2=mainEle.child("institutionName");
   firebasechild2.setValue(institutionN);