-3

Can some please help me to solve this problem. when I try to print getCorrectAnswer it is printing null. how can I getCorrectAnswer to print the correctAnswer thanks.

public class Test1 {

    private String correctAnswer;

    public String getCorrectAnswer() {
        return correctAnswer;
    }

    public void setUpCorrectAnswer() {
        if (1 == 1) {
            correctAnswer = "a";
        } else {
            correctAnswer = "d";
        }
    }

    public static void main(String[] args) {
        Test1 a = new Test1();
        System.out.println(" Answer " + a.getCorrectAnswer());
    }
}
Balázs Édes
  • 13,452
  • 6
  • 54
  • 89

2 Answers2

0

You didn't call setUpCorrectAnswer().

    public static void main(String[] args) {
    Test1 a = new Test1();
    a.setUpCorrectAnswer();
    System.out.println(" Answer " + a.getCorrectAnswer());
}
PaintedRed
  • 1,398
  • 5
  • 19
  • 30
0

You need to use your setUpCorrectAnswer() method to set the value.

public static void main (String [] args) {
  Test1 a = new Test1();
  a.setUpCorrectAnswer(); // here
  System.out.println(" Answer " + a.getCorrectAnswer());
}
MikeM
  • 342
  • 2
  • 10