[Solved] Why can’t i call a method within a protected method of the same class [duplicate]

Introduction

When writing code in an object-oriented language, it is important to understand the concept of access modifiers. Access modifiers are used to control the visibility of class members, such as methods, variables, and constructors. One of the access modifiers is the protected modifier, which allows a class to access its own members, but not members of other classes. This can be confusing when trying to call a method within a protected method of the same class, as it is not allowed. In this article, we will discuss why this is not allowed and how to work around it.

Solution

You can call a method within a protected method of the same class, but you must use the keyword “this” to do so. For example:

public class MyClass {
protected void myProtectedMethod() {
this.myOtherMethod();
}

private void myOtherMethod() {
// Do something
}
}

You are creating local variables of button1, button2 etc inside the onCreate method. This way, the generateQuestion method is unaware of these variables and uses the class variables with the same name (not included in your code, but I imagine you have somewhere declared them probably on top of your activity class) which are not initialized hence you get the NullPointerException.

Try changing your generateQuestion to:

public void generateQuestion(Button button1, Button button2, Button button3, Button button4) {

   Random rand = new Random();
   int a = rand.nextInt(21);
   int b = rand.nextInt(21);

   question.setText(Integer.toString(a) + "+" + Integer.toString(b));
   int locationOfCorrectAnswer = rand.nextInt(4);

   int incorrectAnswer;

   for (int i = 0; i < 4; i++) {

       if (i == locationOfCorrectAnswer) {
           answers.add(a + b);
       } else {
           incorrectAnswer = rand.nextInt(41);
           while (incorrectAnswer == a + b) {

               incorrectAnswer = rand.nextInt(41);

           }
           answers.add(incorrectAnswer);
       }
   }
   button1.setText(Integer.toString(answers.get(0)));
   button2.setText(Integer.toString(answers.get(1)));
   button3.setText(Integer.toString(answers.get(2)));
   button4.setText(Integer.toString(answers.get(3)));


}

and call it from onCreate like this:

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    goButton = (Button) findViewById(R.id.goButton);
    question = (TextView) findViewById(R.id.question);
    pagetimer = (TextView) findViewById(R.id.pagetimer);
    noIndiacator = (TextView) findViewById(R.id.noIndiacator);
    resulttext = (TextView) findViewById(R.id.noIndiacator);
    Button button1 = (Button) findViewById(R.id.button1);
    Button button2 = (Button) findViewById(R.id.button2);
    Button button3 = (Button) findViewById(R.id.button3);
    Button button4 = (Button) findViewById(R.id.button4);

    generateQuestion(button1, button2, button3, button4);

}

This way you will pass the local variables inside the generateQuestion method.

1

solved Why can’t i call a method within a protected method of the same class [duplicate]

Solved: Why Can’t I Call a Method Within a Protected Method of the Same Class?

It is possible to call a method within a protected method of the same class, but it is not recommended. This is because protected methods are meant to be used by subclasses, and calling a method within a protected method of the same class can lead to confusion and unexpected behavior.

When calling a method within a protected method of the same class, it is important to remember that the method being called is not necessarily visible to the outside world. This means that any changes made to the method’s parameters or return values will not be visible to the outside world. This can lead to unexpected behavior and can cause problems if the method is used in other parts of the code.

It is also important to remember that protected methods are meant to be used by subclasses. If a method is called within a protected method of the same class, it can lead to confusion as to which class is actually responsible for the method’s behavior. This can lead to unexpected behavior and can cause problems if the method is used in other parts of the code.

In general, it is best to avoid calling a method within a protected method of the same class. If it is necessary to do so, it is important to be aware of the potential issues that can arise and to take steps to ensure that the method is used correctly.