A
A
anya_hacker2022-01-10 14:49:49
Java
anya_hacker, 2022-01-10 14:49:49

How to fix error with context?

I have a class Questions in which I initialize arrays of strings.
Because need a context to get the Strings Resources, I pass it to the constructor when I instantiate the class in the MainActivity activity:

MainActivity.java

HashMap<String, String[]> questions = new HashMap(); // словарь: ключ - строка, значение - массив строк из Questions.java
Questions q = new Questions(this);

questions.put(getResources().getString(R.string.question1), q.arr1);
questions.put(getResources().getString(R.string.question2), q.arr2);
questions.put(getResources().getString(R.string.question3), q.arr3);


Questions, java
package ru.examplle.pravmin;
import ru.examplle.pravmin.R;
import android.content.Context;

public class Questions {
    private Context context;

    public Questions(Context cur) {
        this.context = cur;
    }


    String[] arr1 = {context.getResources().getString(R.string.answer1_for_question1), context.getResources().getString(R.string.answer2_for_question1), context.getResources().getString(R.string.answer3_for_question1), context.getResources().getString(R.string.answer4_for_question1), context.getResources().getString(R.string.right_answer_for_question1), context.getResources().getString(R.string.comment_for_question1)};
    String[] arr2 = {context.getResources().getString(R.string.answer1_for_question2), context.getResources().getString(R.string.answer2_for_question2), context.getResources().getString(R.string.answer3_for_question2), context.getResources().getString(R.string.answer4_for_question2), context.getResources().getString(R.string.right_answer_for_question2), context.getResources().getString(R.string.comment_for_question2)};
    String[] arr3 = {context.getResources().getString(R.string.answer1_for_question3), context.getResources().getString(R.string.answer2_for_question3), context.getResources().getString(R.string.answer3_for_question3), context.getResources().getString(R.string.answer4_for_question3), context.getResources().getString(R.string.right_answer_for_question3), context.getResources().getString(R.string.comment_for_question3)};
 }

I want to take these arrays from the Questions class in the activity and work with them. But when the application starts, an exception is thrown:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
        at ru.examplle.pravmin.Questions.<init>(Questions.java:15) // строка String[] arr1 = {context.getResources().getString(R.string.answer1_for_question1), context.getResources().getString(R.string.answer2_for_question1), context.getResources().getString(R.string.answer3_for_question1), context.getResources().getString(R.string.answer4_for_question1), context.getResources().getString(R.string.right_answer_for_question1), context.getResources().getString(R.string.comment_for_question1)};
        at ru.examplle.pravmin.MainActivity.onCreate(MainActivity.java:116) // строка Questions q = new Questions(this);

How to fix the error and run the application?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
AlexVWill, 2022-01-10
@AlexVWill

Try to implement the task a little differently ...
I understand that the answers to the questions are a constant? Instead of writing each answer to a question in the strings.xlm file as a separate constant, write an array of strings there:

<string-array name="answers_for_question_1">
        <item>Answer1</item>
        <item>Answer2</item>
        <item>Answer3</item>
        ...
        <item>AnswerN</item>
    </string-array>

And in the code, you can get an array of strings of answers to question No. 1 like this:
List<String> answers_for_q1 = Arrays.asList(getResources().getStringArray(R.array.answers_for_question_1));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question