0

My app has multiple language support and I need to enter a string value to a database. Due to some later fetching of database entries I need to compare some user selected strings regardless of language to enter them in a database.

The user selects a radio button and I retrieve the selected value with

final String userSelection =
            ((RadioButton) findViewById(rb.getCheckedRadioButtonId())).getText().toString();

which by my understanding should get the string in the language that is selected (lets say Italian and English which would be Gamba or Leg). in my database i need to have 'leg' entered regardless of the language.

i tried to make a method like this:

public String mapLanguage (String part){
    if (part.equals(getString(R.string.leg))) return "leg";
    else if (part.equals(getString(R.string.head))) return "head";
    else if (part.equals(getString(R.string.arm))) return "arm";
    else return "back";
}

this method is part of a class that doesnt have any vies associated with it

public class Part extends AppCompatActivity

this method is called from the constructors and setter:

this.part = mapLanguage(part);

error i have refers to the MapLanguage method:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
itsmysterybox
  • 2,748
  • 3
  • 21
  • 26
  • Please take a look at this answer here... [Null Pointer Exception when calling getResources()](https://stackoverflow.com/questions/40636681/null-pointer-exception-when-calling-getresources) – RickV Sep 29 '18 at 12:41
  • Are you instantiating the class `Part` by using `new Part()`? You can not do that with activities. An activity must be shown in order for the `getString` method to work – ByteHamster Sep 29 '18 at 12:41

1 Answers1

0

it ended up my error was in design. i moved the mapping to another class, at the source, i.e. to the class that shows the view, that way i bypass the class i used before (without the view). the guys that answered on my question gave me the insights of how to solve it