357 views
in GUI Development by
Hello, I am declaring a variable (as a project member) within a class.  The variable type is another class that I have already designed - actually, this is a kind of record holding several internal values.  Normally, I put the default value of my variable as null, and update it in code when necessary (it holds some state information).

Here's my questions:

(1) should I (generally) use null as a default value for varaibles like this, before the code runs (and changes the values)?

(2) does the value of the varable become "not null" when the variable is used to store some new values (that is, when the code is running)?

I am asking because I am testing my variable for "null", and am getting some strange results - the value of the variable seems to be equal to null, even when the record (the data type) holds some real values.  Am I doing something wrong?

Thanks!

1 Answer

0 votes
by

Hello Jonathan,

variables defined within a class are always ensured to be pre-initialized with zero values. Your variable will thus remain null as long as you have not assigned any existing object to it. It is not necessary to explicitly assign the default value null in this case. As soon as you assign an object to the variable, the variable will store a reference to this object. This value is then not null anymore.

Concerning your observation, I suppose there is something wrong with the if-condition. Can you post a code-snippet demonstrating this behavior?

Best rehards

Paul Banach

by
Hello Paul,

Thanks so much for your answer, much appreciated.  Unfortunately, I have already changed my code around (and changed the variable), however I think the key may be "assigning an existing object" to the variable.  I'll try to explain what I did:

Variable name: var1

variable type: class1

[class1 contains variables varA, varB and varC].

 

In code, I assigned the values of var1 like:

var1.varA = 0xAA;

var1.varB = 0xBB;

...and later on in code I tried a test like

if (var1 != null) {do something}.

This test always seemed to result in var1==null (and the above if test failed), even when var1.varA==[a value] and var1.varB == [a value].

Is it clear?

Jonathan
by

Hello Jonathan,

thank you for the further details. I tried to reproduce your observation, but without success. How have you initialized var1? Is it possible, that you are eventually dealing with different instances of the class containing var1? While in one instance the variable is still null, the other instance contains already an initialization ...

Also important in this context is, if var1 is null, trying to access its members (e.g. the variable varA) will produce a runtime error.

Best regards

Paul Banach

Ask Embedded Wizard

Welcome to the question and answer site for Embedded Wizard users and UI developers.

Ask your question and receive answers from the Embedded Wizard support team or from other members of the community!

Embedded Wizard Website | Privacy Policy | Imprint

...