veer veer - 3 years ago 86
Java Question

I want to get text from radio button and store it in sqlite database but I am getting an error

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.

enter code here

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;

protected void onCreate(Bundle savedInstanceState) {

editText4 = (EditText) findViewById(;
editText6 = (EditText) findViewById(;



public void addListenerOnButton() {

time = (RadioGroup) findViewById(;
book = (Button) findViewById(;

book.setOnClickListener(new View.OnClickListener() {

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();

//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(
at android.view.View.performClick(
at android.view.View$
at android.os.Handler.handleCallback(
at android.os.Handler.dispatchMessage(
at android.os.Looper.loop(
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(
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.content.res.ColorStateList,$Mode) would have incorrectly overridden the package-private method in
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: : EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_LA.BF.1.1.1_RB1. ()
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. + 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

Answer Source

The problem might be in the RadioGroup time varaible because first you set the id to and in addListener function you change the id to the radioGroup Id which is correct in xml. Sure this help.Thanks

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download