Click here to Skip to main content
15,893,368 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I appreciate your help with a problem with the ratingbar, I want to show the value of the ratingbar in a Toast when pressing a button, therefore each time you press the button, the value of the ratingbar should be displayed, however at the time pressing the button closes the application and debugging I get the following error.

"Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'float android.widget.RatingBar.getRating()' on a null object reference at com.org.notas.asembisencuesta.MainActivity.onClick(MainActivity.java:114) "

What I have tried:

<pre lang="java">```@Override
protected void onCreate(Bundle savedInstanceState) {
commentsFra = new ComentariosFragment();
navhost = new HomeFragment();
thanks = new ThanksFragment();
}

public void onClick(View view){
ratingBar = findViewById(R.id.ratingBarOne);
transaction = getSupportFragmentManager().beginTransaction();
switch (view.getId()){

case R.id.fin_encuesta:
transaction.replace(R.id.nav_host_fragment, thanks);
transaction.addToBackStack(null);
Handler handler = new Handler();
Runnable runnable = new Runnable() {
@Override
public void run() {
transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.nav_host_fragment, navhost);
transaction.addToBackStack(null);
transaction.commit();
}
};
handler.postDelayed(runnable,7000);
comments = findViewById(R.id.comments_dialog);
comments.setText("");
float rating;
rating = ratingBar.getRating(); //Linea 114
Toast.makeText(this, ":" + rating, Toast.LENGTH_SHORT).show();
break;
}
transaction.commit();
}} ```


Y este es el error que me genera:


E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.org.notas.asembisencuesta, PID: 7682
java.lang.IllegalStateException: Could not execute method for android:onClick
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(.java:414)
at android.view.View.performClick(View.java:7146)
at android.view.View.performClickInternal(View.java:7119)
at android.view.View.access$3500(View.java:803)
at android.view.View$(View.java:27533)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7386)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:980)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(.java:409)
at android.view.View.performClick(View.java:7146)
at android.view.View.performClickInternal(View.java:7119)
at android.view.View.access$3500(View.java:803)
at android.view.View$(View.java:27533)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7386)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:980)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'float android.widget.RatingBar.getRating()' on a null object reference
at com.org.notas.asembisencuesta.MainActivity.onClick(MainActivity.java:114)
at java.lang.reflect.Method.invoke(Native Method)```
Posted
Updated 29-Sep-20 6:13am

1 solution

The message means that ratingBar has not been initialised yet. You initialise it in the onClick method, but that has not operated when run starts. So you should add the following before line 114:
Java
ratingBar = findViewById(R.id.ratingBarOne);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900