SORTABLE - FIRST TIME LOADING ISSUE

SORTABLE - FIRST TIME LOADING ISSUE

I have written following function on page which will get displayed in dialog box. When dialog box has opened first time the sortable list doesn't operate or i cant move any element but if i close dialog box and reopen it again then sortable works perfectly. 
  1.  $(function() {
  2.     if($( "#sortablelist" )!=null){
  3.        $( "#sortablelist" ).sortable({
  4.            placeholder: "ui-state-highlight",
  5.            opacity: 0.5,cursor: "move",
  6.            forcePlaceholderSize: true,
  7.            update : function () {
  8.                var newOrdering = $("#sortablelist").sortable("toArray");
  9.                 
  10.                 var newOrderIds = new Array(newOrdering.length);
  11.                 var ctr = 0;
  12.                 // Loop over each value in the array and get the ID
  13.                 $.each(
  14.                   newOrdering,
  15.                   function(intIndex, objValue) {
  16.                     //Get the ID of the reordered items 
  17.                     //- this is sent back to server to save
  18.                     newOrderIds[ctr] = objValue;
  19.                     ctr = ctr + 1;
  20.                   }
  21.                 );
  22.                
  23.            }
  24.             
  25.        }
  26.        );
  27.         
  28.        $( "#sortablelist" ).disableSelection(); 
  29.     }
  30.    }); 

Tried to debug for the same and found that at first time the sortable list doesn't get initialized properly and throws error on update on line number 8 as 'Unable to get value of the property 'sortable': object is null or undefined'. Am i doing anything wrong here?