Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

WebGL Error 'Uncaught TypeError: Cannot read property 'length' of null'

Discussion in 'WebGL' started by crushforth, May 27, 2015.

  1. crushforth

    crushforth

    Joined:
    Jul 22, 2010
    Posts:
    113
    I've build a development build, with full exceptions and no stripping and I'm trying to trackdown the cause of the following error.
    Code (csharp):
    1.  
    2. Invoking error handler due to
    3. Uncaught TypeError: Cannot read property 'length' of null
    4.  
    The call stack is:

    Code (csharp):
    1.  
    2. Uncaught TypeError: Cannot read property 'length' of null
    3. MEMFS.getFileDataAsTypedArray @ WebGL.js:2536
    4. IDBFS.loadLocalEntry @ WebGL.js:2890
    5. (anonymous function) @ WebGL.js:3010
    6. IDBFS.reconcile @ WebGL.js:3003
    7. (anonymous function) @ WebGL.js:2781
    8. IDBFS.getRemoteSet.IDBFS.getDB.index.openKeyCursor.onsuccess @ WebGL.js:2862
    9.  
    The actual line in WebGL.js looks like:

    Code (csharp):
    1.  
    2. getFileDataAsRegularArray: (function(node) {
    3.   if (node.contents && node.contents.subarray) {
    4.    var arr = [];
    5.    for (var i = 0; i < node.usedBytes; ++i) arr.push(node.contents[i]);
    6.    return arr;
    7.   }
    8.   return node.contents;
    9.  }),
    10.  getFileDataAsTypedArray: (function(node) {
    11.   if (node.contents && node.contents.subarray) return node.contents.subarray(0, node.usedBytes);
    12.  
    13.   // !!!! The line below is the line that the crash happens on.  I'm assuming node or node.contents is null
    14.   return new Uint8Array(node.contents);
    15.  }),
    16.  expandFileStorage: (function(node, newCapacity) {
    17.   if (node.contents && node.contents.subarray && newCapacity > node.contents.length) {
    18.    node.contents = MEMFS.getFileDataAsRegularArray(node);
    19.    node.usedBytes = node.contents.length;
    20.   }
    21.  
    Anyone know where this code comes from or what is triggering it? I'm assuming its some sort of in memory file system. Is this asset bundle related?

    Any help that would point me in the general direction would be greatly appreciated.

    I'll probably just start placing some Debug.Log's in there and see if I can narrow it down to a particular asset bundle or area of the code
     
    mewzi likes this.
  2. crushforth

    crushforth

    Joined:
    Jul 22, 2010
    Posts:
    113
    I've added additional debug output to the javascript to try and track this down and it seems like it has something to do with loading cached asset bundles. It also seems like it is only an issue with chrome.

    Looking at the output in Chrome I get the following

    Code (csharp):
    1.  
    2. LOG /idbfs/UnityCache/Shared/__info
    3. LOG 2 [object Object]
    4. LOG 3 [object Object]
    5. LOG 1 /idbfs/UnityCache/Shared/standalonelandingf05b0b5524f2931b46fc65e7e0205c6f/__info
    6. LOG 2 [object Object]
    7. LOG 3 [object Object]
    8. LOG 1 /idbfs/UnityCache/Shared/standalonelandingf05b0b5524f2931b46fc65e7e0205c6f/__lock
    9. LOG 2 [object Object]
    10. LOG 3 [object Object]
    11. LOG 4 null
    12. Crash here
    13.  
    But running the same app in Firefox it never attempts to access the file __lock and doesn't crash in the same way but everything still works perfectly

    Code (csharp):
    1.  
    2. LOG 1 /idbfs/UnityCache/Shared/standalonelandingf05b0b5524f2931b46fc65e7e0205c6f/__info
    3. LOG 2 [object Object]
    4. LOG 3 [object Object]
    5.  
     
  3. crushforth

    crushforth

    Joined:
    Jul 22, 2010
    Posts:
    113
    I didn't manage to track this down and find a solution but turning off exceptions for the WebGL build is an effective workaround. This probably explains why not everyone who is using asset bundles on WebGL is experiencing this. That and maybe it is cause by having certain types on prefabs or objects on the asset bundles.

    As a suggestion I'd recommend adding a 'null check' to the c++ code that the javascript is generated from (in the function getFileDataAsTypedArray).
     
  4. mewzi

    mewzi

    Joined:
    May 28, 2015
    Posts:
    2
    I have a very similar problem utilising LoadFromCacheOrDownload and PlayerPrefs.SetFloat.
    That is, my assetbundle downloads and appears to work perfectly, but when I attempt to save a player's audio volume preferences afterward, I get the exact error and stack that you do.
     
    crushforth likes this.
  5. Claytonious

    Claytonious

    Joined:
    Feb 16, 2009
    Posts:
    881
    We get this exact error, too, but only in release builds (WebGL Error 'Uncaught TypeError: Cannot read property 'length' of null').

    The development build works fine. We were building with exception handling set to "explicitly thrown only". Trying it now with exceptions turned completely off...
     
  6. yuliyF

    yuliyF

    Joined:
    Nov 15, 2012
    Posts:
    188
    How are you add a so detailed debugger on loader? I have a crashes when loading game?
     
  7. Marco-Trivellato

    Marco-Trivellato

    Unity Technologies

    Joined:
    Jul 9, 2013
    Posts:
    1,654
    would you be able to submit a bug report ?
     
  8. Claytonious

    Claytonious

    Joined:
    Feb 16, 2009
    Posts:
    881
    Tried it with exception handling at all 3 of the options and it makes no difference. We always get this error on release builds, never on development builds. :(

    If I'm able to make a repro project then I will submit a bug report.
     
  9. crushforth

    crushforth

    Joined:
    Jul 22, 2010
    Posts:
    113
    Its a while ago since I had this issue but I seem to remember it was a problem with having 'Data Caching' turned on in WebGL/PlayerSettings/Publishing Settings. As soon as I turned it off the issue went away. Also seem to remember each browser handling the 'IDBFS' storage a little bit differently. Again this was a while ago so maybe its fixed or I'm remembering it incorrectly.
     
  10. Claytonious

    Claytonious

    Joined:
    Feb 16, 2009
    Posts:
    881
    Solved the issue for our game: this was entirely our fault but the error was so opaque that it was hard to find.

    We had an obsolete UnityConfig.jsgz and fileloader.jsgz from an earlier Unity version still being linked to from the hosting page. We upload the rest to S3 behind a CloudFront cache, and we invalidate "the things that change" in Cloudfront, but we left those 2 stale files from earlier Unity versions.

    Replacing them with current versions from this build fixed the issue and we are now up and running, with no more "
    Uncaught TypeError: Cannot read property 'length' of null" error.
     
  11. chambahr

    chambahr

    Joined:
    Feb 23, 2016
    Posts:
    1
    mine was resolved by switching browsers but you just need to restart your pc to fix the error
     
  12. pmadrid

    pmadrid

    Joined:
    Apr 8, 2019
    Posts:
    2
  13. Samasab

    Samasab

    Joined:
    May 13, 2018
    Posts:
    13
    I am getting the same error in a WebGL build, in a project where I do a lot of UnityWebRequestTexture.GetTexture() requests. These requests load tile image textures for a minimap. The error seems to occur when I move through the map very fast, quickly starting/stopping a lot of these web requests.
    The idea is that these requests might can be interupted and Disposed, even before completing the download of the tile texture. This works fine most of the time, but sometimes throws that error.

    Unity version: 2019.4.9 (LTS),
    Browser: Chrome 85.0.4183.83 (64 bit)

    The error seems to trace down to this JS line in the CachedXMLHttpRequest method, so it is probably an error in a line throwing the error message :)

    Code (JavaScript):
    1. CachedXMLHttpRequest.log("'" + cache.requestURL + "' downloaded successfully (" + xhr.response.byteLength + " bytes) " + (err ? "but not stored in indexedDB cache due to error." : "and stored in indexedDB cache."));
     
    Last edited: Sep 8, 2020