Topic
9 replies Latest Post - ‏2010-11-27T14:33:08Z by faizan4it
aryan_raj
aryan_raj
9 Posts
ACCEPTED ANSWER

Pinned topic Null check - best way

‏2010-08-02T09:42:03Z |
Hi All,

I have a object which has List objects, strings etc., in it.
What is the best way to check all attributes in that object for null.
Even the objects within must all be checked.
Updated on 2010-11-27T14:33:08Z at 2010-11-27T14:33:08Z by faizan4it
  • Caleb_Payne
    Caleb_Payne
    9 Posts
    ACCEPTED ANSWER

    Re: Null check - best way

    ‏2010-08-05T23:02:54Z  in response to aryan_raj
    > aryan_raj wrote:
    > Hi All,
    >
    > I have a object which has List objects, strings etc., in it.
    > What is the best way to check all attributes in that object for null.
    > Even the objects within must all be checked.

    Just to make sure that I understand what you're asking:
    You want to check EVERY member of EVERY instance of EVERY object within your list?

    I have to ask: why on earth would you want to do that?

    My questions aside, the easiest way would be to use reflection. Iterate over each element in your list, then use reflection to retrieve each field in each object. Iterate over each of them, rinse, wash, repeat.
    • aryan_raj
      aryan_raj
      9 Posts
      ACCEPTED ANSWER

      Re: Null check - best way

      ‏2010-08-06T20:15:22Z  in response to Caleb_Payne
      Hi,

      Thank you for the reply, I get a DTO from ejb call. This DTO has some members in it, a few lists and few string members. I need to check each and every member of this DTO for null, I need to iterate the lists also to check for null.
      How will reflection be helpful in this case. I don't want to have some 50-100 if statements in my code to do this.
      • Caleb_Payne
        Caleb_Payne
        9 Posts
        ACCEPTED ANSWER

        Re: Null check - best way

        ‏2010-08-07T02:19:55Z  in response to aryan_raj
        > aryan_raj wrote:
        > Hi,
        >
        > Thank you for the reply, I get a DTO from ejb call. This DTO has some members in it, a few lists and few string members. I need to check each and every member of this DTO for null, I need to iterate the lists also to check for null.
        > How will reflection be helpful in this case. I don't want to have some 50-100 if statements in my code to do this.

        You wouldn't need 50-100 if statements to check for null with reflection. Just use a loop and iterate over each java.lang.reflect.Field:

        
        
        
        import java.util.List; 
        
        import java.lang.reflect.*;   
        
        class MyObject 
        { 
        
        private String name; 
        
        private String emailAddress; 
        
        private List<Integer> records;   
        
        public MyObject() 
        { 
        }   
        
        public 
        
        void setName(String name) 
        { this.name = name; 
        }   
        
        public 
        
        void setEmailAddress(String emailAddress) 
        { this.emailAddress = emailAddress; 
        }   
        
        public String getName() 
        { 
        
        return this.name; 
        }   
        
        public String getEmailAddress() 
        { 
        
        return this.emailAddress; 
        }   
        
        public List<Integer> getRecords() 
        { 
        
        return this.records; 
        // will always be null. 
        } 
        }   
        
        class ReflectionTest 
        { 
        
        public 
        
        static 
        
        void main(String[] argv) 
        
        throws Exception 
        { MyObject obj = 
        
        new MyObject(); obj.setName(
        "Caleb"); obj.setEmailAddress(
        "test@address.net");   
        
        for(Field field : obj.getClass().getDeclaredFields()) 
        { field.setAccessible(
        
        true); 
        
        if(field.get(obj) == 
        
        null) System.out.printf(
        "Field \"%s\" is null.\n", field.getName()); 
        } 
        } 
        }
        
        • aryan_raj
          aryan_raj
          9 Posts
          ACCEPTED ANSWER

          Re: Null check - best way

          ‏2010-08-07T18:52:10Z  in response to Caleb_Payne
          Thank you for the reply, that is helpful. My List attribute in the object has contains some objects in it, I goggled for checking the objects in the list using reflection, found that we need to use some Type and ParameterizedType for that. Will it be OK if I use these or is there any other method to check that also more easily.
          • Caleb_Payne
            Caleb_Payne
            9 Posts
            ACCEPTED ANSWER

            Re: Null check - best way

            ‏2010-08-07T19:08:53Z  in response to aryan_raj
            > aryan_raj wrote:
            > Thank you for the reply, that is helpful. My List attribute in the object has contains some objects in it, I goggled for checking the objects in the list using reflection, found that we need to use some Type and ParameterizedType for that. Will it be OK if I use these or is there any other method to check that also more easily.

            You could use reflection to find null elements in a list, but I'd simply use reflection to find the list, and then just loop over the elements in the list ...
            Here's a slightly modified example of the code I gave you above:
            
            
            
            import java.util.List; 
            
            import java.util.ArrayList; 
            
            import java.lang.reflect.*;   
            
            class MyObject 
            { 
            
            private String name; 
            
            private String emailAddress; 
            
            private List records;   
            
            public MyObject() 
            { records = 
            
            new ArrayList(); records.add(
            "This is a test."); records.add(
            
            new java.awt.Frame()); records.add(
            
            null); records.add(
            "Last item."); 
            }   
            
            public 
            
            void setName(String name) 
            { this.name = name; 
            }   
            
            public 
            
            void setEmailAddress(String emailAddress) 
            { this.emailAddress = emailAddress; 
            }   
            
            public String getName() 
            { 
            
            return this.name; 
            }   
            
            public String getEmailAddress() 
            { 
            
            return this.emailAddress; 
            }   
            
            public List getRecords() 
            {   
            
            return records; 
            } 
            }   
            
            class ReflectionTest 
            { 
            
            public 
            
            static 
            
            void main(String[] argv) 
            
            throws Exception 
            { MyObject obj = 
            
            new MyObject(); 
            //obj.setName("Caleb"); // make name null this time obj.setEmailAddress(
            "test@address.net");   
            
            for(Field field : obj.getClass().getDeclaredFields()) 
            { field.setAccessible(
            
            true);   Object temp = field.get(obj); 
            
            if(temp == 
            
            null) 
            { System.out.printf(
            "Field \"%s\" is null.\n", field.getName()); 
            
            continue; 
            }   
            
            if(temp 
            
            instanceof List) 
            { List l = (List)temp; 
            
            for(
            
            int i = 0; i < l.size(); i++) 
            { 
            // search for null items in the list 
            
            if(l.get(i) == 
            
            null) System.out.printf(
            "Item at index [%d] is null.\n", i); 
            } 
            } 
            } 
            } 
            }
            
            • aryan_raj
              aryan_raj
              9 Posts
              ACCEPTED ANSWER

              Re: Null check - best way

              ‏2010-08-09T13:49:30Z  in response to Caleb_Payne
              Thank you, really helpful.
              • Caleb_Payne
                Caleb_Payne
                9 Posts
                ACCEPTED ANSWER

                Re: Null check - best way

                ‏2010-08-22T15:27:47Z  in response to aryan_raj
                > aryan_raj wrote:
                > Thank you, really helpful.

                You're very welcome. I'm glad I could help!
                • RaviKrishna.P
                  RaviKrishna.P
                  1 Post
                  ACCEPTED ANSWER

                  Re: Null check - best way

                  ‏2010-09-28T07:38:40Z  in response to Caleb_Payne
                  Hi,

                  The above code works. But i have used List<integer> insted of List. But the execution is entering the if conduction at "if(temp instanceof List<?>){" .

                  import java.util.ArrayList;
                  import java.util.List;
                  import java.lang.reflect.*;

                  class Student{
                  private int SiNo;
                  private String Name;
                  private String EmailId;
                  private List<Integer> Marks;

                  Student(){
                  Marks = new ArrayList<Integer>();
                  Marks.add(0);
                  Marks.add(2);
                  Marks.add(5, 12);
                  Marks.add(null);
                  }

                  public int getSiNo() {
                  return SiNo;
                  }

                  public void setSiNo(int siNo) {
                  SiNo = siNo;
                  }

                  public String getName() {
                  return Name;
                  }

                  public void setName(String name) {
                  Name = name;
                  }

                  public String getEmailId() {
                  return EmailId;
                  }

                  public void setEmailId(String emailId) {
                  EmailId = emailId;
                  }

                  public List<Integer> getMarks() {
                  return Marks;
                  }

                  public void setMarks(List<Integer> marks) {
                  Marks = marks;
                  }
                  }

                  public class ReflectionTest1 {

                  public static void main(String[] args) throws Exception {
                  MyObject obj1=new MyObject();
                  obj1.setEmailAddress("A@A.Com");
                  obj1.setName("A");

                  for(Field field : obj1.getClass().getDeclaredFields()){
                  field.setAccessible(true);

                  Object temp = field.get(obj1);
                  if(temp == null){
                  System.out.println("The Field "field.getName()"Is NULL");
                  continue;
                  }

                  if(temp instanceof List<?>){
                  @SuppressWarnings("unchecked")
                  List<Integer> l=(List<Integer>) temp;

                  for(int i = 0; i < l.size(); i++){
                  if(l.get(i)==null)
                  System.out.println("Item of "field.getName()" Is null at "i" Location\n");
                  }
                  }
                  }
                  }

                  }

                  Can you please help me out..

                  thanks,
  • faizan4it
    faizan4it
    1 Post
    ACCEPTED ANSWER

    Re: Null check - best way

    ‏2010-11-27T14:33:08Z  in response to aryan_raj
    Hi
    WE Hope U Fine.

    thanks for your kind information i am really thankful to you for this.

    [url=http://www.doggyminders.com]Dog Minders[/url]
    [url=http://www.doggyminders.com]Dog Sitters[/url]