364,083 Members | 5932 Browsing Online
Community for Developers & IT Professionals
Bytes IT Community

checking if an object is null

sprightee
P: 4
Hi,
I've a simple object class as testObj.java with getter and setter methods.
In my code i need to check if the testObj object is null.

My code is :
testObj test = new testObj();
//invoke a method A which returns test as return type
if(test==null || test.equals(""))
{
System.out.println("The test object is null);
}

But i'm not getting the expected results. How to check if this test object is null?
Feb 23 '07 #1
Share this Question
Share on Google+
11 Replies


DeMan
100+
P: 1,205
try using .equals
Expand|Select|Wrap|Line Numbers
  1. if(this.equals(null))
  2. {
  3. doThings
  4. }
  5.  
Feb 23 '07 #2

DeMan
100+
P: 1,205
As an aside in this vein....

== compares something (someone will hopefully explain exactly what)

.equals is a method provided by ALL objects (which you overide by defining your own equals methof).

So then,
if you have a method called myString.

You can define equals to do anything (as long as it takes an object as input and returns a boolean)

i had mopre to say but I forgot, so post more if you want more information or this doesn't quite make sense....
Feb 23 '07 #3

r035198x
10K+
P: 10,083
Hi,
I've a simple object class as testObj.java with getter and setter methods.
In my code i need to check if the testObj object is null.

My code is :
testObj test = new testObj();
//invoke a method A which returns test as return type
if(test==null || test.equals(""))
{
System.out.println("The test object is null);
}

But i'm not getting the expected results. How to check if this test object is null?
When you get an object using the new keyword and thus calling the constructor, you will never get that object as null. i.e Constructors never return null objects.

To test if an object is null you simply use
Expand|Select|Wrap|Line Numbers
  1. if(object == null) {
Feb 23 '07 #4

r035198x
10K+
P: 10,083
try using .equals
Expand|Select|Wrap|Line Numbers
  1. if(this.equals(null))
  2. {
  3. doThings
  4. }
  5.  
This won't work. If an object is null, calling .equals or any method on it will give a NullPointerException
Feb 23 '07 #5

DeMan
100+
P: 1,205
I see what you mean ( i made the mistake of assuming that we were testing something against null, but you're right, if it's true, then the test doesn't work because it doesn't refer to the type whose equals we use <I know what I meant>, I didn't even think of that......)
Feb 23 '07 #6

sprightee
P: 4
When you get an object using the new keyword and thus calling the constructor, you will never get that object as null. i.e Constructors never return null objects.

To test if an object is null you simply use
Expand|Select|Wrap|Line Numbers
  1. if(object == null) {

sorry i'm not using the constructor.
My code is inside the method A which i'm calling is :

testObj test = null;
//Invoke this method
search(String str)

return test ;

When the search doesnot match any case, then it returns the tes obj as null.

So in may main program how can i check if the testObj is null.
I use
if(test==null || test.equlas("")

But this does not work
Feb 23 '07 #7

r035198x
10K+
P: 10,083
sorry i'm not using the constructor.
My code is inside the method A which i'm calling is :

testObj test = null;
//Invoke this method
search(String str)

return test ;

When the search doesnot match any case, then it returns the tes obj as null.

So in may main program how can i check if the testObj is null.
I use
if(test==null || test.equlas("")

But this does not work
Just use
Expand|Select|Wrap|Line Numbers
  1.  if(test == null) {
What do you mean by "this does not work"? What results are you getting and what are you expecting to get?
Feb 23 '07 #8

sprightee
P: 4
Just use
Expand|Select|Wrap|Line Numbers
  1.  if(test == null) {
What do you mean by "this does not work"? What results are you getting and what are you expecting to get?

if(test==null)
{
System.out.println("The object is null);
}
else
{
System.out.println("Not null");
}

OUTPUT:
Not null
Feb 23 '07 #9

r035198x
10K+
P: 10,083
if(test==null)
{
System.out.println("The object is null);
}
else
{
System.out.println("Not null");
}

OUTPUT:
Not null
That my friend is actually because test is Not null.
What makes you think that it's supposed to be null at that stage?
Feb 23 '07 #10

sprightee
P: 4
That my friend is actually because test is Not null.
What makes you think that it's supposed to be null at that stage?
Because this object is basically a VO object and is filled based on the search done in database and filled by the values fetched from the database and returned to the main program.
As i'm searching for a string that does not match any records in the database then it shud return a testObj which is null.
Feb 23 '07 #11

r035198x
10K+
P: 10,083
Because this object is basically a VO object and is filled based on the search done in database and filled by the values fetched from the database and returned to the main program.
As i'm searching for a string that does not match any records in the database then it shud return a testObj which is null.
Well the values are being found! You can print the value of the object if it has a good toString on it to see the value. Check your search logic again.
Feb 23 '07 #12

Post your reply

Help answer this question



Didn't find the answer to your Java question?

You can also browse similar questions: Java java isnull java isnull() null object object