checking if an object is null

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sprightee
    New Member
    • Feb 2007
    • 4

    checking if an object is null

    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.prin tln("The test object is null);
    }

    But i'm not getting the expected results. How to check if this test object is null?
  • DeMan
    Top Contributor
    • Nov 2006
    • 1799

    #2
    try using .equals
    Code:
    if(this.equals(null))
    {
    doThings
    }

    Comment

    • DeMan
      Top Contributor
      • Nov 2006
      • 1799

      #3
      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....

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by sprightee
        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.prin tln("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
        Code:
        if(object == null) {

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Originally posted by DeMan
          try using .equals
          Code:
          if(this.equals(null))
          {
          doThings
          }
          This won't work. If an object is null, calling .equals or any method on it will give a NullPointerExce ption

          Comment

          • DeMan
            Top Contributor
            • Nov 2006
            • 1799

            #6
            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......)

            Comment

            • sprightee
              New Member
              • Feb 2007
              • 4

              #7
              Originally posted by r035198x
              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
              Code:
              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

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                Originally posted by sprightee
                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
                Code:
                 if(test == null) {
                What do you mean by "this does not work"? What results are you getting and what are you expecting to get?

                Comment

                • sprightee
                  New Member
                  • Feb 2007
                  • 4

                  #9
                  Originally posted by r035198x
                  Just use
                  Code:
                   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.prin tln("The object is null);
                  }
                  else
                  {
                  System.out.prin tln("Not null");
                  }

                  OUTPUT:
                  Not null

                  Comment

                  • r035198x
                    MVP
                    • Sep 2006
                    • 13225

                    #10
                    Originally posted by sprightee
                    if(test==null)
                    {
                    System.out.prin tln("The object is null);
                    }
                    else
                    {
                    System.out.prin tln("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?

                    Comment

                    • sprightee
                      New Member
                      • Feb 2007
                      • 4

                      #11
                      Originally posted by r035198x
                      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.

                      Comment

                      • r035198x
                        MVP
                        • Sep 2006
                        • 13225

                        #12
                        Originally posted by sprightee
                        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.

                        Comment

                        • vivax83
                          New Member
                          • Oct 2012
                          • 1

                          #13
                          Code:
                          exercice = request.getParameter("exercice");
                          
                                try {
                                    if (exercice.equals(null)){
                                      out.println("IT IS REALLY NULL ...BUT THIS WILL NEVER BE PRINTED BECAUSE OF THE NullPointerException<br/>");
                                    }
                                } catch (NullPointerException e) {
                                    out.println("I'M REALLY SURE exercice IS NULL ... <br/>");
                                } catch (Exception e) {
                                    out.println("NOT REALLY SURE exercice IS NULL ... <br/>");
                                }
                          Last edited by Rabbit; Oct 31 '12, 04:20 PM. Reason: Please use code tags when posting code.

                          Comment

                          Working...