I have 10 textviews each with the following id(s) tv1, tv2, tv3, etc. and each have text in them. These textviews are clickable, I saved the resource ID of the clicked textview in sharedprefrence. This ID is an int.
Now using this resource ID, can I somehow get text attached to this resource(TextView here). Below is code the code I am trying to use, but it gives a null pointer exception.
if (!sharedPref.getString(String.valueOf(i), "error").equals("error")) {
int u = Integer.parseInt (sharedPref.getString(String.valueOf(i), "error"));
System.out.println("For "+i+" object Value of id retrieved is-"+u);
String dohass= ((TextView) findViewById(u)).getText().toString();
}
I have checked value of u and is correct(correct resource id), but the above code says I am using getText on a null object.
Here are the logs
java.lang.RuntimeException: Unable to start activity ComponentInfo{dhritiapps.tulsiramayan/dhritiapps.tulsiramayan.Fav}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.widget.TextView.getText()' on a null object reference