6 Replies Latest reply on Apr 28, 2015 7:48 AM by Jim Birkenseer

    UnitValue vs. null issue.

    Jim Birkenseer Level 1

      Hi Folks,

       

      Has anyone come across an issue where a valid UnitValue object is considered null? I've tested this in Mac versions of: ID CS6, ID CC 2014, and when running the script within ExtendScript Toolkit v4.0.0.1. I haven't tried this on Windows.

       

      var x = .1;
      var uv = new UnitValue(x, "pt");
      $.writeln(uv);
      if (uv === null)
      {
        // uv is incorrectly considered null when (uv.value > -1.0 && uv.value < 1.0)
        $.writeln("uv is null!");
      }
      
      // Output: 
      //   0.1 pt
      //   uv is null!
      

       

      It appears that a UnitValue object is considered to be null by ExtendScript when its value property (the x variable in the code above) is a fraction of 1. i.e.,(uv.value > -1.0 && uv.value < 1.0).

       

      And, yes, this issue can be worked around using a try/catch:

       

      function IsUVNull(theUV)
      {
       // Returns true if null; false otherwise.
        try
        {
          var dummy = theUV.constructor;
          return false;
        }
        catch (e)
        {
          return true;
        }
      }
      

       

      Am I missing something, or is ExtendScript just picking on me. Again. ;-)

       

      On the plus side, maybe I finally found a situation where null actually is an object!

       

      -- Jim

        • 1. Re: UnitValue vs. null issue.
          Loic.Aigon Adobe Community Professional

          Hi,

           

          Perfectly on my side. Mac os 10.7.5 same config as yours for apps (not tested in CS6 though).

           

          FWIW,

           

          Loic

          • 2. Re: UnitValue vs. null issue.
            Vamitul Level 4

            you don't need the try catch, you can simply use:

            if (uv.value === null) 
            { 
              // uv is incorrectly considered null when (uv.value > -1.0 && uv.value < 1.0) 
              $.writeln("uv is null!"); 
            } 
            

            but i agree it is a strange bug (on windows also)

            • 3. Re: UnitValue vs. null issue.
              Jim Birkenseer Level 1

              Hi Loic,

               

              Are you saying that you got the same unexpected output that I did when you ran the first code snippet--in other words, (uv === null) incorrectly matches null when really uv is a variable that references a valid UnitValue object?

               

              Thanks!

               

              -- Jim

              • 4. Re: UnitValue vs. null issue.
                Jim Birkenseer Level 1

                Hi Vamitul,

                 

                On my system, the workaround function with the try/catch, IsUVNull() is required in order to tell if a variable is set to null, or if a variable is set to something like (new UnitValue(.5, "in")).

                 

                So, when I run the code snippet below:

                Line 1 correctly results in false.

                Line 2 incorrectly results in true. (Note the UnitValue value is between -1.0 and 1.0, which seems to causes the incorrect results for me.)

                Line 4 uses the workaround function, and correctly results in false.

                 

                $.writeln((new UnitValue(1, "pt") === null)); // CORRECT: This results in false, as expected.
                $.writeln((new UnitValue(.5, "pt") === null)); // WRONG: This results in true, but it should be false!
                
                $.writeln(IsUVNull((new UnitValue(.5, "pt")))); // CORRECT: This results in false, as expected.
                function IsUVNull(theUV)  
                { 
                // Returns true if null; false otherwise. 
                  try 
                  { 
                    var dummy = theUV.constructor; 
                    return false; 
                  } 
                  catch (e) 
                  { 
                    return true; 
                  } 
                } 
                
                

                 

                Thanks!

                 

                -- Jim

                • 5. Re: UnitValue vs. null issue.
                  Loic.Aigon Adobe Community Professional

                  Hello,

                  Yeah I got the same result but to be honest I first answered that it was ok on my side because I misunderstood your issue. Now I can confirm uv === null //true;

                  Loic

                  • 6. Re: UnitValue vs. null issue.
                    Jim Birkenseer Level 1

                    Hi Loic and Vamitul,

                     

                    Thanks for taking the time to confirm this!

                     

                    -- Jim