Nicholas Muir Nicholas Muir - 2 months ago 12
Android Question

Android: String null pointer exception when android error string == null is always false

I am scanning the users phone and getting a list of all device music uri.

I then use those these Uris to find the music metadata for setting in a RecyclerView. I have found that some of the songs don't have the artist,album or image metadata in the song so when I return the the value to the RecyclerView I try to account for this by setting the name, artist ect to a default title.

I tried

isEmpty()
,
== ""
and
== null
:

For the
== null
Android gives the error that it will never be null.

This is my code:

public class DeviceMusicCollection {

public static ArrayList<DeviceMusicMetadata> getDeviceMusic(Context context)

{
// Get the list of songs on the device
ArrayList <DeviceMusic> deviceMusicArray = MainActivity.deviceMusicStaticArrayList;

// variables for metadata
String metadataTitle;
String metadataArtist;
String metadataAlbum;
byte [] metadataImage;

//Create a new array to retrieve the metadata info for the songs
ArrayList<DeviceMusicMetadata> deviceMusicMetadata = new ArrayList<>();

// Create a metatdata retriever to collect metadata
MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever();

if (deviceMusicArray != null){

for (int i = 0; i < deviceMusicArray.size(); i++ ){

// Create a Uri for each song from the music file address
Uri uri = Uri.fromFile(new File(deviceMusicArray.get(i).getMusicFileAddress()));

// Set the datadource for the metadata retriever
mediaMetadataRetriever.setDataSource(context, uri);

// Extract the title or default
metadataTitle = mediaMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE);

if (metadataTitle.isEmpty() || metadataTitle == null){
metadataTitle = deviceMusicArray.get(i).getMusicFileTitle();
}

// Extract the artist or default
metadataArtist = mediaMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST);

if (metadataArtist.isEmpty()){
metadataArtist = "Artist Unknown";
}

// Extract the album or default
metadataAlbum = mediaMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUM);

if (metadataAlbum.isEmpty()){
metadataAlbum = "Album Unknown";
}

// Extract the image (default handled in adapter)
metadataImage = mediaMetadataRetriever.getEmbeddedPicture();

deviceMusicMetadata.add(new DeviceMusicMetadata(metadataTitle, metadataArtist, metadataAlbum, metadataImage));
}
}
return deviceMusicMetadata;
}
}


As far as I can see I will have the same problem when setting:

String metadataTitle;
String metadataArtist;
String metadataAlbum;


I also tried initialising them in the for loop. I am sure it is something silly but not sure what.

Thanks in advance for your help

Answer

You need to check if it's not null before anything else you check.

Withing an if statement the argument are checked one after another in the order you write them So you're if should be like

                    if (metadataTitle == null || metadataTitle.isEmpty())