i want to get text from radio button but i am getting an error
java.lang.NullPointerException: Attempt to invoke virtual method 'int android.widget.RadioGroup.getCheckedRadioButtonId()' on a null object reference
please tell me how can i get text from radio button and to remove NullPointerException
.
here is the code
public class BookActivity extends AppCompatActivity {
EditText editText4;
EditText editText6;
RadioGroup time;
Button book;
RadioButton radioButton;
//RadioButton radioButton, radioButton2, radioButton3, radioButton4, radioButton5, radioButton6, radioButton7, radioButton8, radioButton9;
Context context = this;
UserDbHelper userDbHelper;
SQLiteDatabase sqLiteDatabase;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.book);
editText4 = (EditText) findViewById(R.id.editText4);
editText6 = (EditText) findViewById(R.id.editText6);
time=(RadioGroup)findViewById(R.id.time);
book=(Button)findViewById(R.id.book);
addListenerOnButton();
}
public void addListenerOnButton() {
time = (RadioGroup) findViewById(R.id.radio);
book = (Button) findViewById(R.id.book);
book.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// get selected radio button from radioGroup
int selectedId = time.getCheckedRadioButtonId();
// find the radiobutton by returned id
radioButton = (RadioButton) findViewById(selectedId);
if (radioButton != null) {
String pname = editText4.getText().toString();
String rtime = radioButton.getText().toString();
userDbHelper = new UserDbHelper(context);
sqLiteDatabase = userDbHelper.getWritableDatabase();
userDbHelper.addInformations(pname, rtime, sqLiteDatabase);
Toast.makeText(BookActivity.this, "Data Saved", Toast.LENGTH_LONG).show();
userDbHelper.close();
}
//Toast.makeText(BookActivity.this,radioButton.getText(), Toast.LENGTH_SHORT).show();
}
});
}
}
here is the logcat
10-23 01:04:35.607 16858-16858/com.example.hp.appointit D/AndroidRuntime: Shutting down VM
---- Beginning of crash
10-23 01:04:35.621 16858-16858/com.example.hp.appointit E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.hp.appointit, PID: 16858
java.lang.NullPointerException: Attempt to invoke virtual method 'int android.widget.RadioGroup.getCheckedRadioButtonId()' on a null object reference
at com.example.hp.appointit.BookActivity$1.onClick(BookActivity.java:54)
at android.view.View.performClick(View.java:4785)
at android.view.View$PerformClick.run(View.java:19884)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5343)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
10-23 01:09:35.642 16858-16858/com.example.hp.appointit I/Process: Sending signal. PID: 16858 SIG: 9
10-23 01:09:36.449 22673-22673/com.example.hp.appointit W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
10-23 01:09:36.737 22673-22692/com.example.hp.appointit I/art: Background sticky concurrent mark sweep GC freed 8864(1707KB) AllocSpace objects, 1(16KB) LOS objects, 0% free, 16MB/16MB, paused 12.340ms total 74.187ms
10-23 01:09:36.944 22673-22673/com.example.hp.appointit E/DATABASE OPERATIONS: Database created / opened
10-23 01:09:36.985 22673-22714/com.example.hp.appointit D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
10-23 01:09:36.996 22673-22673/com.example.hp.appointit D/Atlas: Validating map...
10-23 01:09:37.034 22673-22714/com.example.hp.appointit I/Adreno-EGL: <qeglDrvAPI_eglInitialize:410>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_LA.BF.1.1.1_RB1.05.00.02.042.016_msm8226_LA.BF.1.1.1_RB1__release_AU ()
OpenGL ES Shader Compiler Version: E031.25.03.00
Build Date: 02/11/15 Wed
Local Branch:
Remote Branch: quic/LA.BF.1.1.1_rb1.10
Local Patches: NONE
Reconstruct Branch: AU_LINUX_ANDROID_LA.BF.1.1.1_RB1.05.00.02.042.016 + 62ca4eb + acd831d + 9f8b442 + e027a02 + cba30ba + 53c303a + a649d79 + 23e16f8 + 5e97da7 + cbd2a44 + 33d072a + 7aacf06 + 72b33e7 + 28f6f60 + b4c13d8 + NOTHING
10-23 01:09:37.036 22673-22714/com.example.hp.appointit I/OpenGLRenderer: Initialized EGL, version 1.4
10-23 01:09:37.060 22673-22714/com.example.hp.appointit D/OpenGLRenderer: Enabling debug mode 0