-1

I am trying to multiply two EditText, both editHours and editWage and display results in a TextView (gross) any help is much appreciated

public class PayrollActivity extends Activity implements OnClickListener {

 TextView gross, net, calculated;
 EditText editHours, editWage;
 int hours, wage, intGross, intNet, pay;

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


     gross = (TextView) findViewById(R.id.gross);
     hours= Integer.parseInt(editHours.getText().toString());
     wage = Integer.parseInt(editWage.getText().toString());
     gross.setText(hours*wage);
     net = (TextView) findViewById(R.id.net);
     calculated = (TextView) findViewById(R.id.calculated);

        ((Button) findViewById(R.id.addButton)).setOnClickListener(new     OnClickListener() {
         @Override
         public void onClick(View v) {
             // TODO Auto-generated method stub
             StringBuilder str = new StringBuilder("It worked");
             calculated.setText(str);
         }
     });    
    }

The app is crashing at

hours= Integer.parseInt(editHours.getText().toString());

LogCat

05-26 14:08:36.591: E/AndroidRuntime(1659): FATAL EXCEPTION: main
05-26 14:08:36.591: E/AndroidRuntime(1659): Process: co.mytms.payroll, PID: 1659
05-26 14:08:36.591: E/AndroidRuntime(1659): java.lang.RuntimeException: Unable to start activity ComponentInfo{co.mytms.payroll/co.mytms.payroll.PayrollActivity}: java.lang.NullPointerException
05-26 14:08:36.591: E/AndroidRuntime(1659):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-26 14:08:36.591: E/AndroidRuntime(1659):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-26 14:08:36.591: E/AndroidRuntime(1659):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-26 14:08:36.591: E/AndroidRuntime(1659):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-26 14:08:36.591: E/AndroidRuntime(1659):     at android.os.Handler.dispatchMessage(Handler.java:102)
05-26 14:08:36.591: E/AndroidRuntime(1659):     at android.os.Looper.loop(Looper.java:136)
05-26 14:08:36.591: E/AndroidRuntime(1659):     at android.app.ActivityThread.main(ActivityThread.java:5017)
05-26 14:08:36.591: E/AndroidRuntime(1659):     at java.lang.reflect.Method.invokeNative(Native Method)
05-26 14:08:36.591: E/AndroidRuntime(1659):     at java.lang.reflect.Method.invoke(Method.java:515)
05-26 14:08:36.591: E/AndroidRuntime(1659):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-26 14:08:36.591: E/AndroidRuntime(1659):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-26 14:08:36.591: E/AndroidRuntime(1659):     at dalvik.system.NativeStart.main(Native Method)
05-26 14:08:36.591: E/AndroidRuntime(1659): Caused by: java.lang.NullPointerException
05-26 14:08:36.591: E/AndroidRuntime(1659):     at co.mytms.payroll.PayrollActivity.onCreate(PayrollActivity.java:22)
05-26 14:08:36.591: E/AndroidRuntime(1659):     at android.app.Activity.performCreate(Activity.java:5231)
05-26 14:08:36.591: E/AndroidRuntime(1659):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-26 14:08:36.591: E/AndroidRuntime(1659):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
05-26 14:08:36.591: E/AndroidRuntime(1659):     ... 11 more
njzk2
  • 38,969
  • 7
  • 69
  • 107

2 Answers2

2

There are several problems.

Firstly when calling the getText() method on your EditTexts in onCreate they are still empty (and uninitialized see @Manishika answer). So Integer.parseInt will try to parse an empty String. Move this into the onClick method of the button, this is the behavior you want (i.e when you click on it, it multiplies the two values of both EditText and set the result to the TextView).

Secondly when doing gross.setText(hours*wage);, you will call the setText method that takes an int as parameter. The Android system will try to load the approriate String whose id corresponds to the one you defined in your strings.xml file. You need to call the method that accepts a CharSequence as argument. So do gross.setText(String.valueOf(hours*wage));

Also it's always good to add the stacktrace in your question, as it helps others to find faster your mistake.

Hope it helps! :)

Community
  • 1
  • 1
Alexis C.
  • 91,686
  • 21
  • 171
  • 177
1

You haven't initialized your EditTexts before getting texts from them

editHours = (EditText)findViewById(R.id.editHours_id);
editWage = (EditText)findViewById(R.id.editWage_id);

And also put your calculation inside onClick as initally it will return nothing but empty string.

 ((Button) findViewById(R.id.addButton)).setOnClickListener(new     OnClickListener() {
             @Override
             public void onClick(View v) {

//If your input contains decimal values also then use BigDecimal() 
//Following code will work for both int and decimal values
gross.setText(""+new BigDecimal(editHours.getText()
                        .toString()).multiply(new BigDecimal(editWage.getText()
                        .toString())));
                 StringBuilder str = new StringBuilder("It worked");
                 calculated.setText(str);



             }
         });    
Manishika
  • 5,478
  • 2
  • 22
  • 28