views:

46

answers:

2

I'm working with integers and trying to do calculations that involve decimals at parts. I have used this same setup before and it had worked but for some reason this isn't working.

package table.periodic;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;

public class Gram2mol extends Activity implements AdapterView.OnItemSelectedListener {

private Spinner Spinner01;
private TextView elementnumber;
private EditText grams;
private Button calculate;
private TextView moles5;

String[] element={"Actinium", "Aluminum", "Americium", 
        "Antimony", "Argon", "Arsenic", "Astatine", "Barium", 
        "Berkelium", "Beryllium", "Bismuth", "Bohrium", "Boron", 
        "Bromine", "Cadmium", "Calcium", "Californium", "Carbon", 
        "Cerium", "Cesium", "Chlorine", "Chromium", "Cobalt", 
        "Copernicium", "Copper", "Curium", "Darmstadtium", "Dubnium", 
        "Dysprosium", "Einsteinium", "Erbium", "Europium", "Fermium", 
        "Fluorine", "Francium", "Gadolinium", "Gallium", "Germanium", 
        "Gold", "Hafnium", "Hassium", "Helium", "Holmium", 
        "Hydrogen", "Indium", "Iodine", "Iridium", "Iron", 
        "Krypton", "Lanthanum", "Lawrencium", "Lead", "Lithium", 
        "Lutetium", "Magnesium", "Manganese", "Meitnerium", "Mendelevium", 
        "Mercury", "Molybdenum", "Neodymium", "Neon", "Neptunium", 
        "Nickel", "Niobium", "Nitrogen", "Nobelium", "Osmium", 
        "Oxygen", "Palladium", "Phosphorus", "Platinum", "Plutonium", 
        "Polonium", "Potassium", "Praseodymium", "Promethium", "Protactinium", 
        "Radium", "Radon", "Rhenium", "Rhodium", "Roentgenium", "Rubidium", 
        "Ruthenium", "Rutherfordium", "Samarium", "Scandium", "Seaborgium", 
        "Selenium", "Silicon", "Silver", "Sodium", "Strontium", "Sulfur", 
        "Tantalum", "Technetium", "Tellurium", "Terbium", "Thallium", "Thorium", 
        "Thulium", "Tin", "Titanium", "Tungsten", "Ununpentium", "Ununquadium",
        "Ununtrium", "Uranium", "Vanadium", "Xenon", "Ytterbium", "Yttrium", "Zinc",
        "Zirconium"};

public void onCreate(Bundle icicle){
    super.onCreate(icicle);
    setContentView(R.layout.grams2moles);

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

    grams = (EditText) findViewById(R.id.grams);

    calculate = (Button) findViewById(R.id.calculate);
    calculate.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {my_func01();}});

    Spinner spin = (Spinner) findViewById(R.id.elements);
    spin.setOnItemSelectedListener((OnItemSelectedListener) this);

    ArrayAdapter<String> aa=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, element);

    aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spin.setAdapter(aa);
}

void my_func01(){
    int grams2;
    int elementnumber2;
    int moles2;

        grams2 = Integer.parseInt(grams.getText().toString());
        elementnumber2 = Integer.parseInt(elementnumber.getText().toString());

        moles2 = (grams2 * (1 / elementnumber2));

        moles5.setText("" + moles2);
    }

    public void setSpinner01(Spinner spinner01) {
        Spinner01 = spinner01;
    }

    public Spinner getSpinner01() {
        return Spinner01;
    }

    public void onItemSelected(AdapterView<?> parent, View v, int position, long id)
    {

    if ("Actinium".equals(element[position]))
    {elementnumber.setText("227");}

    if ("Aluminum".equals(element[position]))
    {elementnumber.setText("26.981538");}

    if ("Americium".equals(element[position]))
    {elementnumber.setText("243");}

    if ("Antimony".equals(element[position]))
    {elementnumber.setText("121.760");}

    if ("Argon".equals(element[position]))
    {elementnumber.setText("39.948");}

    if ("Arsenic".equals(element[position]))
    {elementnumber.setText("74.9216");}

    if ("Astatine".equals(element[position]))
    {elementnumber.setText("210");}

    if ("Barium".equals(element[position]))
    {elementnumber.setText("137.327");}

    if ("Berkelium".equals(element[position]))
    {elementnumber.setText("247");}

    if ("Bismuth".equals(element[position]))
    {elementnumber.setText("208.9804");}

    if ("Bohrium".equals(element[position]))
    {elementnumber.setText("264");}

    if ("Boron".equals(element[position]))
    {elementnumber.setText("10.811");}

    if ("Bromine".equals(element[position]))
    {elementnumber.setText("79.904");}

    if ("Cadmium".equals(element[position]))
    {elementnumber.setText("112.41");}

    if ("Calcium".equals(element[position]))
    {elementnumber.setText("40.078");}

    if ("Californium".equals(element[position]))
    {elementnumber.setText("251");}

    if ("Carbon".equals(element[position]))
    {elementnumber.setText("12.0107");}

    if ("Cerium".equals(element[position]))
    {elementnumber.setText("140.116");}

    if ("Cesium".equals(element[position]))
    {elementnumber.setText("132.90545");}

    if ("Chlorine".equals(element[position]))
    {elementnumber.setText("35.453");}

    if ("Chromium".equals(element[position]))
    {elementnumber.setText("51.996");}

    if ("Copper".equals(element[position]))
    {elementnumber.setText("63.546");}

    if ("Curium".equals(element[position]))
    {elementnumber.setText("247");}

    }

    public void onNothingSelected(AdapterView<?> arg0) {
        elementnumber.setText("227");
    }

}

my error is this

09-22 20:02:58.353: ERROR/AndroidRuntime(17745): Uncaught handler: thread main exiting due to uncaught exception
09-22 20:02:58.513: ERROR/AndroidRuntime(17745): java.lang.NullPointerException
09-22 20:02:58.513: ERROR/AndroidRuntime(17745):     at table.periodic.Gram2mol.my_func01(Gram2mol.java:77)
09-22 20:02:58.513: ERROR/AndroidRuntime(17745):     at table.periodic.Gram2mol$1.onClick(Gram2mol.java:56)
09-22 20:02:58.513: ERROR/AndroidRuntime(17745):     at android.view.View.performClick(View.java:2364)
09-22 20:02:58.513: ERROR/AndroidRuntime(17745):     at android.view.View.onTouchEvent(View.java:4179)
09-22 20:02:58.513: ERROR/AndroidRuntime(17745):     at android.widget.TextView.onTouchEvent(TextView.java:6592)
09-22 20:02:58.513: ERROR/AndroidRuntime(17745):     at android.view.View.dispatchTouchEvent(View.java:3709)
09-22 20:02:58.513: ERROR/AndroidRuntime(17745):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:924)
09-22 20:02:58.513: ERROR/AndroidRuntime(17745):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:924)
09-22 20:02:58.513: ERROR/AndroidRuntime(17745):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:924)
09-22 20:02:58.513: ERROR/AndroidRuntime(17745):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:924)
09-22 20:02:58.513: ERROR/AndroidRuntime(17745):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:924)
09-22 20:02:58.513: ERROR/AndroidRuntime(17745):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1695)
09-22 20:02:58.513: ERROR/AndroidRuntime(17745):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1116)
09-22 20:02:58.513: ERROR/AndroidRuntime(17745):     at android.app.Activity.dispatchTouchEvent(Activity.java:2068)
09-22 20:02:58.513: ERROR/AndroidRuntime(17745):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1679)
09-22 20:02:58.513: ERROR/AndroidRuntime(17745):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1697)
09-22 20:02:58.513: ERROR/AndroidRuntime(17745):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-22 20:02:58.513: ERROR/AndroidRuntime(17745):     at android.os.Looper.loop(Looper.java:123)
09-22 20:02:58.513: ERROR/AndroidRuntime(17745):     at android.app.ActivityThread.main(ActivityThread.java:4568)
09-22 20:02:58.513: ERROR/AndroidRuntime(17745):     at java.lang.reflect.Method.invokeNative(Native Method)
09-22 20:02:58.513: ERROR/AndroidRuntime(17745):     at java.lang.reflect.Method.invoke(Method.java:521)
09-22 20:02:58.513: ERROR/AndroidRuntime(17745):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
09-22 20:02:58.513: ERROR/AndroidRuntime(17745):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
09-22 20:02:58.513: ERROR/AndroidRuntime(17745):     at dalvik.system.NativeStart.main(Native Method)

line 77 is the "moles5.setText("" + moles2);"

I don't believe I'm getting any id's mixed up so I'm not worried about that and it doesn't seem to be that kind of error. If anyone has any ideas let me know. Thanks.

A: 

moles5 is the only thing on that line that can be null. You can't get a null pointer on a primitive type. It doesn't looks like you initialize moles5 anywhere though.

Also, a better way to print a number is to say Integer.toString(moles2);

Mayra
Thanks. I had to change to Doubles though. I couldn't get it to work using integers but I will keep that in mind. Thanks.
Joshua Sutherland
Ah, ok.. with doubles, you might consider using a DecimalFormat instead so that you can control how many decimal points are shown, etc. In fact it works for integers as well, if you want to show 1,000 instead of 1000. http://download-llnw.oracle.com/javase/1.4.2/docs/api/java/text/DecimalFormat.html
Mayra
A: 

It seems that you are not instantiating the moles5 variable. If there is a TextView in your layout/grams2moles.xml file with an id of moles5, you would instantiate it in onCreate() by calling

moles5 = (TextView) findViewById(R.id.moles5);
cfei
Ok. Thanks. I Didn't realize that. I've been doing that a bit lately. I've got it all working now.
Joshua Sutherland