Derived from Canonical question for TypeError Cannot [call method / read property / set property] of null in Google Apps Script
Proposed reference for questions like :
Why the Execution order of GS files in a Project causes TypeError: Cannot read property "sayName" from undefined.?
Testing a trigger causes ReferenceError: 'e' is not defined. or TypeError: Cannot read property *...* from undefined
TypeError: Cannot read property "0" from undefined
Google Script send form values by email, error: cannot read property "namedValues"
Not enough details from the OP but three different answers that for different scenarios
TypeError: Cannot call method "getName" of undefined
TypeError: Cannot read property 'value' of undefined (line 3, file "Code"). I have a trigger for on form submit and yet it still has an errorMissing character in property name (OP used value instead of values)
Description
The error message indicates that you are trying to access a property on an Object instance, but during runtime the value actually held by a variable is a special data type undefined.
See key terms definition at the bottom.
Causes:
The error occurs when accessing properties of an object, which does not exist.
If a property doesn't exist in a object, accessing that property results in undefined and eventually a type error, if undefined is accessed like a real object.. This maybe due to typos or using case insensitive names to access a property. A variation of this error with a numeric value in place of property name indicates that an instance of Array was expected. As arrays in JavaScript are objects, everything mentioned here is true about them as well.
const obj = {a:1};
const b = obj.b;//undefined because b isn't available on obj. But doesn't throw a error
console.log(b.toString())//Throws type error
Accessing Arrays index greater than the last element's index
Array.prototype.length returns the number of elements of the Array. This number is always greater than the last element's index, as JavaScript uses 0 based indices. Accessing any index greater than or equal to the length of the array results in this type error. Eg, when accessing 3rd element in a array of length 3,
const a = [[1],[2],[3]];
const getElement3 = a[3];//undefined,because `3`'s index is ``2`` not `3`;But doesn't throw a error
console.log(getElement3[0])//Throws type error
Accessing Event objects without a event:
There is a special case of dynamically constructed objects such as event objects that are only available in specific contexts like making an HTTP request to the app or invoking a function via time or event-based trigger.
The error is a TypeError because an "object" is expected, but "undefined" is received
How to fix
Using default values
Nullish Coalescing operator ?? operator in JavaScript evaluates the right-hand side if the left-hand is null or undefined. An expression like (myVar ?? {}).myProp (or (myVar ?? [])[index] for arrays) will guarantee that no error is thrown, if the property is at least undefined.
One can also provide default values: (myVar ?? { myProp : 2 }) guarantees accessing myProp to return 2 by default. Same goes for arrays: (myVar ?? [1,2,3]).
Checking for type
Especially true for the special case, typeof operator combined with an if statement and a comparison operator will either allow a function to run outside of its designated context (i.e. for debugging purposes) or introduce branching logic depending on whether the object is present or not.
One can control how strict the check should be:
lax ("not undefined"): if(typeof myVar !== "undefined") { //do something; }
strict ("proper objects only"): if(typeof myVar === "object" && myVar) { //do stuff }
Key Terms
Object
It's one of the JavaScript data types.
undefined
It's one of the JavaScript primitive data types.
To learn about the very basics of JavaScript data types and objects see What are JavaScript Data Types? and What exactly is an object? [JavaScript].
Original revision extracted from here (Credit to #OlegValter)
Related
Derived from Canonical question for TypeError Cannot [call method / read property / set property] of null in Google Apps Script
Proposed reference for questions like :
Why the Execution order of GS files in a Project causes TypeError: Cannot read property "sayName" from undefined.?
Testing a trigger causes ReferenceError: 'e' is not defined. or TypeError: Cannot read property *...* from undefined
TypeError: Cannot read property "0" from undefined
Google Script send form values by email, error: cannot read property "namedValues"
Not enough details from the OP but three different answers that for different scenarios
TypeError: Cannot call method "getName" of undefined
TypeError: Cannot read property 'value' of undefined (line 3, file "Code"). I have a trigger for on form submit and yet it still has an errorMissing character in property name (OP used value instead of values)
Description
The error message indicates that you are trying to access a property on an Object instance, but during runtime the value actually held by a variable is a special data type undefined.
See key terms definition at the bottom.
Causes:
The error occurs when accessing properties of an object, which does not exist.
If a property doesn't exist in a object, accessing that property results in undefined and eventually a type error, if undefined is accessed like a real object.. This maybe due to typos or using case insensitive names to access a property. A variation of this error with a numeric value in place of property name indicates that an instance of Array was expected. As arrays in JavaScript are objects, everything mentioned here is true about them as well.
const obj = {a:1};
const b = obj.b;//undefined because b isn't available on obj. But doesn't throw a error
console.log(b.toString())//Throws type error
Accessing Arrays index greater than the last element's index
Array.prototype.length returns the number of elements of the Array. This number is always greater than the last element's index, as JavaScript uses 0 based indices. Accessing any index greater than or equal to the length of the array results in this type error. Eg, when accessing 3rd element in a array of length 3,
const a = [[1],[2],[3]];
const getElement3 = a[3];//undefined,because `3`'s index is ``2`` not `3`;But doesn't throw a error
console.log(getElement3[0])//Throws type error
Accessing Event objects without a event:
There is a special case of dynamically constructed objects such as event objects that are only available in specific contexts like making an HTTP request to the app or invoking a function via time or event-based trigger.
The error is a TypeError because an "object" is expected, but "undefined" is received
How to fix
Using default values
Nullish Coalescing operator ?? operator in JavaScript evaluates the right-hand side if the left-hand is null or undefined. An expression like (myVar ?? {}).myProp (or (myVar ?? [])[index] for arrays) will guarantee that no error is thrown, if the property is at least undefined.
One can also provide default values: (myVar ?? { myProp : 2 }) guarantees accessing myProp to return 2 by default. Same goes for arrays: (myVar ?? [1,2,3]).
Checking for type
Especially true for the special case, typeof operator combined with an if statement and a comparison operator will either allow a function to run outside of its designated context (i.e. for debugging purposes) or introduce branching logic depending on whether the object is present or not.
One can control how strict the check should be:
lax ("not undefined"): if(typeof myVar !== "undefined") { //do something; }
strict ("proper objects only"): if(typeof myVar === "object" && myVar) { //do stuff }
Key Terms
Object
It's one of the JavaScript data types.
undefined
It's one of the JavaScript primitive data types.
To learn about the very basics of JavaScript data types and objects see What are JavaScript Data Types? and What exactly is an object? [JavaScript].
Original revision extracted from here (Credit to #OlegValter)
Derived from Canonical question for TypeError Cannot [call method / read property / set property] of null in Google Apps Script
Proposed reference for questions like :
Why the Execution order of GS files in a Project causes TypeError: Cannot read property "sayName" from undefined.?
Testing a trigger causes ReferenceError: 'e' is not defined. or TypeError: Cannot read property *...* from undefined
TypeError: Cannot read property "0" from undefined
Google Script send form values by email, error: cannot read property "namedValues"
Not enough details from the OP but three different answers that for different scenarios
TypeError: Cannot call method "getName" of undefined
TypeError: Cannot read property 'value' of undefined (line 3, file "Code"). I have a trigger for on form submit and yet it still has an errorMissing character in property name (OP used value instead of values)
Description
The error message indicates that you are trying to access a property on an Object instance, but during runtime the value actually held by a variable is a special data type undefined.
See key terms definition at the bottom.
Causes:
The error occurs when accessing properties of an object, which does not exist.
If a property doesn't exist in a object, accessing that property results in undefined and eventually a type error, if undefined is accessed like a real object.. This maybe due to typos or using case insensitive names to access a property. A variation of this error with a numeric value in place of property name indicates that an instance of Array was expected. As arrays in JavaScript are objects, everything mentioned here is true about them as well.
const obj = {a:1};
const b = obj.b;//undefined because b isn't available on obj. But doesn't throw a error
console.log(b.toString())//Throws type error
Accessing Arrays index greater than the last element's index
Array.prototype.length returns the number of elements of the Array. This number is always greater than the last element's index, as JavaScript uses 0 based indices. Accessing any index greater than or equal to the length of the array results in this type error. Eg, when accessing 3rd element in a array of length 3,
const a = [[1],[2],[3]];
const getElement3 = a[3];//undefined,because `3`'s index is ``2`` not `3`;But doesn't throw a error
console.log(getElement3[0])//Throws type error
Accessing Event objects without a event:
There is a special case of dynamically constructed objects such as event objects that are only available in specific contexts like making an HTTP request to the app or invoking a function via time or event-based trigger.
The error is a TypeError because an "object" is expected, but "undefined" is received
How to fix
Using default values
Nullish Coalescing operator ?? operator in JavaScript evaluates the right-hand side if the left-hand is null or undefined. An expression like (myVar ?? {}).myProp (or (myVar ?? [])[index] for arrays) will guarantee that no error is thrown, if the property is at least undefined.
One can also provide default values: (myVar ?? { myProp : 2 }) guarantees accessing myProp to return 2 by default. Same goes for arrays: (myVar ?? [1,2,3]).
Checking for type
Especially true for the special case, typeof operator combined with an if statement and a comparison operator will either allow a function to run outside of its designated context (i.e. for debugging purposes) or introduce branching logic depending on whether the object is present or not.
One can control how strict the check should be:
lax ("not undefined"): if(typeof myVar !== "undefined") { //do something; }
strict ("proper objects only"): if(typeof myVar === "object" && myVar) { //do stuff }
Key Terms
Object
It's one of the JavaScript data types.
undefined
It's one of the JavaScript primitive data types.
To learn about the very basics of JavaScript data types and objects see What are JavaScript Data Types? and What exactly is an object? [JavaScript].
Original revision extracted from here (Credit to #OlegValter)
Derived from Canonical question for TypeError Cannot [call method / read property / set property] of null in Google Apps Script
Proposed reference for questions like :
Why the Execution order of GS files in a Project causes TypeError: Cannot read property "sayName" from undefined.?
Testing a trigger causes ReferenceError: 'e' is not defined. or TypeError: Cannot read property *...* from undefined
TypeError: Cannot read property "0" from undefined
Google Script send form values by email, error: cannot read property "namedValues"
Not enough details from the OP but three different answers that for different scenarios
TypeError: Cannot call method "getName" of undefined
TypeError: Cannot read property 'value' of undefined (line 3, file "Code"). I have a trigger for on form submit and yet it still has an errorMissing character in property name (OP used value instead of values)
Description
The error message indicates that you are trying to access a property on an Object instance, but during runtime the value actually held by a variable is a special data type undefined.
See key terms definition at the bottom.
Causes:
The error occurs when accessing properties of an object, which does not exist.
If a property doesn't exist in a object, accessing that property results in undefined and eventually a type error, if undefined is accessed like a real object.. This maybe due to typos or using case insensitive names to access a property. A variation of this error with a numeric value in place of property name indicates that an instance of Array was expected. As arrays in JavaScript are objects, everything mentioned here is true about them as well.
const obj = {a:1};
const b = obj.b;//undefined because b isn't available on obj. But doesn't throw a error
console.log(b.toString())//Throws type error
Accessing Arrays index greater than the last element's index
Array.prototype.length returns the number of elements of the Array. This number is always greater than the last element's index, as JavaScript uses 0 based indices. Accessing any index greater than or equal to the length of the array results in this type error. Eg, when accessing 3rd element in a array of length 3,
const a = [[1],[2],[3]];
const getElement3 = a[3];//undefined,because `3`'s index is ``2`` not `3`;But doesn't throw a error
console.log(getElement3[0])//Throws type error
Accessing Event objects without a event:
There is a special case of dynamically constructed objects such as event objects that are only available in specific contexts like making an HTTP request to the app or invoking a function via time or event-based trigger.
The error is a TypeError because an "object" is expected, but "undefined" is received
How to fix
Using default values
Nullish Coalescing operator ?? operator in JavaScript evaluates the right-hand side if the left-hand is null or undefined. An expression like (myVar ?? {}).myProp (or (myVar ?? [])[index] for arrays) will guarantee that no error is thrown, if the property is at least undefined.
One can also provide default values: (myVar ?? { myProp : 2 }) guarantees accessing myProp to return 2 by default. Same goes for arrays: (myVar ?? [1,2,3]).
Checking for type
Especially true for the special case, typeof operator combined with an if statement and a comparison operator will either allow a function to run outside of its designated context (i.e. for debugging purposes) or introduce branching logic depending on whether the object is present or not.
One can control how strict the check should be:
lax ("not undefined"): if(typeof myVar !== "undefined") { //do something; }
strict ("proper objects only"): if(typeof myVar === "object" && myVar) { //do stuff }
Key Terms
Object
It's one of the JavaScript data types.
undefined
It's one of the JavaScript primitive data types.
To learn about the very basics of JavaScript data types and objects see What are JavaScript Data Types? and What exactly is an object? [JavaScript].
Original revision extracted from here (Credit to #OlegValter)
I want to check a function definition exists in a lisp program or not to decide which program block to run.
The function definition is written on another file with.Net & I am working for AutoCAD.
Please help.
There are many ways to do this, but ultimately you need to check whether the symbol corresponding to the function name holds a value (for example using the boundp function), and perhaps additionally whether such value is of SUBR, USUBR, or EXRXSUBR data type (using the type function).
For example:
(member (type YourFunctionName) '(subr usubr exrxsubr))
In this case, if the symbol YourFunctionName is null, (type YourFunctionName) will return nil which will cause the member expression to return nil. Similarly, if the value held by the YourFunctionName symbol is anything other than a function, the member function will return nil.
Since any non-nil value in AutoLISP is interpreted as True, the use of member will validate an if test expression, even though member does not explicitly return a boolean value.
Lee's Answer is great, many time to check function is loaded or not I am use (and functionName) it return T if exist or if not returns Nil.
Suppose I am binding using
bs.Bind(y)
.For(v => v.Visibility)
.To("Failures['TaxPercent'].Length")
.WithConversion("Visibility")
.WithFallback(false);
Where Failures is a dictionary which would contain the property name (e.g. TaxPercent) if, and only if, the property fails validation.
Therefore Failure['TaxPercent'] returns the validation failure message (e.g value missing).
I want to have an expandable textview in Android which is visible only when the error is detected. I used the above code and it is not working. The fallback value does not trigger when Failure['TaxPercent'] does not exist in the dictionary.
How do I get an expandable/collapsible textview based on the validation result using a dictionary in the viewmodel??? I would really like to use a dictionary because that would save me from creating IsErrorVisible for each property.
Oddly enough, using a dictionary works for retrieving the error message though, but not for visibility! In other words, this is working great
bs.Bind(y)
.For(v => v.Text)
.To("Failures['TaxPercent']");
Also, any reason why I cannot concatenate the binding, meaning can I do this???
bs.Bind(y)
.For(v => v.Text)
.To("Failures['TaxPercent']")
.For(v => v.Visibility)
.To("Failures['TaxPercent'].Length")
.WithConversion("Visibility")
.WithFallback(false);
EDIT
The error msg in the log is
MvxBind:Error:168.86 Problem seen during binding execution for binding Visibility for Failures['TaxPercent'].Length - problem ArgumentException: The value passed in must be an enum base or an underlying type for an enum, such as an Int32.
If the dictionary doesn't contain an entry for 'TaxPercent' then the expression Failures['TaxPercent'].Length will not evaluate (an exception will be throw) so UnsetValue will be used.
In the case of UnsetValue, the ValueConverter will not be called, and the Fallback will be used. This is the same pattern as in Wpf - http://msdn.microsoft.com/en-us/library/system.windows.dependencyproperty.unsetvalue(v=vs.110).aspx
For your particular situation, it looks like you could either:
change the Fallback to the correct value for the platform instead of to a boolean value (the question didn't specify which platform(s) you are using)
create a new Visibility ValueConverter that takes Failures as its binding source and 'TaxPercent' as its parameter
remove the .Length from your binding expression - just test on the existence of the entry.
you could switch to free text binding expressions - then you could do more complicated binding statements, including nested bindings, multiple value converters, multiple fallback values, ...
For this particular case, I would just drop the .Length
For "any reason why I cannot concatenate", that won't work as the return type of Bind is a single Fluent binding entry - not a Fluent binding set.