Upload Service (and Avatar Service) fail with null reference exception when uploading.

0 votes

In my latest attempt I switched over to using the Upload Service for this one, but I tried previously with the Avatar Service and was fairly certain that it was the same error. I get a null reference exception however as near as I can tell none of the values being passed to the upload function are null (see debug values below).

Using Unity 3D and C#.

Error Message:

NullReferenceException: Object reference not set to an instance of an object
StudentEditor.SaveAvatarPortrait (System.String filename, ClassMembership m, UserStudent s) (at Assets/LootAssets/Scripts/Controllers/StudentEditor.cs:345)
StudentEditor.SaveAvatarChanges () (at Assets/LootAssets/Scripts/Controllers/StudentEditor.cs:262)
UnityEngine.Events.InvokableCall.Invoke () (at <3dc54541a2574ac7826a004a212a4332>:0)
UnityEngine.Events.UnityEvent.Invoke () (at <3dc54541a2574ac7826a004a212a4332>:0)
UnityEngine.UI.Button.Press () (at C:/Program Files/Unity/Hub/Editor/2019.4.1f1/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/UI/Core/Button.cs:68)
UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/Program Files/Unity/Hub/Editor/2019.4.1f1/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/UI/Core/Button.cs:110)
UnityEngine.EventSystems.ExecuteEvents.Execute (UnityEngine.EventSystems.IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/Program Files/Unity/Hub/Editor/2019.4.1f1/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/EventSystem/ExecuteEvents.cs:50)
UnityEngine.EventSystems.ExecuteEvents.Execute[T] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.ExecuteEvents+EventFunction`1[T1] functor) (at C:/Program Files/Unity/Hub/Editor/2019.4.1f1/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/EventSystem/ExecuteEvents.cs:261)
UnityEngine.EventSystems.EventSystem:Update() (at C:/Program Files/Unity/Hub/Editor/2019.4.1f1/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/EventSystem/EventSystem.cs:377)

Here is some debug output on the values being passed:

StudentEditor.SaveAvatarPortrait(tlcc-au-nsw-000.tlcc-au-nsw-000-casejustin2023.0000-9999-TESTCLASS)
Filename: tlcc-au-nsw-000.tlcc-au-nsw-000-casejustin2023.0000-9999-TESTCLASS.png
Description: Justin's avatar. (Class: 0000-9999-TESTCLASS, School: tlcc-au-nsw-000) [tlcc-au-nsw-000-casejustin2023]
Stream Length: 74883
 
UnityEngine.Debug:Log(Object)
StudentEditor:SaveAvatarPortrait(String, ClassMembership, UserStudent) (at Assets/LootAssets/Scripts/Controllers/StudentEditor.cs:340)
StudentEditor:SaveAvatarChanges() (at Assets/LootAssets/Scripts/Controllers/StudentEditor.cs:262)
UnityEngine.EventSystems.EventSystem:Update() (at C:/Program Files/Unity/Hub/Editor/2019.4.1f1/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/EventSystem/EventSystem.cs:377)

Also from the App42 Log:

GetInstance Not Null 
UnityEngine.Debug:Log(Object)
com.shephertz.app42.paas.sdk.csharp.App42Log:Console(String)
com.shephertz.app42.paas.sdk.csharp.connection.RESTConnectorWWW:GetInstance()
com.shephertz.app42.paas.sdk.csharp.upload.UploadService:UploadFile(String, Stream, String, String, App42CallBack)
StudentEditor:SaveAvatarPortrait(String, ClassMembership, UserStudent) (at Assets/LootAssets/Scripts/Controllers/StudentEditor.cs:346)
StudentEditor:SaveAvatarChanges() (at Assets/LootAssets/Scripts/Controllers/StudentEditor.cs:262)
UnityEngine.EventSystems.EventSystem:Update() (at C:/Program Files/Unity/Hub/Editor/2019.4.1f1/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/EventSystem/EventSystem.cs:377)
 

Here is the latest version of my code:

if (thumbnailTexture != null)
        {
            // Remember currently active render texture
            RenderTexture currentActiveRT = RenderTexture.active;

            // Set the supplied RenderTexture as the active one
            RenderTexture.active = thumbnailTexture;

            // Create a new Texture2D and read the RenderTexture image into it
            Texture2D tex = new Texture2D(thumbnailTexture.width, thumbnailTexture.height);
            tex.ReadPixels(new Rect(0, 0, tex.width, tex.height), 0, 0);

            // Restorie previously active render texture
            RenderTexture.active = currentActiveRT;

            byte[] bytes = tex.EncodeToPNG();
            if (bytes.Length <= 0)
            {
                Debug.LogError("StudentEditor.SaveAvatarPortrait(" + filename + ") :: bytes array error");
                return;
            }

            System.IO.Stream stream = new System.IO.MemoryStream(bytes);
            if (stream == null)
            {
                Debug.LogError("StudentEditor.SaveAvatarPortrait(" + filename + ") :: stream == NULL");
                return;
            }

            // Upload to App42
            Debug.Log(
                "<color=yellow>StudentEditor.SaveAvatarPortrait(" + filename + ")</color>\r\n"
                + "Filename: " + filename + ".png\r\n"
                + "Student: " + s.firstName + "\r\n"
                + "Class: " + m.classID + "\r\n"
                + "Stream Length: " + stream.Length + "\r\n"
            );
            uploadService.UploadFile(filename, stream, "image", s.firstName+"'s avatar. (Class: "+m.classID+", School: "+s.schoolID+") ["+s.ssID+"]", new UploadAvatarPortraitCallBack());
        }

 

 

asked Oct 19, 2020 in Unity by ant (13 points)

Please log in or register to answer this question.

Download Widgets
Welcome to ShepHertz Product line forum, where you can ask questions and receive answers from the community. You can also reach out to us on support@shephertz.com
...