user1296441
user1296441

Reputation: 3

Force Close on Button Pressed

I am a bit new with android but I have some java experience. So I decided to start to make a 'physics calculator' just to see if I could make an app after watching a bunch of tutorials on the internet. I have my xml file set up how I want, but all is not on the 'up and up'. I've tried a bunch of things, but I am somewhat lost in what I am doing. Basically, what I want to do with this code is to get what is in the appropriate edit text field(s) and turn them into integers when the appropriate button is pressed, but the application always force closes on my whenever I hit a button (but I am able to 'see' all of the graphics such as buttons and such). (For example, for velocity, it would only pull the time and distance since that is all the user supplies). Anyone know what I am doing wrong here?

package t.t.slash25;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Physics extends Activity implements OnClickListener
{
int v1 , d , t;
EditText velocity, distance , time;
Button calcVel, calcDis, calcTime;
TextView formula, result;

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

private void initialize() 
{
    formula = (TextView) findViewById(R.id.tvFormula);
    velocity = (EditText) findViewById(R.id.etVelocity);
    distance = (EditText) findViewById(R.id.etDistance);
    time = (EditText) findViewById(R.id.etTime);
    calcVel = (Button) findViewById(R.id.bVel);
    calcVel.setOnClickListener(this);
    calcDis = (Button) findViewById(R.id.bDis);
    calcDis.setOnClickListener(this);
    calcTime = (Button) findViewById(R.id.bTime);
    calcTime.setOnClickListener(this);
    result = (TextView) findViewById(R.id.tvResult);
}

public void onClick(View v) 
{

    switch (v.getId()) 
    {
         case R.id.bVel:   //when the button is pressed, convert the editText's to integers, do the formula, and set the text
        d = Integer.getInteger(distance.getText().toString());
        t = Integer.getInteger(time.getText().toString());
        v1 = (d/t);
        result.setText("Velocity = " + v1);
          break;

    }
  }
}

I'm really lost with all of this and I would appreciate any help anyone can give me.

P.S. Here is the logcat error messages:

03-27 15:26:58.959: E/AndroidRuntime(1084): FATAL EXCEPTION: main
03-27 15:26:58.959: E/AndroidRuntime(1084): java.lang.NullPointerException
03-27 15:26:58.959: E/AndroidRuntime(1084):     at t.t.slash25.Physics.onClick(Physics.java:47)
03-27 15:26:58.959: E/AndroidRuntime(1084):     at android.view.View.performClick(View.java:2532)
03-27 15:26:58.959: E/AndroidRuntime(1084):     at android.view.View$PerformClick.run(View.java:9277)
03-27 15:26:58.959: E/AndroidRuntime(1084):     at android.os.Handler.handleCallback(Handler.java:587)
03-27 15:26:58.959: E/AndroidRuntime(1084):     at android.os.Handler.dispatchMessage(Handler.java:92)
03-27 15:26:58.959: E/AndroidRuntime(1084):     at android.os.Looper.loop(Looper.java:143)
03-27 15:26:58.959: E/AndroidRuntime(1084):     at android.app.ActivityThread.main(ActivityThread.java:4196)
03-27 15:26:58.959: E/AndroidRuntime(1084):     at java.lang.reflect.Method.invokeNative(Native Method)
03-27 15:26:58.959: E/AndroidRuntime(1084):     at java.lang.reflect.Method.invoke(Method.java:507)
03-27 15:26:58.959: E/AndroidRuntime(1084):     at      com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-27 15:26:58.959: E/AndroidRuntime(1084):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-27 15:26:58.959: E/AndroidRuntime(1084):     at dalvik.system.NativeStart.main(Native Method)

Thanks a bunch!

Upvotes: 0

Views: 350

Answers (2)

Akhil
Akhil

Reputation: 14038

Change the above code for getting d and t to:

d = Integer.parseInt(distance.getText().toString().trim());
 t = Integer.parseInt(time.getText().toString().trim());
    Btw dont use int for storing v1, you may get 0 as the answer even if it is 0.37 or something like that.

Upvotes: 0

Alexei
Alexei

Reputation: 1068

The problem may be in this two lines:

d = Integer.getInteger(distance.getText().toString());
t = Integer.getInteger(time.getText().toString());

The Integer.getInteger() doesn't converts a String to Integer, you should instead use the Integer.parseInt(), like this:

d = Integer.parseInt(distance.getText().toString());
t = Integer.parseInt(time.getText().toString());

See this answer for more details: https://stackoverflow.com/a/3123350/284618

Upvotes: 2

Related Questions