本文介绍了Firebase,classmaper:在类“myclass”上找不到“UserTwo”的setter / field的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,firebase不断给我nullpointer异常,也无法找到类ChatModel上的任何设置/字段,这是我的chatModel类:

  package edusolution.matrimony; 

import java.util.HashMap;
import java.util.Map;

/ **
*由al于16/26/16创建。
* /
public class ChatModel
{
public Map< String,Object> message = new HashMap<>();
public String foruser;
public String foruserAnother;

public ChatModel()
{

}

公共ChatModel(字符串foruser,字符串foruserAnother)
{
this.foruser = foruser;
this.foruserAnother = foruserAnother;
}

public String getForuser()
{
return foruser;
}

public void setForuser(String foruser)
{
this.foruser = foruser;
}

public String getForuserAnother()
{
return foruserAnother;
}

public void setForuserAnother(String foruserAnother)
{
this.foruserAnother = foruserAnother;
}

public Map< String,Object> getMessage()
{
返回消息;

$ b $ public void setMessage(Map< String,Object> message)
{
this.message = message;
}

public Map< String,Object> toMap()
{
HashMap< String,Object> result = new HashMap<>();
message.put(somebody,iusedtoknow);
result.put(UserOne,foruser);
result.put(UserTwo,foruserAnother);
result.put(Messages,message);
返回结果;


$ / code $ / pre
$ b $ ps我试图使字段私有和一切,但didn'这是我如何在数据库中创建chatmodel:

pre $私有无效createChatModel(字符串名称,字符串currentUserName)
{
DatabaseReference数据库= FirebaseDatabase.getInstance()。getReference();
String Key = FirebaseDatabase.getInstance()。getReference()。child(chatRooms)。push()。getKey();

ChatModel chat = new ChatModel(Name,currentUserName);
地图< String,Object> postValues = chat.toMap();
地图< String,Object> childUpdates = new HashMap<>();
childUpdates.put(/ chatRooms /+ Key,postValues);
database.updateChildren(childUpdates);





最后我是怎么得到这些数据的:

  public void setupChat(final String username)
{
//检查聊天室是否已经存在$ b $ (DataSnapshot s:dataSnapshot.getChildren()); //为数据快照创建一个数据快照(数据快照数据快照) ))
{
ChatModel chat = s.getValue(ChatModel.class);
if(chat!= null)
{
Log.d(Key and hash(),s.getKey()++ chat.getMessage()。get(0));
if(chat.getForuser()。equals(auth.getCurrentUser()。getUid())& ;& chat.getForuserAnother()。equals(username))
{
Log.d(Chat is,s.getKey ));
RoomName = new String(s.getKey());
ChatEnabled = true;

else if(chat.getForuser()。equals(username)&& chat.getForuserAnother()。equals(auth.getCurrentUser()。getUid()))
{
Log.d(chat is also,s.getKey());
RoomName = new String(s.getKey());
ChatEnabled = true;

else
{
Log.d(No chat:,creating chat);




$ public void onCancelled(DatabaseError databaseError)
{
Toast.makeText(getApplicationContext( ),无法检索数据,Toast.LENGTH_LONG).show();
}
});





示例数据,保存在firebasedatabase中,图片:



最后错误我得到eveytime :/ b>

  08-28 18:28:51.005 31762-31762 / edusolution.matrimony W / ClassMapper:UserTwo没有setter / field在类上找到edusolution.matrimony.ChatModel 
08-28 18:28:51.005 31762-31762 / edusolution.matrimony W / ClassMapper:在类中找到的消息没有setter / field edusolution.matrimony.ChatModel
08 -28 18:28:51.005 31762-31762 / edusolution.matrimony W / ClassMapper:在类上找到的UserOne没有设置者/字段edusolution.matrimony.ChatModel
08-28 18:28:51.005 31762-31762 / edusolution。婚姻D /密钥和哈希:: -KQGYLHb2IQ34AHkzx5G null
08-28 18:28:51.005 31762-31762 / edusolution.matrimony D / AndroidRuntime:关闭VM
08-28 18:28:51.005 31762 -31762 / edusolution.matrimony W / dalvikvm:threadid = 1:线程退出未捕获的异常(group = 0x41919700)
08-28 18:28:51.012 31762-31762 / edusolution.matrimony E / AndroidRuntime:致命异常:main
java.lang.NullPointerExceptio
at edusolution.matrimony.Chat $ 1.onDataChange(Chat.java:70)
在com.google.android.gms.internal.zzaie.zza(未知来源)
在com.google.android.gms。 internal.zzaje.zzcta(Unknown Source)
在com.google.android.gms.internal.zzajh $ 1.run(未知源)
在android.os.Handler.handleCallback(Handler.java:730 )
在android.os.Handler.dispatchMessage(Handler.java:92)
在android.os.Looper.loop(Looper.java:137)
在android.app.ActivityThread。 main(ActivityThread.java:5103)
在java.lang.reflect.Method.invokeNative(本地方法)$ b $在java.lang.reflect.Method.invoke(Method.java:525)
在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:737)
在com.android.internal.os.ZygoteInit.main(ZygoteInit.ja va:553)
at dalvik.system.NativeStart.main(Native Method)


解决方案

POJO类与数据库结构不同。



更改您的 ChatModel

  public class ChatModel {
public Map< String,Object> Messages = new HashMap<>();
public String UserOne;
public String UserTwo;
$ b $ // //构造函数
// setter和getter
}


So firebase keeps giving me nullpointer exception , and also that it can't find any setter/field on class 'ChatModel' , here is my chatModel class :

package edusolution.matrimony;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by el on 8/26/16.
 */
public class ChatModel
{
    public Map<String,Object> message = new HashMap<>();
    public String foruser;
    public String foruserAnother;

    public  ChatModel()
    {

    }

    public  ChatModel(String foruser,String foruserAnother)
    {
        this.foruser = foruser;
        this.foruserAnother = foruserAnother;
    }

    public String getForuser()
    {
        return foruser;
    }

    public void setForuser(String foruser)
    {
        this.foruser = foruser;
    }

    public String getForuserAnother()
    {
        return foruserAnother;
    }

    public void setForuserAnother(String foruserAnother)
    {
        this.foruserAnother = foruserAnother;
    }

    public Map<String, Object> getMessage()
    {
        return message;
    }

    public void setMessage(Map<String,Object> message)
    {
        this.message = message;
    }

    public Map<String,Object> toMap()
    {
        HashMap<String,Object> result = new HashMap<>();
        message.put("somebody","iusedtoknow");
        result.put("UserOne",foruser);
        result.put("UserTwo",foruserAnother);
        result.put("Messages",message);
        return result;
    }
}

p.s i tried making field private and everything but didn't work , here is how i create chatmodel in database :

private void createChatModel (String Name,String currentUserName)
    {
        DatabaseReference database = FirebaseDatabase.getInstance().getReference();
        String Key = FirebaseDatabase.getInstance().getReference().child("chatRooms").push().getKey();

        ChatModel chat = new ChatModel(Name,currentUserName);
        Map<String,Object> postValues = chat.toMap();
        Map<String,Object> childUpdates = new HashMap<>();
        childUpdates.put("/chatRooms/"+Key,postValues);
        database.updateChildren(childUpdates);
    }

and finally how i am trieng to get these data :

public void setupChat(final String username)
    {
        //check if chat room is already exists or not
        databaseReference.child("chatRooms").addValueEventListener(new ValueEventListener()
        {
            public void onDataChange(DataSnapshot dataSnapshot)
            {
                for(DataSnapshot s : dataSnapshot.getChildren())
                {
                    ChatModel chat = s.getValue(ChatModel.class);
                    if(chat != null )
                    {
                        Log.d("Key and hash : ",s.getKey()+" "+chat.getMessage().get(0));
                        if (chat.getForuser().equals(auth.getCurrentUser().getUid()) && chat.getForuserAnother().equals(username))
                        {
                            Log.d("Chat is ", s.getKey());
                            RoomName = new String(s.getKey());
                            ChatEnabled = true;
                        }
                        else if (chat.getForuser().equals(username) && chat.getForuserAnother().equals(auth.getCurrentUser().getUid()))
                        {
                            Log.d("chat is also ", s.getKey());
                            RoomName = new String(s.getKey());
                            ChatEnabled = true;
                        }
                        else
                        {
                            Log.d("No chat : ", "creating chat");
                        }
                    }
                }
            }

            public void onCancelled(DatabaseError databaseError)
            {
                Toast.makeText(getApplicationContext(),"Unable to retrieve data",Toast.LENGTH_LONG).show();
            }
        });
    }

example data , saved in firebasedatabase , picture : Saved data image example

and lastly error i am getting eveytime :

08-28 18:28:51.005 31762-31762/edusolution.matrimony W/ClassMapper: No setter/field for UserTwo found on class edusolution.matrimony.ChatModel
08-28 18:28:51.005 31762-31762/edusolution.matrimony W/ClassMapper: No setter/field for Messages found on class edusolution.matrimony.ChatModel
08-28 18:28:51.005 31762-31762/edusolution.matrimony W/ClassMapper: No setter/field for UserOne found on class edusolution.matrimony.ChatModel
08-28 18:28:51.005 31762-31762/edusolution.matrimony D/Key and hash :: -KQGYLHb2IQ34AHkzx5G null
08-28 18:28:51.005 31762-31762/edusolution.matrimony D/AndroidRuntime: Shutting down VM
08-28 18:28:51.005 31762-31762/edusolution.matrimony W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x41919700)
08-28 18:28:51.012 31762-31762/edusolution.matrimony E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NullPointerExceptio
    at edusolution.matrimony.Chat$1.onDataChange(Chat.java:70)
    at com.google.android.gms.internal.zzaie.zza(Unknown Source)
    at com.google.android.gms.internal.zzaje.zzcta(Unknown Source)
    at com.google.android.gms.internal.zzajh$1.run(Unknown Source)
    at android.os.Handler.handleCallback(Handler.java:730)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:5103)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:525)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
    at dalvik.system.NativeStart.main(Native Method)
解决方案

The POJO class does not have the same structure with the database.

Change your ChatModel class fields to

public class ChatModel {
    public Map<String,Object> Messages = new HashMap<>();
    public String UserOne;
    public String UserTwo;

    // constructor
    // setter and getter
}

这篇关于Firebase,classmaper:在类“myclass”上找不到“UserTwo”的setter / field的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 11:32