jayanth
jayanth

Reputation: 63

Hitting a nullPointerExecption in my android project

Hi Iam trying to make a chat app. In this process Iam trying to retrieve messages stored in database but Iam getting a Null Object reference exception.

The code I used to add messages to database is:

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


            String msg = text_send.getText().toString();
            if(!msg.equals(""))
                sendMessage(fuser.getUid(), currUserId, msg);
            else
                Toast.makeText(MessageActivity.this, "Enter Something", 
            Toast.LENGTH_SHORT).show();
         }
       });


  private void sendMessage(String sender,String receiver,String message)
  {
    DatabaseReference reference = 
    FirebaseDatabase.getInstance().getReference();
    HashMap<String,Object>hashMap = new HashMap<>();
    hashMap.put("sender",sender);
    hashMap.put("receiver",receiver);
    hashMap.put("message",message);

    reference.child("Chats").push().setValue(hashMap);

}

NOTE:The above part of code is working fine. My database is getting updated correctly every time I send message.

After this part I tried to display messages of users, using the following code

     DatabaseReference chatref = 
     FirebaseDatabase.getInstance().getReference().child("Chats");
     ArrayList<String> userchats = new ArrayList<>();

     listView = findViewById(R.id.listView);
     final ArrayAdapter <String> arrayAdapter = new ArrayAdapter<String> 
     (this,android.R.layout.simple_list_item_1,userchats);
     listView.setAdapter(arrayAdapter);

     chatref.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(@NonNull DataSnapshot dataSnapshot, 
        @Nullable String s) {

            String value = dataSnapshot.getValue().toString();
            try {
                JSONObject object = new JSONObject(value);
                message = object.getString("message");
            }
            catch (JSONException e) {
                e.printStackTrace();
            }
            userchats.add(message);
            arrayAdapter.notifyDataSetChanged();
        }

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

        }

        @Override
        public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {

        }

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

        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
        });

When I ran the app at this moment Iam getting error and my stacktrace is as follows:

         Process: com.kcw.firebasebasics, PID: 10943
         java.lang.NullPointerException: Attempt to invoke virtual method 
         'java.lang.String java.lang.Object.toString()' on a null object 
         reference at android.widget.ArrayAdapter.
         createViewFromResource(ArrayAdapter.java:401)
         at android.widget.ArrayAdapter.getView(ArrayAdapter.java:369)
         at android.widget.AbsListView.obtainView(AbsListView.java:2346)
         at android.widget.ListView.makeAndAddView(ListView.java:1875)

Finally a picture of my database:

picture of database

Upvotes: 0

Views: 60

Answers (2)

Erik
Erik

Reputation: 5119

Your chatref refer to the "Chats" key witch is not a String. That´s maybe why you get the

'java.lang.String java.lang.Object.toString()' on a null object

You should parse the dataSnapshot into a class like this example

Comment comment = dataSnapshot.getValue(Comment.class);

And the Comment class have the message receiver Sender variables

Check this Work with Lists of Data from Firebase doc

And finally here is the Firebase Android Codelab showing how to create a complete chat app called Friendly Chat

Upvotes: 0

Peter Haddad
Peter Haddad

Reputation: 80904

Change this:

String value = dataSnapshot.getValue().toString();

into this:

String value = dataSnapshot.child("message").getValue(String.class);

Upvotes: 1

Related Questions