Bug 507134 - NullPointerException in CellDragMode.setCellImage

Change-Id: I3127d9673d95b407139199d6771248f41c087ee0
Signed-off-by: Dirk Fauth <dirk.fauth@googlemail.com>
diff --git a/org.eclipse.nebula.widgets.nattable.core/src/org/eclipse/nebula/widgets/nattable/ui/action/CellDragMode.java b/org.eclipse.nebula.widgets.nattable.core/src/org/eclipse/nebula/widgets/nattable/ui/action/CellDragMode.java
index 7d97f97..ebe9d4e 100644
--- a/org.eclipse.nebula.widgets.nattable.core/src/org/eclipse/nebula/widgets/nattable/ui/action/CellDragMode.java
+++ b/org.eclipse.nebula.widgets.nattable.core/src/org/eclipse/nebula/widgets/nattable/ui/action/CellDragMode.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2012 Original authors and others.
+ * Copyright (c) 2012, 2016 Original authors and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -54,7 +54,10 @@
     @Override
     public void mouseUp(NatTable natTable, MouseEvent event) {
         natTable.removeOverlayPainter(this.cellImageOverlayPainter);
-        this.cellImage.dispose();
+
+        if (this.cellImage != null) {
+            this.cellImage.dispose();
+        }
 
         natTable.redraw(0, 0, natTable.getWidth(), natTable.getHeight(), false);
     }
@@ -70,30 +73,28 @@
     private void setCellImage(NatTable natTable) {
         int columnPosition = natTable.getColumnPositionByX(this.currentEvent.x);
         int rowPosition = natTable.getRowPositionByY(this.currentEvent.y);
-        ILayerCell cell = natTable.getCellByPosition(columnPosition,
-                rowPosition);
+        ILayerCell cell = natTable.getCellByPosition(columnPosition, rowPosition);
 
-        Rectangle cellBounds = cell.getBounds();
-        this.xOffset = this.currentEvent.x - cellBounds.x;
-        this.yOffset = this.currentEvent.y - cellBounds.y;
-        Image image = new Image(natTable.getDisplay(), cellBounds.width,
-                cellBounds.height);
+        if (cell != null) {
+            Rectangle cellBounds = cell.getBounds();
+            this.xOffset = this.currentEvent.x - cellBounds.x;
+            this.yOffset = this.currentEvent.y - cellBounds.y;
+            Image image = new Image(natTable.getDisplay(), cellBounds.width, cellBounds.height);
 
-        GC gc = new GC(image);
-        IConfigRegistry configRegistry = natTable.getConfigRegistry();
-        ICellPainter cellPainter = cell.getLayer().getCellPainter(
-                columnPosition, rowPosition, cell, configRegistry);
-        if (cellPainter != null) {
-            cellPainter.paintCell(cell, gc, new Rectangle(0, 0,
-                    cellBounds.width, cellBounds.height), configRegistry);
+            GC gc = new GC(image);
+            IConfigRegistry configRegistry = natTable.getConfigRegistry();
+            ICellPainter cellPainter = cell.getLayer().getCellPainter(columnPosition, rowPosition, cell, configRegistry);
+            if (cellPainter != null) {
+                cellPainter.paintCell(cell, gc, new Rectangle(0, 0, cellBounds.width, cellBounds.height), configRegistry);
+            }
+            gc.dispose();
+
+            ImageData imageData = image.getImageData();
+            image.dispose();
+            imageData.alpha = 150;
+
+            this.cellImage = new Image(natTable.getDisplay(), imageData);
         }
-        gc.dispose();
-
-        ImageData imageData = image.getImageData();
-        image.dispose();
-        imageData.alpha = 150;
-
-        this.cellImage = new Image(natTable.getDisplay(), imageData);
     }
 
     private class CellImageOverlayPainter implements IOverlayPainter {
@@ -101,7 +102,9 @@
         @Override
         public void paintOverlay(GC gc, ILayer layer) {
             if (CellDragMode.this.cellImage != null & !CellDragMode.this.cellImage.isDisposed()) {
-                gc.drawImage(CellDragMode.this.cellImage, CellDragMode.this.currentEvent.x - CellDragMode.this.xOffset,
+                gc.drawImage(
+                        CellDragMode.this.cellImage,
+                        CellDragMode.this.currentEvent.x - CellDragMode.this.xOffset,
                         CellDragMode.this.currentEvent.y - CellDragMode.this.yOffset);
             }
         }