Benjamin Starostka Jakobsen Benjamin Starostka Jakobsen - 6 months ago 16
Java Question

Pythagoras in android application

Hey guys I would really like to calculate not only the c=hypotenuse but also a or b out from what was given in the 3 EditText-Views. But it crashes and won't output a result. Any suggestions? I'm pretty new to this stuff. :)

package net.starostka.somefunctions;

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

public class Pythagoras extends AppCompatActivity implements View.OnClickListener {

EditText aNumPyt;
EditText bNumPyt;
EditText cNumPyt;

Button pythagorasCalcu;

TextView pythagorasResultFinal;

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

aNumPyt = (EditText) findViewById(R.id.aNumPyt);
bNumPyt = (EditText) findViewById(R.id.bNumPyt);

pythagorasCalcu = (Button) findViewById(R.id.pythagorasCalcu);

pythagorasResultFinal = (TextView) findViewById(R.id.pythagorasResultFinal);

// set a listener
pythagorasCalcu.setOnClickListener(this);
}

@Override
public void onClick(View v) {
double a=0,b=0,c=0;
double hypot = 0.0;

/*
// check if the fields are empty
if (TextUtils.isEmpty(aNumPyt.getText().toString()) || TextUtils.isEmpty(bNumPyt.getText().toString()) || TextUtils.isEmpty(cNumPyt.getText().toString())) {
return;
}
*/

// read EditText and fill variables with numbers
a = Double.parseDouble(aNumPyt.getText().toString());
b = Double.parseDouble(bNumPyt.getText().toString());
c = Double.parseDouble(cNumPyt.getText().toString());

if (a>b && a>c){
hypot=a;
if(hypot*hypot==(b*b+c*c)){
//result = Math.sqrt(Math.pow(a,2)+Math.pow(b,2));
pythagorasResultFinal.setText(String.valueOf(hypot));
}
} else if (b>a && b>c){
hypot=b;
if(hypot*hypot==(a*a+c*c)){
//result = Math.sqrt(Math.pow(a,2)+Math.pow(b,2));
pythagorasResultFinal.setText(String.valueOf(hypot));
}
} else if (c>a && c>b){
hypot=c;
if(hypot*hypot==(a*a+b*b)){
//result = Math.sqrt(Math.pow(a,2)+Math.pow(b,2));
pythagorasResultFinal.setText(String.valueOf(hypot));
}
}
}


}

The XML file














</LinearLayout>
<TextView
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_marginLeft="5pt"
android:layout_marginRight="5pt"
android:textSize="12pt"
android:layout_marginTop="3pt"
android:id="@+id/pythagorasResultFinal"
android:gravity="center_horizontal">
</TextView>




Before crash:



After crash:


Answer

EDIT Interesting that no one pointed this out earlier, but I just realized you never initialize cNumPyt. You initialize the other EditTexts but never C. So when you try to get the text, you get NullPointerException because you try to get information from nothing.

Initialize the correct text box in your onCreate method.


In your image, you show that you are leaving input blank for one of the text boxes (in the "after crash" image, we see that you entered 3, 4, and then nothing). This means that there is no Double to be parsed, because "" is not a number.

From the Double documentation:

public static double parseDouble(String s) throws NumberFormatException
[...]
Throws:
NullPointerException - if the string is null
NumberFormatException - if the string does not contain a parsable double.

If your input is blank, you'll get an exception. Since it's not in a try-catch block, your application will crash.

To be more specific:

//cNumPyt is empty, and "" is not a parseable number, so NumberFormatException
c = Double.parseDouble(cNumPyt.getText().toString());

Sample block (you will need to do this for every variable separately so you can tell which variable was empty):

try{
    a = Double.parseDouble(aNumPyt.getText().toString());
}catch (NumberFormatException e){
    //oops, no number available
    a = 0; //default to 0 (or whatever you choose)
}