Solved

object reference null check issue, why not if? but try-catch block?

Posted on 2011-09-29
19
419 Views
Last Modified: 2012-06-21
Hi there;

In my C# application, I want to check whether the object reference is null or not by;

if(myobj == null)
do this;

but C# doesn't let me do so, want me to surround the block with try-catch in runtime for that null value, but i am checking anyways, why is like that?

Isn't there a way to use if check for this?

In C, I was doing like that. It was a problem. In Java, I don't remember.

Could you inform me regarding C# and Java perspective?

Kind regards.
0
Comment
Question by:jazzIIIlove
  • 8
  • 6
  • 2
  • +2
19 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 72 total points
ID: 36815842
did you declare the variable with:

<type> myobj;

if yes:

<type> myobj = null;

should do it ...
at least, it does for me...
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36815853
>>and Java perspective?

Is not a problem. If doThis() throws an exception, then yes, you will need try/catch. I'm guessing it's exactly the same in C#
0
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 286 total points
ID: 36815864
What is the type of myobj? If it is a value type (e.g. int, byte, char), then checking against null doesn't make sense, and is not permitted. If it is a reference type (e.g. anything defined as class), then you should certainly be able to test for null in that manner.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36815868
P.S.

I'm having a bit of difficulty in determining what the target language of the question is. My previous comment is C#-specific  = )
0
 
LVL 12

Author Comment

by:jazzIIIlove
ID: 36815924
Hi there;

angelIII:
angellll, I believe that you are talking about C#. Ok, you are right. It's working but what is the difference between assigning a null and not? I mean in practice, aren't they the same?

CEHJ:
Your comment seems contradicting angelIII's.

kaufmed:
In C#, my object is a reference type.

I ask Java to populate my thinking.

Regards.
0
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 286 total points
ID: 36815947
It's working but what is the difference between assigning a null and not?
The pre-compiler will complain about unitialized variables if you don't assign something to a variable defined inside of a block. The initial value will be null if you don't assign anything; the pre-compiler is just trying to foreward you of a potential problem with unitialized variables (i.e. prevent NullReferenceExceptions). Assigning null to a variable tricks the precompiler into thinking you've actually initialized the variable, or it assumes that you know you are, and have a reason for, assigning null to the variable.
0
 
LVL 12

Author Comment

by:jazzIIIlove
ID: 36815961
i see, ok what about the java perspective?

Regards.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36815963
...the pre-compiler is just trying to foreward...
the pre-compiler is just trying to forewarn
0
 
LVL 12

Author Comment

by:jazzIIIlove
ID: 36815969
yep, foreward can be also suitable semantically to the sentence :)
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 36816030
I see the explanation of your question to me has already been given ... it's a compiler warning, basically, to tell you :
watch out, this sounds like it could give you problems ...
0
 
LVL 12

Author Comment

by:jazzIIIlove
ID: 36816337
But it's not a warning, a runtime error, even i handle it with if check.

So, when is that precompiler working? Build time or only in run time?

Regards.

Regards.
0
 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 142 total points
ID: 36816412
for Java:

Java compiler would not allow you to use local variable in any way if it is not sure that
some value has bee assigned to it.

So if you say

String s0;

if(s0 == null){do something}

it will give an errorr  of if line : Vraible s00 might not be initialized - aned it is an error - you cannot compile it


even if you say:


int i0=5;
String s0;

if(i0==5) s0 = "five";

if(s0 == null) {do smething} this will still generate thesame error

       


To avoid that you need to initialize like that:

String s0 = null;





0
 
LVL 12

Author Comment

by:jazzIIIlove
ID: 36816552
Ok for both Java and C#;

Any differences between class member scope and function scope?

Regards.
0
 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 142 total points
ID: 36816573

In Java instance variables usually have some defaults - so when
they are declaredf they already have defaults, so compiler will not have such issues with instance (class level variables)
You usually encounter these things only with local - method levle, block level variables
0
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 286 total points
ID: 36816726
Any differences between class member scope and function scope?
In C#, you can essentially think of class-level members as globally scoped, albeit globally scoped to the class. The pre-compiler will still check that a variable has been initialized, but it look for any occurrence of initialization; it will not check to see that a variable has been initialized prior to any given call.
0
 
LVL 12

Author Comment

by:jazzIIIlove
ID: 36816727
Thanks. What about C#?

Kind regards.
0
 
LVL 12

Author Comment

by:jazzIIIlove
ID: 36817895
To clarify, what about the difference regarding scopes in C#?

Regards.
0
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 286 total points
ID: 36817932
Did you see my comment three posts above ( http:#36816726 )? Perhaps I am misunderstanding your question.
0
 
LVL 12

Author Comment

by:jazzIIIlove
ID: 36817958
oh, my mistake, sorry.

Kind regards.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
coded character set iptc field within a jpeg 1 69
Replace &lt; with < 14 56
reverse digits of a number using for loop 5 36
MySqlDump not dumping triggers 1 17
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now