This is my error:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
This is part of my code:
public static final String PLACEHOLDER_LIST_FILE = "placeholder_file";
public static final String STORY_NUMBER_FILE = "story_number_file";
Button button3;
Story story;
EditText placeholderField;
TextView wordsLeftText;
Set<String> placeholderList;
int storyIndex;
int placeholderCount;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.display);
// Restore preferences
SharedPreferences placeholderSettings = getSharedPreferences(PLACEHOLDER_LIST_FILE, 0);
placeholderList = placeholderSettings.getStringSet("placeholders", null);
if (placeholderList == null) {
placeholderList = new HashSet<>();
}
SharedPreferences storyNumberSettings = getSharedPreferences(STORY_NUMBER_FILE, 0);
storyIndex = storyNumberSettings.getInt("story_number", 404);
storyIndex = 404; // Used to test spinner
if (storyIndex == 404) {
storyIndex = getIntent().getIntExtra("selected_story", 404);
}
InputStream is;
is = this.getResources().openRawResource(getStoryNumber(storyIndex));
story = new Story(is);
for (String currentPlaceholder : placeholderList) {
story.fillInPlaceholder(currentPlaceholder);
}
@Override
protected void onStop() {
super.onStop();
SharedPreferences placeholderSettings = getSharedPreferences(PLACEHOLDER_LIST_FILE, 0);
SharedPreferences.Editor editor1 = placeholderSettings.edit();
editor1.putStringSet("placeholders", placeholderList);
editor1.apply();
SharedPreferences storyNumberSettings = getSharedPreferences(STORY_NUMBER_FILE, 0);
SharedPreferences.Editor editor2 = storyNumberSettings.edit();
editor2.putInt("story_number", storyIndex);
editor2.apply();
}
What I want to fix:
When I press the button in the second activity to continue to the 'input'activity. Without getting a crash and the nullpointer error.
What have I done to try and fix it:
Look at people who also had the problem, but their fixes don't apply to my code. I have also no idea why I can't declare it to null.
Any help would be appreciated.
p.s. If you guys see any error in my code please tell me