0
I’m not able to do a simple BMI calculation. This is the error that appears in the image.
Code:
ButtonCalcular.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
/* Variáveis que recebem o conteúdo do Edit*/
String valorPeso = EdtPeso.getText().toString();
String valorAltura = EdtAltura.getText().toString();
if (isCampoVazio(valorPeso) || (isCampoVazio(valorAltura))){
Toast.makeText(getApplicationContext(), R.string.MSG_Preencher_Campos, Toast.LENGTH_SHORT).show();
}else{
double imc = CalculaImc(Double.parseDouble(valorPeso), Double.parseDouble(valorAltura));
TextImc.setText(String.valueOf(imc));
}
}
});
Method of calculating BMI:
//FUNÇÃO QUE FAZ O CÁLCULO DO ÍNDICE DE MASSA CORPORAL
private double CalculaImc(double peso, double altura){
double resultado = peso/(altura * altura);
return resultado;
}
Error:
E/AndroidRuntime: FATAL EXCEPTION: main Process: net.ddns.tiyuri.trimc, PID: 14839 java.lang.RuntimeException: Unable to start activity ComponentInfo{net.ddns.tiyuri.trimc/net.ddns.tiyuri.trimc.Calcular}: 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:2830) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2905) at android.app.ActivityThread.-wrap11(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1606) at android.os.Handler.dispatchMessage(Handler.java:105) at android.os.Looper.loop(Looper.java:169) at android.app.ActivityThread.main(ActivityThread.java:6595) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 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 net.ddns.tiyuri.trimc.Calcular.onCreate(Calcular.java:64) at android.app.Activity.performCreate(Activity.java:7016) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2783) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2905) at android.app.ActivityThread.-wrap11(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1606) at android.os.Handler.dispatchMessage(Handler.java:105) at android.os.Looper.loop(Looper.java:169) at android.app.ActivityThread.main(ActivityThread.java:6595) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) I/zygote64: Do partial code cache collection, code=23KB, data=28KB I/zygote64: After code cache collection, code=23KB, data=28KB I/zygote64: Increasing code cache capacity to 128KB I/zygote64: Do partial code cache collection, code=23KB, data=48KB I/zygote64: After code cache collection, code=23KB, data=48KB I/zygote64: Increasing code cache capacity to 256KB I/zygote64: Compiler allocated 8MB to compile void android.widget.TextView.<init>(android.content.Context, android.util.AttributeSet, int, int)
Please send the error in text form, it is easier to analyze what is happening, are missing some parts of the exception
– RafaelFerreira
all right, I’ll do it.
– Yuri Ferreira
I edited the question, putting the code and the error. Sorry for anything, this is the first time I use Stack Overflow.
– Yuri Ferreira
The button
ButtonCalcular
does not exist in the Activity– Valdeir Psr
Valdeir, it was exactly that, I saw here and really when referencing the name of the button, I put the name of another button. thanks for the help.
– Yuri Ferreira