Create class object only if its parameter is not null
<p>I am developing an application in which  i have one separate class to do all socket related communication.in my service class i am creating array of class object like this :</p>

<pre><code>String ip1 = "192.168.1.19";
String ip2 = "192.168.1.51";

@Override
public void onCreate() {
        roomStatus = new ReadRoomStatus[] {
       new ReadRoomStatus(ip1),
       new ReadRoomStatus(ip2) 
    };
}
</code></pre>

<p>and then i have loop to read status :</p>

<pre><code>for(i=0;i&lt;roomStatus.length;i++)
{       
    String status = roomStatus[i].readStatus();
    Log.v(TAG, "Status is ::"+status);
    broadcastRoomStatus(i, status);
}   
</code></pre>

<p>so this  loop iterate only number of object array. here it will iterate two times.              </p>

<p>i am getting this IP address from shared preferences. so if user doesn't set any of IP then class object should not be created for that ip.</p>

<p>i.e if only one IP is set then created class object should be like this</p>

<pre><code> roomStatus = new ReadRoomStatus[] {new ReadRoomStatus(ip1)};
</code></pre>

<p>if two IPs are set then </p>

<pre><code> roomStatus = new ReadRoomStatus[] {new ReadRoomStatus(ip1),new ReadRoomStatus(ip2};
</code></pre>

<p>So my initial goal is if any of ip is null then object shouldn't be initialize for that ip and loop should be iterate only no of times which IPs are available.</p>

<p>How do i achieve this? if you have any other idea then please suggest me. any help and advice will be appreciated.</p>

<p>Thanks          </p>

<p><strong>Update</strong></p>

<pre><code>ReadRoomStatus roomStatus1=new ReadRoomStatus(ip1);
        ReadRoomStatus roomStatus2=new ReadRoomStatus(ip2);

        aListStatus = new ArrayList&lt;ReadRoomStatus&gt;();
        if(ip1!=""){
            Log.v(TAG, "IP1 is not null");
            aListStatus.add(roomStatus1);
        }       
        if(ip2!=""){
            Log.v(TAG, "IP2 is not null");
            aListStatus.add(roomStatus2);
        }
</code></pre>
