Search Unity

ArgumentNullException appear randomly in Unitywebrequest

Discussion in 'Scripting' started by MBlomme, Jul 21, 2018.

Thread Status:
Not open for further replies.
  1. MBlomme

    MBlomme

    Joined:
    Dec 18, 2017
    Posts:
    11
    Hi everyone,

    I'm posting here today because I have some weird issues using UnityWebRequests

    I'm developing an app that requires getting some files over the Internet. In order to get those files I use UnityWebRequests and a FileStream to download everything locally, but sometimes I have issues with some files that stop being downloaded (while some others are being downloaded entirely), when I look with my debugger, I discover that many fields in my UnityWebRequest have the same exception :

    Code (CSharp):
    1. System.ArgumentNullException : Value cannot be null.
    2. Parameter name: _unity_self


    Does anyone know where that could come from?

    Thanks a lot
     
    MilenaRocha likes this.
  2. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    You could try and simplify the problem. Create a new scene and add into it only the download script (give some hardcoded filenames etc. just to make it run). Can you reproduce the problem that way?

    If not, then maybe something else in the real scene is causing the problem. If it is repeatable, then try posting that cut-down code here. It may help others to see what is happening.
     
  3. GGeff

    GGeff

    Joined:
    Nov 11, 2014
    Posts:
    40
    Did you find a solution to this? I'm having it as well.
     
  4. MBlomme

    MBlomme

    Joined:
    Dec 18, 2017
    Posts:
    11
    I didn't find a solution so I went with another way to download my files (using WebClient and async methods)
     
  5. Rodolphito

    Rodolphito

    Joined:
    Jan 31, 2014
    Posts:
    17
    I've been getting this too, in 2018.2 and also in 2018.3.0b5, its a real bummer. I was hoping the new web requests would be nice and robust... does anyone know of a work-around? My old code used WWW, but since that was obsoleted I switched to UnityWebRequest, is WebClient fast and full cross platform?
     
  6. tonyhan

    tonyhan

    Joined:
    Dec 29, 2015
    Posts:
    12
    me too!
     
  7. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,732
    It looks like you are using UnityWebRequest or Download/UploadHandler after it has been disposed of. If you put UnityWebRequest inside using() block or explicitly call Dispose(), it cannot be used afterwards.
     
  8. Rodolphito

    Rodolphito

    Joined:
    Jan 31, 2014
    Posts:
    17
    Thanks for the response, I solved the problem by having less concurrent downloads. I was downloading ~100 small files simultaneously, and it seems that it did not like doing that. Maybe it has to do with available system resources prohibiting having too many files open being written to simultaneously, and not unity code?
     
  9. jirimotejlek

    jirimotejlek

    Joined:
    Nov 8, 2016
    Posts:
    20
    Having exactly the same problem, periodically doing UnityWebRequest, after a while it fails with timeout and the game has to be restarted as making any new requests causes:
    Code (CSharp):
    1. System.ArgumentNullException : Value cannot be null.
    2. Parameter name: _unity_self
    Happening on Unity 2019.1.9f
     
  10. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,732
    All symptoms of reusing UnityWebRequest after it has been disposed. It's a bug in your code.
    If you call Dispose() method on UWR or put UWR object in a using block (which calls Dispose() uppon leaving the block), you have to make sure you don't use that same UWR object. You should create a new one.
     
    Tarrag likes this.
  11. jirimotejlek

    jirimotejlek

    Joined:
    Nov 8, 2016
    Posts:
    20
    Thanks, yes, that was the cause.
     
  12. Pincape

    Pincape

    Joined:
    Dec 4, 2018
    Posts:
    5
  13. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,732
  14. andrew_pearce_

    andrew_pearce_

    Joined:
    Nov 5, 2018
    Posts:
    169
    I would like to update the topic, the bug still exist in 2018.4.1:

    Code (CSharp):
    1.     if (uwr != null) {
    2.       if (!uwr.isDone)
    3.         uwr.Abort();
    4.       uwr.Dispose();
    5.     }
    Despite that I am checking that uwr is not null and not done, I am still getting ArgumentNullException: Value cannot be null. So seems like that uwr gets disposed in another thread (coroutine) by using () {} construction.

    When I did some experiments, calling only Dispose() also aborts the download process. To be 100% sure that download will be aborted and no exception will be invoked, I used try {} except ().

    I hope this information will be helpful for someone.
     
  15. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,732
    Your code sample is incorrect!
    After calling Dispose you should set uwr to null, otherwise you are left with UnityWebRequest object that is invalid to do anything with. After calling Dispose you cannot call anything else except call Dispose more times.
     
    andrew_pearce_ likes this.
  16. andrew_pearce_

    andrew_pearce_

    Joined:
    Nov 5, 2018
    Posts:
    169
    @Aurimas-Cernius thanks for your answer. I was expecting that using() {} construction not only calls Dispose() method but also nulls variable. Thanks!
     
    Last edited: Mar 17, 2020
  17. Tarrag

    Tarrag

    Joined:
    Nov 7, 2016
    Posts:
    215
    very helpful explanation @Aurimas-Cernius , I was disposing after isnetworkerror was true and reusing UWR obj. thanks!
     
  18. malcomjarr

    malcomjarr

    Joined:
    Nov 18, 2020
    Posts:
    7
    The exception that is thrown when a C# null reference is passed to a method that does not accept it as a valid argument. An ArgumentNullException exception is thrown at run time in the following two major circumstances:

    • An uninstantiated object is passed to a method. To prevent the error, instantiate the object.
    • An object returned from a method call is then passed as an argument to a second method, but the value of the original returned object is null. To prevent the error, check for a return value that is null and call the second method only if the return value is not null.
     
Thread Status:
Not open for further replies.