keiwando / nativefileso

Unity - Native file dialogs and file type associations on Windows, macOS, iOS and Android
Mozilla Public License 2.0
92 stars 11 forks source link

java.lang.NullPointerException com.keiwando.lib_nativefileso.NativeFileOpenURLBuffer.getFilenameFromUri #7

Open marvpaul opened 2 years ago

marvpaul commented 2 years ago

I'm trying to find out why some of my customers on Android getting these errors, perhaps you can help?

In the Google Play Console it says: Type java.lang.RuntimeException

java.lang.RuntimeException: at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:3634) at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:3879) at android.app.servertransaction.LaunchActivityItem.execute (LaunchActivityItem.java:85) at android.app.servertransaction.TransactionExecutor.executeCallbacks (TransactionExecutor.java:135) at android.app.servertransaction.TransactionExecutor.execute (TransactionExecutor.java:95) at android.app.ActivityThread$H.handleMessage (ActivityThread.java:2203) at android.os.Handler.dispatchMessage (Handler.java:106) at android.os.Looper.loop (Looper.java:268) at android.app.ActivityThread.main (ActivityThread.java:8017) at java.lang.reflect.Method.invoke (Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:627) at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:997) Caused by: java.lang.NullPointerException: at java.util.Objects.requireNonNull (Objects.java:245) at android.content.ContentResolver.query (ContentResolver.java:1172) at android.content.ContentResolver.query (ContentResolver.java:1130) at com.keiwando.lib_nativefileso.NativeFileOpenURLBuffer.getFilenameFromUri (NativeFileOpenURLBuffer.java:119) at com.keiwando.lib_nativefileso.NativeFileOpenURLBuffer.saveFilesInCacheDir (NativeFileOpenURLBuffer.java:171) at com.keiwando.lib_nativefileso.NativeFileOpenActivity.onCreate (NativeFileOpenActivity.java:73) at android.app.Activity.performCreate (Activity.java:8077) at android.app.Activity.performCreate (Activity.java:8061) at android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1315) at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:3607)

My code is:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
#if UNITY_ANDROID

using Keiwando.NFSO;
#endif

public class PresetHandler : MonoBehaviour
{
/*#if UNITY_ANDROID
void Start() {
    DontDestroyOnLoad(this.gameObject);
        NativeFileSOMobile.shared.FilesWereOpened += delegate (OpenedFile[] files) {

            // Process the opened files
            Debug.Log(files[0].Name);
        GameObject.Find("Canvas").GetComponent<UIStateMachineFluid>().loadPresetString(files[0].ToUTF8String()); 
        };      
    }
#else*/
void Start()
    {
        ImaginationOverflow.UniversalFileAssociation.FileAssociationManager.Instance.FileActivated += FileActivatedHandler;
    }
    private void
    FileActivatedHandler(ImaginationOverflow.UniversalFileAssociation.Data.FileInformation s)
    {
        GameObject.Find("Canvas").GetComponent<UIStateMachineFluid>().loadPresetStream(s.Stream); 
    }

    void OnDestroy()
    {
        ImaginationOverflow.UniversalFileAssociation.FileAssociationManager.Instance.FileActivated -= FileActivatedHandler;
    }
//#endif

}

And for import / export files I use:

if (path == null)
            {

SupportedFileType[] supportedFileTypes = {
    SupportedFileType.Any
};

NativeFileSO.shared.OpenFile(supportedFileTypes,
  delegate (bool fileWasOpened, OpenedFile file) {
    if (fileWasOpened) {
        // Process the loaded contents of "file"
        success = loadPresetString(file.ToUTF8String());
    } else {
         GameObject.Find("Notification").GetComponent<UINotificationManager>().Notify("", "Preset importing failed", "Failed to import Preset.");
    }
});
            }
            else
            {
                success = loadPresetPath(path); 
            }

And:

string filePath = Path.Combine(Application.temporaryCachePath, name + ".fluids");
            StreamWriter writer = new StreamWriter(filePath, false);
            writer.Write(encrypted);
            writer.Close();
FileToSave file = new FileToSave(filePath,  name + ".fluids", SupportedFileType.Any);

// Allows the user to choose a save location and saves the 
// file to that location
NativeFileSO.shared.SaveFile(file);
keiwando commented 2 years ago

From a quick glance at the code it seems like you are ending up with uri being null here for some reason: https://github.com/keiwando/nativefileso/blob/47d2c3f8ccfcd8ca04f46c7d6c7cfdc77b996c56/Plugin-Projects/Android/NativeFileSO/lib_nativefileso/src/main/java/com/keiwando/lib_nativefileso/NativeFileOpenURLBuffer.java#L125

Maybe there need to be explicit null checks before adding elements to the uris ArrayList here: https://github.com/keiwando/nativefileso/blob/47d2c3f8ccfcd8ca04f46c7d6c7cfdc77b996c56/Plugin-Projects/Android/NativeFileSO/lib_nativefileso/src/main/java/com/keiwando/lib_nativefileso/NativeFileOpenActivity.java#L107 and here https://github.com/keiwando/nativefileso/blob/47d2c3f8ccfcd8ca04f46c7d6c7cfdc77b996c56/Plugin-Projects/Android/NativeFileSO/lib_nativefileso/src/main/java/com/keiwando/lib_nativefileso/NativeFileOpenActivity.java#L110

I won't have time to be able to properly look into this any time soon.

marvpaul commented 2 years ago

Thanks a lot for your thoughts about it. I'll try to do this. Anyways, a fix from your side would be really appreciated as it produces a lot of crashes for my customers.