My object class doesn’t want to be passed with Intent.putExtra/Intent.getExtra

Published

I have a object class that I want to use it between activities.
I used exactly the same method for another activities and worked perfectly fine. But here I can’t figure it out what I am doing wrong. This is the object class :

`package com.mecachrome.ffbf;

 import android.os.Parcel;
 import android.os.Parcelable;

 public class topic implements Parcelable {
public static Creator<topic> getCREATOR() {
    return CREATOR;
}

public static final Creator<topic> CREATOR = new Creator<topic>() {
    @Override
    public topic createFromParcel(Parcel in) {
        return new topic(in);
    }

    @Override
    public topic [] newArray(int size) {
        return new topic[size];
    }
};

private String name;
private String comment;
private String id;
private String username;

public topic() {
}

public topic(String name, String comment, String id, String username) {
    this.name = name;
    this.comment = comment;
    this.id = id;
    this.username = username;
}

protected topic(Parcel in) {
    name = in.readString();
    comment = in.readString();
    id = in.readString();
    username = in.readString();
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getComment() {
    return comment;
}

public void setComment(String comment) {
    this.comment = comment;
}

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String getUsername() {
    return username;
}

public void setUsername(String username) {
    this.username = username;
}

@Override
public String toString() {
    return "topic{" +
            "name='" + name + ''' +
            ", comment='" + comment + ''' +
            ", id='" + id + ''' +
            ", username='" + username + ''' +
            '}';
}

@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(name);
    dest.writeString(comment);
    dest.writeString(id);
    dest.writeString(username);
}
}`

This is the class that I want to take the object from:

`package com.mecachrome.ffbf;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.Toast;

import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.util.ArrayList;

public class forum extends AppCompatActivity {

RecyclerView rv_forum;
forumAdapter myadapter;
Button add_topic;
Button test;
DatabaseReference databaseref;


private ArrayList<topic> topicList;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_forum);

    add_topic = findViewById(R.id.btn_new_topic);
    rv_forum = findViewById(R.id.rv_topics);
    rv_forum.setHasFixedSize(true);
    rv_forum.setLayoutManager(new LinearLayoutManager(this));
    test = findViewById(R.id.test_btn);

    topicList = new ArrayList<>();


    databaseref = FirebaseDatabase.getInstance().getReference("forum_topics");

    databaseref.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {

            for (DataSnapshot dss: snapshot.getChildren()){

                topic top = dss.getValue(topic.class);
                topicList.add(top);

            }

            myadapter = new forumAdapter(forum.this, topicList, mTopic -> {
                Intent intent = new Intent(forum.this, topic_chat.class);
                intent.putExtra("topic",mTopic);
                startActivity(intent);
            });
            rv_forum.setAdapter(myadapter);
        }

        @Override
        public void onCancelled(@NonNull DatabaseError error) {
            Toast.makeText(forum.this, error.getMessage(), Toast.LENGTH_SHORT).show();
        }

    });

    add_topic.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(forum.this, add_new_topic.class);
            startActivity(i);
        }
    });


}
}`

And this is the class that I want to transfer the object to:

`package com.mecachrome.ffbf;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Calendar;

public class topic_chat extends AppCompatActivity {

RecyclerView rv_chat;
chatAdapter myadapter;
Button send;
TextView topic_name;
TextView chat_text;
String chat;
private topic mTopic;

private ArrayList<chat> chatList;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_topic_chat);

    mTopic = getIntent().getParcelableExtra("topic");

    send = findViewById(R.id.btn_send_chat);
    rv_chat = findViewById(R.id.rv_topics);
    rv_chat.setHasFixedSize(true);
    rv_chat.setLayoutManager(new LinearLayoutManager(this));

    topic_name = findViewById(R.id.tv_topic_name);
    chat_text = findViewById(R.id.et_comment);

    chat = chat_text.getText().toString().trim();

    chatList = new ArrayList<>();



    DatabaseReference databaseref = FirebaseDatabase.getInstance().getReference("forum_topics").child(mTopic.getId()).child("chat");


    topic_name.setText(mTopic.getName());

    Calendar calendar = Calendar.getInstance();
    String currentDate = DateFormat.getDateInstance(DateFormat.SHORT).format(calendar.getTime());

    chatList = new ArrayList<>();

    databaseref.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {

            for (DataSnapshot dss: snapshot.getChildren()){

                chat mess = dss.getValue(chat.class);
                chatList.add(mess);

            }

            myadapter = new chatAdapter(topic_chat.this, chatList, chat -> {
                Intent intent = new Intent(topic_chat.this, topic_chat.class);
                startActivity(intent);
            });
            rv_chat.setAdapter(myadapter);
        }

        @Override
        public void onCancelled(@NonNull DatabaseError error) {
            Toast.makeText(topic_chat.this, error.getMessage(), Toast.LENGTH_SHORT).show();
        }

    });

    send.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            chat msg = new chat(mTopic.getName(),chat,mTopic.getId(),currentDate);
            databaseref.setValue(msg).addOnCompleteListener(task1 -> {

                if (task1.isSuccessful()){
                    Toast.makeText(topic_chat.this, "Review was been added Successfully", Toast.LENGTH_LONG).show();
                }else{
                    Toast.makeText(topic_chat.this, "Review failed to post. Try Again!", Toast.LENGTH_SHORT).show();
                }
            });;
        }
    });
}
}`

The object class is "topic"

The error:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.mecachrome.ffbf.topic.getId()' on a null object reference

On this line
DatabaseReference databaseref = FirebaseDatabase.getInstance().getReference("forum_topics").child(mTopic.getId()).child("chat");

because mTopic.getId() is null. Any help?

Source: Android Questions

Published
Categorized as android, android-activity, android-intent, firebase, object

Answers

Leave a Reply

Still Have Questions?


Our dedicated development team is here for you!

We can help you find answers to your question for as low as 5$.

Contact Us
faq