-4

I have an Activity that recieves some Strings and Floats (that I converted to Strings) to fill some TextFields. After that process I need to take these values and convert them to Doubles to operate them with the function "beta" and pass them to the next Activity, but something is going wrong. The program is crashing when I try to start the Activity "ConferenciaDosDados", but it was working fine before I create the Button "next" and the function "beta". What am I doing wrong?

Activity "ConferenciaDosDados":

package com.mateus.ligacoestubulares;

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

import java.math.BigDecimal;

public class ConferenciaDosDados extends AppCompatActivity {

EditText normal1;
EditText normal0;
EditText momento;
EditText teta;
EditText Fy1;
EditText Fyo;
EditText T1;
EditText To;
EditText BaseM;
EditText AlturaM;
EditText BaseB;
EditText AlturaB;
Button next;
Intent intentNext;

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

    normal1 = (EditText) findViewById(R.id.NormalM);
    normal0 = (EditText) findViewById(R.id.NormalB);
    momento = (EditText) findViewById(R.id.Momento);
    teta = (EditText) findViewById(R.id.Teta1);
    Fy1 = (EditText) findViewById(R.id.fy1);
    Fyo = (EditText) findViewById(R.id.fyo);
    T1 = (EditText) findViewById(R.id.t1);
    To = (EditText) findViewById(R.id.to);
    BaseM = (EditText) findViewById(R.id.b1);
    AlturaM = (EditText) findViewById(R.id.h1);
    BaseB = (EditText) findViewById(R.id.bo);
    AlturaB = (EditText) findViewById(R.id.ho);

    String normalM = getIntent().getExtras().getString("NormalM");
    String normalB = getIntent().getExtras().getString("NormalB");
    String momentoB = getIntent().getExtras().getString("MomentoB");
    String teta1 = getIntent().getExtras().getString("Teta1");
    String fy1 = getIntent().getExtras().getString("fy1");
    String fyo = getIntent().getExtras().getString("fyo");
    String t1 = getIntent().getExtras().getString("t1");
    String to = getIntent().getExtras().getString("to");

    Float baseMt = getIntent().getExtras().getFloat("baseM");
    String b1 = baseMt.toString();
    Float alturaMt = getIntent().getExtras().getFloat("alturaM");
    String h1 = alturaMt.toString();
    Float baseBz = getIntent().getExtras().getFloat("baseB");
    String bo = baseBz.toString();
    Float alturaBz = getIntent().getExtras().getFloat("alturaB");
    String ho = alturaBz.toString();

    normal1.setText(normalM);
    normal0.setText(normalB);
    momento.setText(momentoB);
    teta.setText(teta1);
    Fy1.setText(fy1);
    Fyo.setText(fyo);
    T1.setText(t1);
    To.setText(to);
    BaseM.setText(b1);
    AlturaM.setText(h1);
    BaseB.setText(bo);
    AlturaB.setText(ho);

    next = (Button) findViewById(R.id.prosseguir);
    intentNext = new Intent(ConferenciaDosDados.this, Resultados.class);

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

            double hM = Double.parseDouble(AlturaM.getText().toString());
            double hB = Double.parseDouble(AlturaB.getText().toString());

            String valorBeta = Double.toString(beta(hM, hB));

            intentNext.putExtra("beta", valorBeta);
            startActivity(intentNext);

        }
    });

}

public double beta(double h1, double h2){

    double b = (h1 / h2);

    b = Math.round(b*100.0)/100.0;

    return b;
}
}

Logcat:

--------- beginning of crash
09-03 22:00:48.299 2752-2752/com.mateus.ligacoestubulares E/AndroidRuntime: FATAL EXCEPTION: main
                                                                        Process: com.mateus.ligacoestubulares, PID: 2752
                                                                        java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mateus.ligacoestubulares/com.mateus.ligacoestubulares.ConferenciaDosDados}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                                                                            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                                            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                            at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                            at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                            at android.os.Looper.loop(Looper.java:148)
                                                                            at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                            at java.lang.reflect.Method.invoke(Native Method)
                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                         Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                                                                            at com.mateus.ligacoestubulares.ConferenciaDosDados.onCreate(ConferenciaDosDados.java:86)
                                                                            at android.app.Activity.performCreate(Activity.java:6237)
                                                                            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                                            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                                            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                            at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                            at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                            at android.os.Looper.loop(Looper.java:148) 
                                                                            at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                            at java.lang.reflect.Method.invoke(Native Method) 
                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • 1
    Show your logcats plz – Malik Ahsan Sep 03 '17 at 21:37
  • 1
    Welcome to Stack Overflow! You have posted way to much code in your question, which makes it unclear to us (and to future readers) exactly where the problem is. Please reduce your problem code to 10 lines or less. See: [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) and [How to Debug Small Programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Joe C Sep 03 '17 at 21:38

2 Answers2

1

Variable next is null when you call next.setOnClickListener() probably R.id.prosseguir is a wrong identifier

from56
  • 3,976
  • 2
  • 13
  • 23
0

It's an error occurring in the mapping of the next button. On this line:

next = (Button) findViewById(R.id.prosseguir);

Probably R.id.prosseguir is not the correct button id. So go to your xml activity_conferencia_dos_dados and be sure that the button id is correct

Rodrigo João Bertotti
  • 5,179
  • 2
  • 23
  • 34
  • That was it, thank you. I've copied the code from other Activity and forgot to change the button id. Sorry for my carelessness. – Mateus Nogueira Sep 03 '17 at 23:58