diff --git a/sql/ha_partition.cc b/sql/ha_partition.cc
index ccda01de6b7..36ad06aa7f7 100644
--- a/sql/ha_partition.cc
+++ b/sql/ha_partition.cc
@@ -5472,7 +5472,7 @@ int ha_partition::index_end()
   file= m_file;
   do
   {
-    if ((*file)->inited == INDEX)
+    if (*file && (*file)->inited == INDEX)
     {
       int tmp;
       if ((tmp= (*file)->ha_index_end()))
@@ -8054,10 +8054,12 @@ int ha_partition::compare_number_of_records(ha_partition *me,
                                             const uint32 *b)
 {
   handler **file= me->m_file;
-  /* Note: sorting in descending order! */
-  if (file[*a]->stats.records > file[*b]->stats.records)
+  /* Note: sorting in descending order! */  ///psergey-todo: might  be NULLs!
+  ha_rows a_records= file[*a] ? file[*a]->stats.records : 0;
+  ha_rows b_records= file[*b] ? file[*b]->stats.records : 0;
+  if (a_records > b_records)
     return -1;
-  if (file[*a]->stats.records < file[*b]->stats.records)
+  if (a_records < b_records)
     return 1;
   return 0;
 }
@@ -9882,7 +9883,10 @@ handler::Table_flags ha_partition::table_flags() const
     if (first_used_partition == MY_BIT_NONE)
       first_used_partition= 0;
   }
-  DBUG_RETURN((m_file[first_used_partition]->ha_table_flags() &
+  handler *file= m_file[first_used_partition];
+  if (!file)
+    file= m_file_sample;
+  DBUG_RETURN((file->ha_table_flags() &
                  ~(PARTITION_DISABLED_TABLE_FLAGS)) |
                  (PARTITION_ENABLED_TABLE_FLAGS));
 }
diff --git a/sql/ha_partition.h b/sql/ha_partition.h
index 1085eca72cc..b7108da9074 100644
--- a/sql/ha_partition.h
+++ b/sql/ha_partition.h
@@ -1222,7 +1222,7 @@ class ha_partition :public handler
       The following code is not safe if you are using different
       storage engines or different index types per partition.
     */
-    return m_file[0]->index_flags(inx, part, all_parts);
+    return (m_file[0]? m_file[0]: m_file_sample)->index_flags(inx, part, all_parts);
   }
 
   /**
