Could some take a look at the below and try and explain what I’m missing? I’ve used the below method before in an application and its worked perfectly. Im sure its something silly I have missed and would love to get someone to look over it…
I input the air volume and pressure and click on my RadioButton ForwardCBD. This should create a double power, this should then be outputted to the screen but instead it causes the app to crash out.
Any ideas?
EditText AirVolume, Pressure,FCBD,BCBD,BCDD;
TextView Power;
RadioGroup Fans;
RadioButton ForwardCBD, BackwardCBD, BackwardCDD;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.car);
AirVolume=(EditText)findViewById(R.id.AirVolume);
Pressure=(EditText)findViewById(R.id.Pressure);
Power=(TextView)findViewById(R.id.Power);
ForwardCBD=(RadioButton)findViewById(R.id.ForwardCBD);
BackwardCBD=(RadioButton)findViewById(R.id.BackwardCBD);
BackwardCDD=(RadioButton)findViewById(R.id.BackwardCDD);
Fans=(RadioGroup)findViewById(R.id.Fans);
Fans.setOnCheckedChangeListener(this);
}
public void onCheckedChanged(RadioGroup Fans, int aplication) {
if (aplication==R.id.ForwardCBD){
double airVolume = Double.parseDouble(AirVolume.getText().toString());
double pressure = Double.parseDouble(Pressure.getText().toString());
double fCBD = Double.parseDouble(FCBD.getText().toString());
double power = (airVolume * pressure) / (fCBD /100);
DecimalFormat df = new DecimalFormat();
Power.setText(df.format(power)+("W"));
}
if (aplication==R.id.BackwardCBD){
double airVolume = Double.parseDouble(AirVolume.getText().toString());
double pressure = Double.parseDouble(Pressure.getText().toString());
double bCBD = Double.parseDouble(BCBD.getText().toString());
double power = (airVolume * pressure) / (bCBD /100);
DecimalFormat df = new DecimalFormat();
Power.setText(df.format(power)+("W"));
}
if (aplication==R.id.BackwardCDD){
double airVolume = Double.parseDouble(AirVolume.getText().toString());
double pressure = Double.parseDouble(Pressure.getText().toString());
double bCDD = Double.parseDouble(BCDD.getText().toString());
double power = (airVolume * pressure) / (bCDD /100);
DecimalFormat df = new DecimalFormat();
Power.setText(df.format(power)+("W"));
}
}
}
See the LogCat below.
Not really sure how to present it in a decent format on here so sorry in advance!
08-19 12:10:24.576: E/AndroidRuntime(11733): FATAL EXCEPTION: main
08-19 12:10:24.576: E/AndroidRuntime(11733): java.lang.NullPointerException
08-19 12:10:24.576: E/AndroidRuntime(11733): at com.test.will.CarSystems.onCheckedChanged(CarSystems.java:46)
08-19 12:10:24.576: E/AndroidRuntime(11733): at android.widget.RadioGroup.setCheckedId(RadioGroup.java:172)
08-19 12:10:24.576: E/AndroidRuntime(11733): at android.widget.RadioGroup.access$600(RadioGroup.java:52)
08-19 12:10:24.576: E/AndroidRuntime(11733): at android.widget.RadioGroup$CheckedStateTracker.onCheckedChanged(RadioGroup.java:342)
08-19 12:10:24.576: E/AndroidRuntime(11733): at android.widget.CompoundButton.setChecked(CompoundButton.java:128)
08-19 12:10:24.576: E/AndroidRuntime(11733): at android.widget.CompoundButton.toggle(CompoundButton.java:87)
08-19 12:10:24.576: E/AndroidRuntime(11733): at android.widget.RadioButton.toggle(RadioButton.java:75)
08-19 12:10:24.576: E/AndroidRuntime(11733): at android.widget.CompoundButton.performClick(CompoundButton.java:99)
08-19 12:10:24.576: E/AndroidRuntime(11733): at android.view.View$PerformClick.run(View.java:14400)
08-19 12:10:24.576: E/AndroidRuntime(11733): at android.os.Handler.handleCallback(Handler.java:605)
08-19 12:10:24.576: E/AndroidRuntime(11733): at android.os.Handler.dispatchMessage(Handler.java:92)
08-19 12:10:24.576: E/AndroidRuntime(11733): at android.os.Looper.loop(Looper.java:154)
08-19 12:10:24.576: E/AndroidRuntime(11733): at android.app.ActivityThread.main(ActivityThread.java:4945)
08-19 12:10:24.576: E/AndroidRuntime(11733): at java.lang.reflect.Method.invokeNative(Native Method)
08-19 12:10:24.576: E/AndroidRuntime(11733): at java.lang.reflect.Method.invoke(Method.java:511)
08-19 12:10:24.576: E/AndroidRuntime(11733): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
08-19 12:10:24.576: E/AndroidRuntime(11733): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
08-19 12:10:24.576: E/AndroidRuntime(11733): at dalvik.system.NativeStart.main(Native Method)
Where have you assigned components to FCBD,BCBD,BCDD; which are probably null pointers.
your calling
But where have you defined/assigned FCBD i.e.
Otherwise, step through in debug mode and you will find that one of your properties is null, which your trying to call a method upon.