Search code examples
javaandroidgpsexifmetadata-extractor

NullPointerException in metadata-extractor


I am using metadata-extractor library, to read exif-data from photos in phone. https://github.com/drewnoakes/metadata-extractor

I wanna get the GPS and some specific tags from metadata, for example:Latitude & Longtitude, focal length. I write code as below:

...
Metadata metadata = ImageMetadataReader.readMetadata(file);
String metaDataString = "" ;
GpsDirectory gpsDirectory = metadata.getFirstDirectoryOfType(GpsDirectory.class);    
metaDataString += "Long: " + String.valueOf(gpsDirectory.getGeoLocation().getLongitude()) + "Lat: " + String.valueOf(gpsDirectory.getGeoLocation().getLatitude());

ExifSubIFDDirectory exifSubIFDDirectory = metadata.getFirstDirectoryOfType(ExifSubIFDDirectory.class);
metaDataString += "[focal length]" + exifSubIFDDirectory.getString(ExifSubIFDDirectory.TAG_FOCAL_LENGTH);

...

I received the NullException with code above. Please help me to figure it out. Thanks in advance.

** Add a log **

java.lang.RuntimeException: An error occured while executing doInBackground()
       at android.os.AsyncTask$3.done(AsyncTask.java:299)
       at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
       at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
       at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
       at java.util.concurrent.FutureTask.run(FutureTask.java:137)
       at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
       at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
       at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
       at java.lang.Thread.run(Thread.java:856)
Caused by: java.lang.NullPointerException
       at com.testing.MetaData.LoadPhoto.doInBackground(LoadPhoto.java:113)
       at android.os.AsyncTask$2.call(AsyncTask.java:287)
       at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
       at java.util.concurrent.FutureTask.run(FutureTask.java:137)
       at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
       at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
       at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
       at java.lang.Thread.run(Thread.java:856)

The NPE error at code line 113 is

metaDataString += "Long: " + String.valueOf(gpsDirectory.getGeoLocation().getLongitude()) + "Lat: " + String.valueOf(gpsDirectory.getGeoLocation().getLatitude());

Solution

  • Cheers guys, you're all right. After debug, I saw some of my photos don't have GPS, and null. I post the code to control null value as below (this can be also applied for other specific tag)

    // Check if metadata contains the specific Directory
    if (metadata.containsDirectoryOfType(GpsDirectory.class)) {
    
          GpsDirectory gpsDirectory = metadata.getFirstDirectoryOfType(GpsDirectory.class);
    
          //Check if Directory contains the specific Tag
          if(gpsDirectory.containsTag(GpsDirectory.TAG_LATITUDE)&& gpsDirectory.containsTag(GpsDirectory.TAG_LONGITUDE)) {
    
              metaDataString = "[Longtitude]: " + String.valueOf(gpsDirectory.getGeoLocation().getLongitude()) + ", " +
                                        "[Latitude]: " + String.valueOf(gpsDirectory.getGeoLocation().getLatitude()) + ", ";
          }
          else {
            //Show error or notification
          }
    }
    

    That's it, any concern or better implement, please leave comment.