Skip to main content
Inspiring
April 28, 2015
Question

UnitValue vs. null issue.

  • April 28, 2015
  • 2 replies
  • 471 views

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

This topic has been closed for replies.

2 replies

Vamitul
Legend
April 28, 2015

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)

Inspiring
April 28, 2015

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

Loic.Aigon
Legend
April 28, 2015

Hi,

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

FWIW,

Loic

Inspiring
April 28, 2015

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

Loic.Aigon
Legend
April 28, 2015

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