Android。解析查询错误
Android. Error In Parse Query
我正在构建一个聊天应用程序,因为我使用 Firebase 来存储和传输消息,并使用 Parse 来存储所有用户数据,例如(用户注册、登录、存储最近 table 和发送推送)。
在 Parse 中,我将所有用户登录数据存储在 Usertable 中,并且我正在维护一个 Recent table 来存储所有最近的聊天记录。
用户一对一聊天将保存在一个 GroupId(String) 下。该组 ID 是通过添加
形成的
GroupId = UserOneObjectId+UserTwoObjectId;
如果有人与特定的人进行了新的聊天,它将在最近 table 中存储有关最近聊天的数据,主要是它必须将 GroupId 和成员详细信息存储在数组中,例如:
members = [UserOneObjectId, UserTwoObjectId]
下次如果用户再次与同一个人聊天,则必须查询 table 并查找成员数组。如果两个人的对象 ID 都已经存在,则必须从该行中获取 GroupId。
为此我添加了这些代码。但这不起作用,请检查并告诉我我犯了什么错误...
我的完整代码是...
public class StartChat extends AppCompatActivity {
private String FIREBASE_URL = "https://craking-fire-base.cracked.com/";
private String user1ObjectId, user1Name;
private String user2ObjectId, user2Name;
private static String groupId;
private Firebase mFirebaseRef;
private Chat chat;
private ChatListAdapter mChatListAdapter;
private ValueEventListener mConnectedListener;
private PrefManager pref;
private Toolbar mtoolbar;
private ListView listView;
private EditText inputText;
String groupIdGot;
ParseObject entryToRecentChat;
ParseQuery<ParseObject> groupIdQuery;
public StartChat() {
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start_chat);
mtoolbar = (Toolbar) findViewById(R.id.home_toolbar);
setSupportActionBar(mtoolbar);
getSupportActionBar().setDisplayShowHomeEnabled(false);
mtoolbar.setTitle(user2Name);
pref = new PrefManager(getApplicationContext());
user1ObjectId = pref.getObjectId();
user1Name = pref.getUserName();
Intent i = getIntent();
user2ObjectId = i.getStringExtra("user2ObjectId");
user2Name = i.getStringExtra("user2Name");
listView = (ListView) findViewById(R.id.list);
/*groupId = user1ObjectId + user2ObjectId;*/
groupIdQuery = ParseQuery.getQuery("Recent");
groupIdQuery.whereContainsAll("members", Arrays.asList(user1ObjectId, user2ObjectId));
Log.d("objectArray", Arrays.asList(user1ObjectId, user2ObjectId).toString());
groupIdQuery.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> list, ParseException e) {
if (e == null)
{
for (ParseObject dealsObject : list) {
// use dealsObject.get('columnName') to access the properties of the Deals object.
String objId = dealsObject.getObjectId();
groupIdGot = (String) dealsObject.get("groupId");
Log.d("GroupId***Got", groupIdGot);
//String members = (String) dealsObject.get("members");
Toast.makeText(getApplicationContext(), "ObjectId: " + objId + "\n" + "GroupId: " + groupId + " value present", Toast.LENGTH_LONG).show();
}
}else{
Toast.makeText(getApplicationContext(), " No Data present", Toast.LENGTH_LONG).show();
groupId = user1ObjectId + user2ObjectId;
entryToRecentChat = new ParseObject("Recent");
entryToRecentChat.put("groupId", groupId);
entryToRecentChat.put("lastUser", ParseObject.createWithoutData("_User", user1ObjectId));
entryToRecentChat.put("user", ParseObject.createWithoutData("_User", user2ObjectId));
entryToRecentChat.addAllUnique("members", Arrays.asList(user1ObjectId, user2ObjectId));
entryToRecentChat.saveEventually(new SaveCallback() {
@Override
public void done(ParseException e) {
if (e == null) {
Toast.makeText(getApplicationContext(), "Value saved", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "Value not saved", Toast.LENGTH_LONG).show();
Log.d("exception", e.getMessage());
}
}
});
}
}
});
//groupId = user1ObjectId + user2ObjectId;
if(groupIdGot != null){
groupId = groupIdGot;
}else{
groupId = user1ObjectId + user2ObjectId;
}
mFirebaseRef = new Firebase(FIREBASE_URL).child("Message").child(groupId);
inputText = (EditText) findViewById(R.id.messageInput);
inputText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
if (actionId == EditorInfo.IME_NULL && keyEvent.getAction() == KeyEvent.ACTION_DOWN) {
sendMessage();
}
return true;
}
});
//input = inputText.getText().toString();
findViewById(R.id.sendButton).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sendMessage();
sendPush();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_start_chat, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onStart() {
super.onStart();
// Setup our view and list adapter. Ensure it scrolls to the bottom as data changes
//final ListView listView = getListView();
mtoolbar.setTitle(user2Name);
mChatListAdapter = new ChatListAdapter(mFirebaseRef.limit(50), this, R.layout.chat_message, user1Name);
listView.setAdapter(mChatListAdapter);
mChatListAdapter.registerDataSetObserver(new DataSetObserver() {
@Override
public void onChanged() {
super.onChanged();
listView.setSelection(mChatListAdapter.getCount() - 1);
}
});
// Finally, a little indication of connection status
mConnectedListener = mFirebaseRef.getRoot().child(".info/connected").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
boolean connected = (Boolean) dataSnapshot.getValue();
if (connected) {
Toast.makeText(StartChat.this, "Connected to ChatRoom", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(StartChat.this, "Disconnected from ChatRoom", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onCancelled(FirebaseError firebaseError) {
// No-op
}
});
}
@Override
protected void onStop() {
super.onStop();
mFirebaseRef.getRoot().child(".info/connected").removeEventListener(mConnectedListener);
mChatListAdapter.cleanup();
}
private void sendMessage() {
String input = inputText.getText().toString();
if (!input.equals("")) {
// Create our 'model', a Chat object
chat = new Chat(input, user1Name);
// Create a new, auto-generated child of that chat location, and save our chat data there
mFirebaseRef.push().setValue(chat);
inputText.setText("");
}
}
特别是我在这一行中收到空指针错误
mFirebaseRef = new Firebase(FIREBASE_URL).child("Message").child(groupId);
因为对我来说这个查询不适合我。
groupIdQuery = ParseQuery.getQuery("Recent");
groupIdQuery.whereContainsAll("members", Arrays.asList(user1ObjectId, user2ObjectId));
Log.d("objectArray", Arrays.asList(user1ObjectId, user2ObjectId).toString());
groupIdQuery.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> list, ParseException e) {
if (e == null)
{
for (ParseObject dealsObject : list) {
// use dealsObject.get('columnName') to access the properties of the Deals object.
String objId = dealsObject.getObjectId();
groupIdGot = (String) dealsObject.get("groupId");
Log.d("GroupId***Got", groupIdGot);
//String members = (String) dealsObject.get("members");
Toast.makeText(getApplicationContext(), "ObjectId: " + objId + "\n" + "GroupId: " + groupId + " value present", Toast.LENGTH_LONG).show();
}
}else{
Toast.makeText(getApplicationContext(), " No Data present", Toast.LENGTH_LONG).show();
groupId = user1ObjectId + user2ObjectId;
entryToRecentChat = new ParseObject("Recent");
entryToRecentChat.put("groupId", groupId);
entryToRecentChat.put("lastUser", ParseObject.createWithoutData("_User", user1ObjectId));
entryToRecentChat.put("user", ParseObject.createWithoutData("_User", user2ObjectId));
entryToRecentChat.addAllUnique("members", Arrays.asList(user1ObjectId, user2ObjectId));
entryToRecentChat.saveEventually(new SaveCallback() {
@Override
public void done(ParseException e) {
if (e == null) {
Toast.makeText(getApplicationContext(), "Value saved", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "Value not saved", Toast.LENGTH_LONG).show();
Log.d("exception", e.getMessage());
}
}
});
}
}
});
//groupId = user1ObjectId + user2ObjectId;
if(groupIdGot != null){
groupId = groupIdGot;
}else{
groupId = user1ObjectId + user2ObjectId;
}
在你的问题中你说:
Particularly I am getting nullpointer error in this line
mFirebaseRef = new Firebase(FIREBASE_URL).child("Message").child(groupId);
当您尝试对 null
值调用方法时抛出 NullPointerException
。
"SO".toString(); // works
null.toString(); // throws a NullPointerException
我一直记得:
a NullPointerException
is thrown when you have a null
value before a dot
你的语句中有两个点,所以有两个地方 NullPointException
可以抛出。让我们将语句分成几行:
mFirebaseRef =
new Firebase(FIREBASE_URL) // can this be null?
.child("Message") // can this be null?
.child(groupId);
所以要么 new Firebase(FIREBASE_URL)
returns 为空,要么 child("Messages")
returns 为空。鉴于 Firebase 的工作方式,很可能 child("Message")
returns 为空。所以你的数据库没有名为 Message
.
的子节点
我正在构建一个聊天应用程序,因为我使用 Firebase 来存储和传输消息,并使用 Parse 来存储所有用户数据,例如(用户注册、登录、存储最近 table 和发送推送)。
在 Parse 中,我将所有用户登录数据存储在 Usertable 中,并且我正在维护一个 Recent table 来存储所有最近的聊天记录。 用户一对一聊天将保存在一个 GroupId(String) 下。该组 ID 是通过添加
形成的GroupId = UserOneObjectId+UserTwoObjectId;
如果有人与特定的人进行了新的聊天,它将在最近 table 中存储有关最近聊天的数据,主要是它必须将 GroupId 和成员详细信息存储在数组中,例如:
members = [UserOneObjectId, UserTwoObjectId]
下次如果用户再次与同一个人聊天,则必须查询 table 并查找成员数组。如果两个人的对象 ID 都已经存在,则必须从该行中获取 GroupId。
为此我添加了这些代码。但这不起作用,请检查并告诉我我犯了什么错误...
我的完整代码是...
public class StartChat extends AppCompatActivity {
private String FIREBASE_URL = "https://craking-fire-base.cracked.com/";
private String user1ObjectId, user1Name;
private String user2ObjectId, user2Name;
private static String groupId;
private Firebase mFirebaseRef;
private Chat chat;
private ChatListAdapter mChatListAdapter;
private ValueEventListener mConnectedListener;
private PrefManager pref;
private Toolbar mtoolbar;
private ListView listView;
private EditText inputText;
String groupIdGot;
ParseObject entryToRecentChat;
ParseQuery<ParseObject> groupIdQuery;
public StartChat() {
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start_chat);
mtoolbar = (Toolbar) findViewById(R.id.home_toolbar);
setSupportActionBar(mtoolbar);
getSupportActionBar().setDisplayShowHomeEnabled(false);
mtoolbar.setTitle(user2Name);
pref = new PrefManager(getApplicationContext());
user1ObjectId = pref.getObjectId();
user1Name = pref.getUserName();
Intent i = getIntent();
user2ObjectId = i.getStringExtra("user2ObjectId");
user2Name = i.getStringExtra("user2Name");
listView = (ListView) findViewById(R.id.list);
/*groupId = user1ObjectId + user2ObjectId;*/
groupIdQuery = ParseQuery.getQuery("Recent");
groupIdQuery.whereContainsAll("members", Arrays.asList(user1ObjectId, user2ObjectId));
Log.d("objectArray", Arrays.asList(user1ObjectId, user2ObjectId).toString());
groupIdQuery.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> list, ParseException e) {
if (e == null)
{
for (ParseObject dealsObject : list) {
// use dealsObject.get('columnName') to access the properties of the Deals object.
String objId = dealsObject.getObjectId();
groupIdGot = (String) dealsObject.get("groupId");
Log.d("GroupId***Got", groupIdGot);
//String members = (String) dealsObject.get("members");
Toast.makeText(getApplicationContext(), "ObjectId: " + objId + "\n" + "GroupId: " + groupId + " value present", Toast.LENGTH_LONG).show();
}
}else{
Toast.makeText(getApplicationContext(), " No Data present", Toast.LENGTH_LONG).show();
groupId = user1ObjectId + user2ObjectId;
entryToRecentChat = new ParseObject("Recent");
entryToRecentChat.put("groupId", groupId);
entryToRecentChat.put("lastUser", ParseObject.createWithoutData("_User", user1ObjectId));
entryToRecentChat.put("user", ParseObject.createWithoutData("_User", user2ObjectId));
entryToRecentChat.addAllUnique("members", Arrays.asList(user1ObjectId, user2ObjectId));
entryToRecentChat.saveEventually(new SaveCallback() {
@Override
public void done(ParseException e) {
if (e == null) {
Toast.makeText(getApplicationContext(), "Value saved", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "Value not saved", Toast.LENGTH_LONG).show();
Log.d("exception", e.getMessage());
}
}
});
}
}
});
//groupId = user1ObjectId + user2ObjectId;
if(groupIdGot != null){
groupId = groupIdGot;
}else{
groupId = user1ObjectId + user2ObjectId;
}
mFirebaseRef = new Firebase(FIREBASE_URL).child("Message").child(groupId);
inputText = (EditText) findViewById(R.id.messageInput);
inputText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
if (actionId == EditorInfo.IME_NULL && keyEvent.getAction() == KeyEvent.ACTION_DOWN) {
sendMessage();
}
return true;
}
});
//input = inputText.getText().toString();
findViewById(R.id.sendButton).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sendMessage();
sendPush();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_start_chat, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onStart() {
super.onStart();
// Setup our view and list adapter. Ensure it scrolls to the bottom as data changes
//final ListView listView = getListView();
mtoolbar.setTitle(user2Name);
mChatListAdapter = new ChatListAdapter(mFirebaseRef.limit(50), this, R.layout.chat_message, user1Name);
listView.setAdapter(mChatListAdapter);
mChatListAdapter.registerDataSetObserver(new DataSetObserver() {
@Override
public void onChanged() {
super.onChanged();
listView.setSelection(mChatListAdapter.getCount() - 1);
}
});
// Finally, a little indication of connection status
mConnectedListener = mFirebaseRef.getRoot().child(".info/connected").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
boolean connected = (Boolean) dataSnapshot.getValue();
if (connected) {
Toast.makeText(StartChat.this, "Connected to ChatRoom", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(StartChat.this, "Disconnected from ChatRoom", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onCancelled(FirebaseError firebaseError) {
// No-op
}
});
}
@Override
protected void onStop() {
super.onStop();
mFirebaseRef.getRoot().child(".info/connected").removeEventListener(mConnectedListener);
mChatListAdapter.cleanup();
}
private void sendMessage() {
String input = inputText.getText().toString();
if (!input.equals("")) {
// Create our 'model', a Chat object
chat = new Chat(input, user1Name);
// Create a new, auto-generated child of that chat location, and save our chat data there
mFirebaseRef.push().setValue(chat);
inputText.setText("");
}
}
特别是我在这一行中收到空指针错误
mFirebaseRef = new Firebase(FIREBASE_URL).child("Message").child(groupId);
因为对我来说这个查询不适合我。
groupIdQuery = ParseQuery.getQuery("Recent");
groupIdQuery.whereContainsAll("members", Arrays.asList(user1ObjectId, user2ObjectId));
Log.d("objectArray", Arrays.asList(user1ObjectId, user2ObjectId).toString());
groupIdQuery.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> list, ParseException e) {
if (e == null)
{
for (ParseObject dealsObject : list) {
// use dealsObject.get('columnName') to access the properties of the Deals object.
String objId = dealsObject.getObjectId();
groupIdGot = (String) dealsObject.get("groupId");
Log.d("GroupId***Got", groupIdGot);
//String members = (String) dealsObject.get("members");
Toast.makeText(getApplicationContext(), "ObjectId: " + objId + "\n" + "GroupId: " + groupId + " value present", Toast.LENGTH_LONG).show();
}
}else{
Toast.makeText(getApplicationContext(), " No Data present", Toast.LENGTH_LONG).show();
groupId = user1ObjectId + user2ObjectId;
entryToRecentChat = new ParseObject("Recent");
entryToRecentChat.put("groupId", groupId);
entryToRecentChat.put("lastUser", ParseObject.createWithoutData("_User", user1ObjectId));
entryToRecentChat.put("user", ParseObject.createWithoutData("_User", user2ObjectId));
entryToRecentChat.addAllUnique("members", Arrays.asList(user1ObjectId, user2ObjectId));
entryToRecentChat.saveEventually(new SaveCallback() {
@Override
public void done(ParseException e) {
if (e == null) {
Toast.makeText(getApplicationContext(), "Value saved", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "Value not saved", Toast.LENGTH_LONG).show();
Log.d("exception", e.getMessage());
}
}
});
}
}
});
//groupId = user1ObjectId + user2ObjectId;
if(groupIdGot != null){
groupId = groupIdGot;
}else{
groupId = user1ObjectId + user2ObjectId;
}
在你的问题中你说:
Particularly I am getting nullpointer error in this line
mFirebaseRef = new Firebase(FIREBASE_URL).child("Message").child(groupId);
当您尝试对 null
值调用方法时抛出 NullPointerException
。
"SO".toString(); // works
null.toString(); // throws a NullPointerException
我一直记得:
a
NullPointerException
is thrown when you have anull
value before a dot
你的语句中有两个点,所以有两个地方 NullPointException
可以抛出。让我们将语句分成几行:
mFirebaseRef =
new Firebase(FIREBASE_URL) // can this be null?
.child("Message") // can this be null?
.child(groupId);
所以要么 new Firebase(FIREBASE_URL)
returns 为空,要么 child("Messages")
returns 为空。鉴于 Firebase 的工作方式,很可能 child("Message")
returns 为空。所以你的数据库没有名为 Message
.