0

I am trying to pass a double value from one class (MaterialCollect), to another Class (Main Activity). I am receiving a NullPointerException error in my Main Activity class when I try to define rho_m. I have followed passing a double with no success. I have attached an exert of my MainActivity, MaterialCollect and MatrixBase class files. According to the logcat the issue seems to be in the line not successfully creating a new Bundle. I am new to Java thank you.

public class MainActivity extends Activity{

double rho_m;
double E_maxial;
double E_mtrans;
double UTS_m;
double v_m;

@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);

    Intent intent;
    intent = new Intent(MainActivity.this, MaterialCollect.class);
    startActivity(intent);

    Intent it = new Intent();
    Bundle nylon66params = it.getExtras();
    rho_m = nylon66params.getDouble("nylon66.matrixrho");
    Log.d("print out","THE VALUE OF " + Double.toString(rho_m));


 }}

MaterialCollect class:

public class MaterialCollect extends AppCompatActivity {

MatrixBase nylon66 = new MatrixBase(1140, 2.7e9, 2.7e9, 2800e6, 0.33);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Intent it = new Intent(MaterialCollect.this, MainActivity.class);
    Bundle nylon66values = new Bundle();
    nylon66values.putDouble("nylon66.matrixrho", nylon66.matrixrho);
    nylon66values.putDouble("nylon66.matrixaxialTmodulus", nylon66.matrixaxialTmodulus);
    nylon66values.putDouble("nylon66.matrixtransTmodulus", nylon66.matrixtransTmodulus);
    nylon66values.putDouble("nylon66.matrixpoissons",nylon66.matrixpoissons);
    nylon66values.putDouble("nylon66.UTS",nylon66.matrixpoissons);
    it.putExtras(nylon66values);
    Log.d("ADebugTag", "Value: " + Double.toString(nylon66.matrixrho));
    startActivity(it);
}}

and MatrixBase

public class MatrixBase {

double matrixrho;
double matrixaxialTmodulus;
double matrixtransTmodulus;
double matrixpoissons;
double matrixUTS;

MatrixBase(double matrixrho, double matrixaxialTmodulus, double matrixtransTmodulus, double matrixpoissons, double matrixUTS) {
    this.matrixrho = matrixrho;
    this.matrixaxialTmodulus = matrixaxialTmodulus;
    this.matrixtransTmodulus = matrixtransTmodulus;
    this.matrixpoissons = matrixpoissons;
    this.matrixUTS = matrixUTS;
}}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433

3 Answers3

1

From what you have posted i am guessing you need to get a value back in your MainActivity.

Your MainActivity code is incorrect (you are creating a new Bundle and additionnaly your are calling back your MaterialCollect Activity).

You need simply to get the extras from the callee using getIntent().getExtras()

@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);

    Bundle b = getIntent().getExtras();
    rho_m = b.getDouble("nylon66.matrixrho");
    if (rho_m != null)
    {
        Log.d("print out","THE VALUE OF " + Double.toString(rho_m));
    }   
 }
Laurent B
  • 2,200
  • 19
  • 29
  • Hi Laurent, thanks for this - I have tried it but it still returns a nullpointerexception, do you think there could be an issue with my manifest that is causing it not to work? – mdeforestbrown Jan 05 '19 at 19:20
  • @mullone sorry edited code. Of course rho_m can still be null, for instance if you did not get your value from another activity – Laurent B Jan 07 '19 at 10:32
  • when I try this method (with your adjusted code) it returns the value of null, but as you can see from my code I want it to return the value of 1140. Do you know what I need to adjust to achieve 1140 not null? Thank you! – mdeforestbrown Jan 08 '19 at 20:47
0

As demidust points there is a loop... you start a MainActivity that starts a MaterialCollect which starts another MainActivity...

Check How to manage startActivityForResult on Android? if you need to get a value from another activity.

Marcks1
  • 15
  • 5
0

It is better to use Serialize class as you can pass the whole model to activity. please check below updated code

public class MatrixBase implements Serializable {

double matrixrho;
double matrixaxialTmodulus;
double matrixtransTmodulus;
double matrixpoissons;
double matrixUTS;

MatrixBase(double matrixrho, double matrixaxialTmodulus, double matrixtransTmodulus, double matrixpoissons, double matrixUTS) {
    this.matrixrho = matrixrho;
    this.matrixaxialTmodulus = matrixaxialTmodulus;
    this.matrixtransTmodulus = matrixtransTmodulus;
    this.matrixpoissons = matrixpoissons;
    this.matrixUTS = matrixUTS;
}}

Change code in MaterialCollect

 Intent it = new Intent(MaterialCollect.this, MainActivity.class);
 it.putExtra("it",nylon66)
 startActivity(it);

and get the whole item with the below code in MainActivity oncreate method

MatrixBase matrixBase= (MatrixBase)getIntent().getSerializableExtra("it");

then you can access all the data from the single model

Navin
  • 285
  • 1
  • 8
  • Hi Navin - thanks for this. I have tried your method in a completely new project and it still returns a nullpointerexception when I then try and define a double using: rho_m = matrixBase.matrixaxialTmodulus; do you know what could be causing this? – mdeforestbrown Jan 05 '19 at 19:22