0

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

1337
  • 11
  • 3
  • Android Studio is saying that my error is in this line: Button button3 = (Button) findViewById(R.id.button3); button3.setOnClickListener(new View.OnClickListener() I can't see what is wrong with this line.. any ideas? – 1337 Apr 24 '16 at 12:51
  • The error refers to a `TextView.setText()` method call where the `TextView ` is null. So check out where you are trying to set the text to a `TextView` and is the `TextView` properly initialized. For example the `findViewById()` call might not have found any `TextView` with the id that you gave. – Markus Kauppinen Apr 24 '16 at 15:57

0 Answers0