-4
package com.example.teopeishen.test2;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class First extends AppCompatActivity {

    public Button but;
    public void change(){
        but = (Button) findViewById(R.id.buttonGenerate);
        View.OnClickListener click = new View.OnClickListener(){
          @Override
            public void onClick (View v){
              Intent calc = new Intent (First.this,Second.class);
              startActivity(calc);

              Bundle bundle = new Bundle();
              bundle.putString("testName",testName.getText().toString());
              bundle.putDouble("writeMark3",writeMark3);
              bundle.putDouble("speakMark3",speakMark3);
              bundle.putDouble("listenMark3",listenMark3);
              bundle.putDouble("finalMark3",finalMark3);

              Intent intent = new Intent(First.this,Second.class);
              intent.putExtras(bundle);
              startActivity(intent);
          }
        };
        but.setOnClickListener(click);
    }


    private EditText testName;
    private EditText writeMark;
    private EditText speakMark;
    private EditText listenMark;

    private Double writeMark2;
    private Double speakMark2;
    private Double listenMark2;

    private Double writeMark3;
    private Double speakMark3;
    private Double listenMark3;
    private Double finalMark3;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first);
        testName = (EditText) findViewById(R.id.editTextName);
        writeMark = (EditText) findViewById(R.id.editTextWrite);
        speakMark = (EditText) findViewById(R.id.editTextSpeak);
        listenMark = (EditText) findViewById(R.id.editTextListen);



        try {
            writeMark2 = Double.valueOf(writeMark.getText().toString());
            speakMark2 = Double.valueOf(speakMark.getText().toString());
            listenMark2 = Double.valueOf(listenMark.getText().toString());

            writeMark3 = writeMark2 / 100 * 50;
            speakMark3 = speakMark2 / 100 * 30;
            listenMark3 = listenMark2 / 100 * 20;
            finalMark3 = writeMark3 + speakMark3 + listenMark3;




        } catch (Exception e) {
            finalMark3 = 0.0;

        }
        change();

    }
}

I try to addextra bundle and but get and error of

java.lang.NullPointerException: Attempt to invoke virtual method 'double java.lang.Double.doubleValue()' on a null object reference at com.example.teopeishen.test2.First$1.onClick(First.java:23). What it means? Anyone know about it. It say the problem is within line 23 which is bundle.putDouble("writeMark3",writeMark3);

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
TeoPeiShen
  • 43
  • 1
  • 7
  • Use your change() method after variable Initialized. – Vinothkumar Nataraj Oct 10 '17 at 12:37
  • i already try to modify the code. Instead of writeMark3 = writeMark2/100*50, i write writeMark3 = new Dobule (writeMark2/100*50). But it did not work too. – TeoPeiShen Oct 10 '17 at 12:55
  • that's because an exception is thrown and that code doesn't execute... writeMark.getText() is null, calling writeMark.getText().toString() throws exception, catch block is exectued – Bjelis Oct 10 '17 at 12:58
  • Please be more specific how to fix it. Ok, i try to add code to catch(){writeMark3=0;speakMark3 =0;finalMark3=0,listenMark3=0;}then the app did not crashed but the points is why it comes to exception. I did type some value into the editText box. – TeoPeiShen Oct 10 '17 at 13:04
  • In your code you get text from EditText in onCreate. That means you get values only once when the activity starts and then you ignore any input in the EditTexts, check my answer, this is exactly what I already told you already. You have to get your EditText values in the OnClickListener... and read some tutorial on android basics. – Bjelis Oct 11 '17 at 08:57
  • You are right. My bad. – TeoPeiShen Oct 11 '17 at 12:41

2 Answers2

0

writeMark3 is null but that is not the biggest problem you have. In fact, you have quite a few of them.

  1. any EditText input after onCreate is ignored
  2. the try-catch in onCreate is causing most of your relevant variables to be null (including writeMark3)
  3. the Bundle in onClick is not used at all, you start an activity with an empty Intent and then initialize the Bundle you should've sent with that Intent

Providing a complex answer would mean explaining programming basics to you so maybe this is not a good place to start acquiring programming skills.

Bjelis
  • 116
  • 7
0

Change your First class Activity like this

public class First extends AppCompatActivity {

private EditText testName;
private EditText writeMark;
private EditText speakMark;
private EditText listenMark;

private Double writeMark2;
private Double speakMark2;
private Double listenMark2;

private Double writeMark3;
private Double speakMark3;
private Double listenMark3;
private Double finalMark3;

public Button but;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_first);
    testName = (EditText) findViewById(R.id.editTextName);
    writeMark = (EditText) findViewById(R.id.editTextWrite);
    speakMark = (EditText) findViewById(R.id.editTextSpeak);
    listenMark = (EditText) findViewById(R.id.editTextListen);

    but = (Button) findViewById(R.id.buttonGenerate);

    but.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            getValues();
            Bundle bundle = new Bundle();
            bundle.putString("testName", testName.getText().toString());
            bundle.putDouble("writeMark3", writeMark3);
            bundle.putDouble("speakMark3", speakMark3);
            bundle.putDouble("listenMark3", listenMark3);
            bundle.putDouble("finalMark3", finalMark3);

            Intent intent = new Intent(First.this, Second.class);
            intent.putExtras(bundle);
            startActivity(intent);
        }
    });
}

public void getValues() {
    try {
        writeMark2 = Double.valueOf(writeMark.getText().toString());
        speakMark2 = Double.valueOf(speakMark.getText().toString());
        listenMark2 = Double.valueOf(listenMark.getText().toString());

        writeMark3 = writeMark2 / 100 * 50;
        speakMark3 = speakMark2 / 100 * 30;
        listenMark3 = listenMark2 / 100 * 20;
        finalMark3 = writeMark3 + speakMark3 + listenMark3;


    } catch (Exception e) {
        finalMark3 = 0.0;

    }
  }
}

Changes made are:

1.You are computing the editText value initially and not at the time of button click,so the values were not getting updated.

2.Bundle value was not used as you were calling intent two times inside onclick function.

Sharath kumar
  • 4,064
  • 1
  • 14
  • 20