From d1dabed82e0f381faced96e6e6f09b6101cd5a3f Mon Sep 17 00:00:00 2001
From: apriestman <apriestman@basistech.com>
Date: Wed, 10 Mar 2021 10:48:21 -0500
Subject: [PATCH] Refactor DrawableFile NPE fix

---
 .../imagegallery/datamodel/DrawableFile.java  | 29 ++++++-------------
 1 file changed, 9 insertions(+), 20 deletions(-)

diff --git a/ImageGallery/src/org/sleuthkit/autopsy/imagegallery/datamodel/DrawableFile.java b/ImageGallery/src/org/sleuthkit/autopsy/imagegallery/datamodel/DrawableFile.java
index 534bdfc8c8..e520e2d0ec 100644
--- a/ImageGallery/src/org/sleuthkit/autopsy/imagegallery/datamodel/DrawableFile.java
+++ b/ImageGallery/src/org/sleuthkit/autopsy/imagegallery/datamodel/DrawableFile.java
@@ -100,20 +100,9 @@ public static DrawableFile create(Long fileID, boolean analyzed) throws TskCoreE
 
     private String model;
 
-    private final CategoryManager categoryManager;
-
     protected DrawableFile(AbstractFile file, Boolean analyzed) {
         this.analyzed = new SimpleBooleanProperty(analyzed);
         this.file = file;
-  
-        ImageGalleryController controllerForCase = ImageGalleryController.getController(Case.getCurrentCase());
-        if (controllerForCase != null) {
-            categoryManager = ImageGalleryController.getController(Case.getCurrentCase()).getCategoryManager();
-        } else {
-            // If getting the controller fails it means the case is currently closing. No need to 
-            // print an error.
-            categoryManager = null;
-        }
     }
 
     public abstract boolean isVideo();
@@ -252,19 +241,19 @@ public SimpleObjectProperty<TagName> categoryProperty() {
     /**
      * Update the category property.
      */
-    private void updateCategory() {
-        // This only happens when a DrawableFile is created while the case is closing. No need
-        // to display the error message.
-        if (categoryManager == null) {
-            return;
-        }
-        
+    private void updateCategory() {    
         try {
+            ImageGalleryController controllerForCase = ImageGalleryController.getController(Case.getCurrentCaseThrows());
+            if (controllerForCase == null) {
+                // This can only happen during case closing, so return without generating an error.
+                return;
+            }
+            
             List<ContentTag> contentTags = getContentTags();
             TagName tag = null;
             for (ContentTag ct : contentTags) {
                 TagName tagName = ct.getName();
-                if (categoryManager.isCategoryTagName(tagName)) {
+                if (controllerForCase.getCategoryManager().isCategoryTagName(tagName)) {
                     tag = tagName;
                     break;
                 }
@@ -272,7 +261,7 @@ private void updateCategory() {
             categoryTagName.set(tag);
         } catch (TskCoreException ex) {
             LOGGER.log(Level.WARNING, "problem looking up category for " + this.getContentPathSafe(), ex); //NON-NLS
-        } catch (IllegalStateException ex) {
+        } catch (IllegalStateException | NoCurrentCaseException ex) {
             // We get here many times if the case is closed during ingest, so don't print out a ton of warnings.
         }
     }
-- 
GitLab