I am trying to set a text to a textview in another activity, but im getting an error while the app is running :-
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
Here is the main activity :-
package com.example.testlab;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
RocketDetailsActivity rda;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button1 = findViewById(R.id.button);
Button button2 = findViewById(R.id.button2);
Button button3 = findViewById(R.id.button3);
Button button4 = findViewById(R.id.button4);
rda = new RocketDetailsActivity();
button1.setOnClickListener(v -> {
callRocketDetails();
setDetails("Custom Starship", "SFS Gaming",
"22-05-2020", "NIL", R.drawable.rocket1);
});
button2.setOnClickListener(v -> {
callRocketDetails();
setDetails("Eagle 1+", "SFS Gaming",
"22-05-2020", "NIL", R.drawable.rocket2);
});
button3.setOnClickListener(v -> {
callRocketDetails();
setDetails("Space Station", "SFS Gaming",
"22-05-2020", "NIL", R.drawable.rocket3);
});
button4.setOnClickListener(v -> {
callRocketDetails();
setDetails("Eagle 9+ 1st Stage", "SFS Gaming",
"22-05-2020", "NIL", R.drawable.rocket4);
});
}
public void callRocketDetails() {
Intent myIntent = new Intent(this, RocketDetailsActivity.class);
startActivity(myIntent);
}
public void setDetails(String rocketNameS, String rocketBuilderS, String rocketDateS,
String rocketDescS, int rocketImageS) {
RocketDetailsActivity rda = new RocketDetailsActivity();
rda.rocketName.setText(rocketNameS);
rda.rocketBuilder.setText(rocketBuilderS);
rda.rocketDate.setText(rocketDateS);
rda.rocketDesc.setText(rocketDescS);
rda.rocketImg.setImageResource(rocketImageS);
}
}
at this point, im getting error:-
(here)rda.rocketBuilder.setText(rocketBuilderS);
rda.rocketDate.setText(rocketDateS);
rda.rocketDesc.setText(rocketDescS);
rda.rocketImg.setImageResource(rocketImageS);
And here is the rocket details activity:-
package com.example.testlab;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class RocketDetailsActivity extends AppCompatActivity {
TextView rocketName;
TextView rocketBuilder;
TextView rocketDate;
TextView rocketDesc;
ImageView rocketImg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rocket_details);
rocketName = findViewById(R.id.textRocketName);
rocketBuilder = findViewById(R.id.textRocketBuilder);
rocketDate = findViewById(R.id.textRocketDate);
rocketDesc = findViewById(R.id.textRocketDesc);
rocketImg = findViewById(R.id.rocketImage);
Button downloadBP = findViewById(R.id.buttonDownloadBP);
Button back = findViewById(R.id.buttonBack);
Button exit = findViewById(R.id.buttonExit);
back.setOnClickListener(v -> {
Intent myIntent = new Intent(this, MainActivity.class);
startActivity(myIntent);
});
exit.setOnClickListener(v -> finish());
}
}
I know there are answers for NullPointerException out there, but I want the solution for this specific code. Please help me point it out. Thanks.