F
F
FR342015-05-31 21:35:20
Java
FR34, 2015-05-31 21:35:20

Why is the variable displayed correctly when called from one class, but displayed as null when called from another?

The question related to the same IQ test (you can see it in my other questions), now ran into such a problem that by calling a variable int testAnswerthat is in the Question class from the UserInterface class, we get the normal value of the variable, but if we call the same variable from the ResultOfTest class, we get zero, what's wrong, please tell
me The text of the function from the UserInterface class

public void actionPerformed(ActionEvent e) { 
                Question quest = new Question();
                radioButtonChoose();
                res.reca();
                System.out.println("usinabc"+res.abc);
                System.out.println("usintes"+quest.tes);
                res.testAnswer(radButt);
                res.result();
                System.out.println(res.resultOfIQ);
                quest.nextQuestion();
                setMyInterface();                
            }

Function text from the ResultOfTest class
public void reca(){
        Question quest = new Question();
        System.out.println("ReOftes = "+quest.tes);
        abc = quest.tes;
        System.out.println("ReOfabc = "+abc);
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Emin, 2015-05-31
@FR34

Hello. What means

call the same variable from the ResultOfTest class?

When you declare and create two variables of the same class, say, in different parts of your application, they certainly won't be the same.
And the function reca()gives the wrong result because you create a new class object in it Question, it has nothing to do with any of your other variables. And zero because the type class field int(in your case testAnswer) is initialized to zero by default.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question