ASTERIXDB-1363: Fix NPE on bulkload failure

Change-Id: Ic1626baa63371834cc2abbe30366df506d817da6
Reviewed-on: https://asterix-gerrit.ics.uci.edu/1033
Sonar-Qube: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Tested-by: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Reviewed-by: abdullah alamoudi <bamousaa@gmail.com>
Integration-Tests: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries/load/load_non-empty_index/load_non-empty_index.1.ddl.aql b/asterixdb/asterix-app/src/test/resources/runtimets/queries/load/load_non-empty_index/load_non-empty_index.1.ddl.aql
new file mode 100644
index 0000000..963e105
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries/load/load_non-empty_index/load_non-empty_index.1.ddl.aql
@@ -0,0 +1,33 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+/*
+ * Description  : Bulkload non-empty index.
+ * Expected Res : Failure
+ * Date         : 29 July 2016
+ */
+drop dataverse OpenTinySocial if exists;
+create dataverse OpenTinySocial;
+use dataverse OpenTinySocial;
+
+create type FacebookMessageType as {
+  message-id: int64
+};
+
+create dataset FacebookMessages(FacebookMessageType)
+primary key message-id;
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries/load/load_non-empty_index/load_non-empty_index.2.update.aql b/asterixdb/asterix-app/src/test/resources/runtimets/queries/load/load_non-empty_index/load_non-empty_index.2.update.aql
new file mode 100644
index 0000000..21db981
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries/load/load_non-empty_index/load_non-empty_index.2.update.aql
@@ -0,0 +1,32 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+/*
+ * Description  : Bulkload non-empty index.
+ * Expected Res : Failure
+ * Date         : 29 July 2016
+ */
+use dataverse OpenTinySocial;
+
+load dataset FacebookMessages
+using localfs
+(("path"="asterix_nc1://data/tinysocial/fbm.adm"),("format"="adm"));
+
+load dataset FacebookMessages
+using localfs
+(("path"="asterix_nc1://data/tinysocial/fbm.adm"),("format"="adm"));
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/testsuite.xml b/asterixdb/asterix-app/src/test/resources/runtimets/testsuite.xml
index b675ece..0553fba 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/testsuite.xml
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/testsuite.xml
@@ -6380,6 +6380,12 @@
   </test-group>
   <test-group name="load">
     <test-case FilePath="load">
+      <compilation-unit name="load_non-empty_index">
+        <output-dir compare="Text">load_non-empty_index</output-dir>
+        <expected-error>Cannot load an index that is not empty</expected-error>
+      </compilation-unit>
+    </test-case>
+    <test-case FilePath="load">
       <compilation-unit name="dataset-with-meta">
         <output-dir compare="Text">dataset-with-meta</output-dir>
         <expected-error>load dataset is not supported on Datasets with Meta records</expected-error>
diff --git a/hyracks-fullstack/hyracks/hyracks-storage-am-common/src/main/java/org/apache/hyracks/storage/am/common/dataflow/IndexBulkLoadOperatorNodePushable.java b/hyracks-fullstack/hyracks/hyracks-storage-am-common/src/main/java/org/apache/hyracks/storage/am/common/dataflow/IndexBulkLoadOperatorNodePushable.java
index 36b46a7..56cad89 100644
--- a/hyracks-fullstack/hyracks/hyracks-storage-am-common/src/main/java/org/apache/hyracks/storage/am/common/dataflow/IndexBulkLoadOperatorNodePushable.java
+++ b/hyracks-fullstack/hyracks/hyracks-storage-am-common/src/main/java/org/apache/hyracks/storage/am/common/dataflow/IndexBulkLoadOperatorNodePushable.java
@@ -97,7 +97,10 @@
     @Override
     public void close() throws HyracksDataException {
         try {
-            bulkLoader.end();
+            // bulkloader can be null if an exception is thrown before it is initialized.
+            if (bulkLoader != null) {
+                bulkLoader.end();
+            }
         } catch (Throwable th) {
             throw new HyracksDataException(th);
         } finally {
@@ -123,4 +126,4 @@
             writer.fail();
         }
     }
-}
+}
\ No newline at end of file