0

I am building a feedback Android application, and i need to save the inputs but the shared preferences is causing a crash, and the internal storage is not working

  // Shared Preferences Code // Crashes
     final String radioValues =
                    ((RadioButton)findViewById(rGrop.getCheckedRadioButtonId()))
                            .getText().toString();
           final String nameValue = Ename.getText().toString();
           final String cityValue = Ecity.getText().toString();
           final String phoneNum = Ephone.getText().toString();
           final String comments = ans.getText().toString();
           final float rbarValue = rBar.getNumStars();
            final float rbarValue2 = rBar2.getNumStars();
            final float rbarValue3 = rBar3.getNumStars();

            SharedPreferences settings = getApplicationContext().getSharedPreferences("Visitors Data",MODE_PRIVATE );
            SharedPreferences.Editor editor = settings.edit();

            editor.putString("Visitor Name: ",nameValue);
            editor.putString("Visitor City: ",cityValue);
            editor.putString("visitor Phone Number: ",phoneNum);
            editor.putString("Radio Group Choice: ",radioValues);
            editor.putString("Comments: ",comments );
            editor.putFloat("rating bar : ", rbarValue);
            editor.putFloat("rating bar 2: ", rbarValue2);
            editor.putFloat("rating bar 3: ", rbarValue3);
            // save changes in SharedPreferences
            editor.apply();

and the next one is the Internal Storage Code

try{
            FileOutputStream fileout=openFileOutput("EnginesTechDATA.txt", MODE_PRIVATE);
            OutputStreamWriter outputWriter=new OutputStreamWriter(fileout);

            outputWriter.write(Ecity.getText().toString());
            outputWriter.write(Ename.getText().toString());
            outputWriter.write(Ephone.getText().toString());
outputWriter.write(ans.getText().toString());

outputWriter.write(rBar.getNumStars());
outputWriter.write(rBar2.getNumStars());
outputWriter.write(rBar3.getNumStars());

final String radioValues =
        ((RadioButton)findViewById(rGrop.getCheckedRadioButtonId()))
                .getText().toString();
outputWriter.write(radioValues);

outputWriter.close(); } catch (Exception e) {
e.printStackTrace(); }

i hope someone can help i need to save these data by any way and i need it by tomorrow

These are the Errors

02-12 12:58:25.170 10593-10593/? E/CfgFilePolicy: ****ERROR: env CUST_POLICY_DIRS not set, use default 02-12 12:58:38.510 10593-10593/com.example.etq E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.etq, PID: 10593 java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.widget.RadioButton.getText()' on a null object reference at com.example.etq.MainActivity$1.onClick(MainActivity.java:154) at android.view.View.performClick(View.java:4792) at android.view.View$PerformClick.run(View.java:19938) 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:5601) 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:964) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:759)

XML Radio Group code

<RadioGroup
        android:id="@+id/Rgrp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"

        >

        <RadioButton
            android:id="@+id/rBtn1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_gravity="end"
            android:fontFamily="@font/lalezar"
            />

        <RadioButton
            android:id="@+id/rBtn2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:fontFamily="@font/lalezar"
            />

        <RadioButton
            android:id="@+id/rBtn3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:fontFamily="@font/lalezar"
            />
    </RadioGroup>

and this is the line in java file

        final RadioGroup rGrop = findViewById(R.id.Rgrp);
Zoe
  • 27,060
  • 21
  • 118
  • 148
  • post the error log with your question. – karan Feb 12 '19 at 09:56
  • done please see – Sara Mohammed Feb 12 '19 at 10:02
  • what is line 154 in main activity? – karan Feb 12 '19 at 10:09
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Zoe Apr 08 '19 at 04:56
  • [Here](https://stackoverflow.com/a/46093455/4116560) is an elegant way to use shared preferences. – vss Apr 08 '19 at 06:43
  • @SaraMohammed use editor.commit to save your shared preference in to cache. but it can work only till 1400 kb of data you can store in to shared preference in device if you are most frequently used shared preference then in that case your shared preference will either cache full or may be override sum perimeters of data. – Amitsharma May 02 '19 at 18:02

2 Answers2

1

Regarding your crash report, it seems like a Null Pointer Exception. You tried to call getText() method on a RadioButton object, but the radioButton view probably wasn't initialized. for example, you might have forgot to call radioButton = findViewById(R.id.radio_button_id); (this is syntax, use your RadioButton variable name and id)

1

As per as your crash log

Java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.widget.RadioButton.getText()'

You are trying to get text from null radio button

Your radio button is null and that is the cause of crash

Check your layout file and activity , make sure radio button is in xml and in activity you link it via findViewById with same id given in xml

After getting all your need i suggest you to call this method from where you want to perform action

public void saveCheckedBox()
{

//rGrop.getCheckedRadioButtonId() returns the id of the RadioButton(or -1 if no RadioButtons are checked) that is checked in the Radiogroup
if(rGrop.getCheckedRadioButtonId()==-1)
{
Toast.makeText(context, "All checkbox unchecked", Toast.LENGTH_SHORT).show();
return;
// if no radio button checked simply toast and return
}

     final String radioValues =
                    ((RadioButton)findViewById(rGrop.getCheckedRadioButtonId()))
                            .getText().toString();
           final String nameValue = Ename.getText().toString();
           final String cityValue = Ecity.getText().toString();
           final String phoneNum = Ephone.getText().toString();
           final String comments = ans.getText().toString();
           final float rbarValue = rBar.getNumStars();
            final float rbarValue2 = rBar2.getNumStars();
            final float rbarValue3 = rBar3.getNumStars();

            SharedPreferences settings = getApplicationContext().getSharedPreferences("Visitors Data",MODE_PRIVATE );
            SharedPreferences.Editor editor = settings.edit();

            editor.putString("Visitor Name: ",nameValue);
            editor.putString("Visitor City: ",cityValue);
            editor.putString("visitor Phone Number: ",phoneNum);
            editor.putString("Radio Group Choice: ",radioValues);
            editor.putString("Comments: ",comments );
            editor.putFloat("rating bar : ", rbarValue);
            editor.putFloat("rating bar 2: ", rbarValue2);
            editor.putFloat("rating bar 3: ", rbarValue3);
            // save changes in SharedPreferences
            editor.apply();


} 
Mayank Sharma
  • 2,735
  • 21
  • 26
  • final String radioValues = ((RadioButton) findViewById(rGrop.getCheckedRadioButtonId())).getText().toString(); > this is the code of the radio group – Sara Mohammed Feb 12 '19 at 10:53
  • rGrop.getCheckedRadioButtonId() return id of checked radio button id under "rGrop" so if any radio group not checked intially it will null . For more clarification can you please share "rGrop" initialization and xml code – Mayank Sharma Feb 12 '19 at 11:00
  • As you can see all your three radio button are unchecked and on this code of line " ((RadioButton) findViewById(rGrop.getCheckedRadioButtonId())).getText().toString()" you try to get text from checked radio button , as there is no checked radio button so it return null – Mayank Sharma Feb 12 '19 at 11:32
  • well cuz i need to know which one is checked and i need to save it. what do you think? – Sara Mohammed Feb 12 '19 at 11:34
  • Please check updated answer , you need to call saveCheckedBox method from anywhere like on button click or something else , if you want to save it on check/uncheck on radio button simply add setOnCheckedChangeListener on radio button and call this method there – Mayank Sharma Feb 12 '19 at 11:41