Skip to main content
MADink_Designs27
Inspiring
June 17, 2021
Answered

Nested for loop with null errors

  • June 17, 2021
  • 1 reply
  • 2826 views

I'm having an issue with a nested for loop generating a null error.

Info:

  • 8 text fields named reset#.#
    • reset fields are numbered 1-8, followed by a dot "." then 2, 4, 6, or 8; these are not widget numbers 
    • reset1.2, reset2.2
    • reset3.4, reset4.4
    • reset5.6, reset6.6
    • reset7.8, reset8.8

Here's what I'm getting in the console; it's giving me 8 object fields but 3-4 null returns between each:

for(var i = 1; i < 9; i++) {
	var ary1 = ["2", "4", "6", "8"];
	for(var j = 0; j < ary1.length; j ++) {
		var cReset = getField("reset" + i + "." + ary1[j]);
		console.println(cReset);
	}
}
[object Field]
null
null
null
[object Field]
null
null
null
null
[object Field]
null
null
null
[object Field]
null
null
null
null
[object Field]
null
null
null
[object Field]
null
null
null
null
[object Field]
null
null
null
[object Field]

true

 

Here's what the code I'm trying to write says in the console; it's saying the field is null when I try and set the mouse up setAction, but the console IS showing 8 object fields. What am I doing wrong and how do I get rid of all the null returns?:

for(var i = 1; i < 9; i++) {
	var ary1 = ["2", "4", "6", "8"];
	for(var j = 0; j < ary1.length; j++) {
		var cReset = this.getField("reset" + i + ary1[j]);
		cReset.setAction("MouseUp", "resetForm([\"card' + i + '\"]);");
	}
}
TypeError: cReset is null
5:Console:Exec
undefined

 

This topic has been closed for replies.
Correct answer try67

OK... Well, you don't need to use loops at all. This is easily written out:

 

 

this.getField("reset1.2").setAction("MouseUp", 'resetForm(["card1"]);');
this.getField("reset2.2").setAction("MouseUp", 'resetForm(["card2"]);');
this.getField("reset3.4").setAction("MouseUp", 'resetForm(["card3"]);');
this.getField("reset4.4").setAction("MouseUp", 'resetForm(["card4"]);');
this.getField("reset5.6").setAction("MouseUp", 'resetForm(["card5"]);');
this.getField("reset6.6").setAction("MouseUp", 'resetForm(["card6"]);');
this.getField("reset7.8").setAction("MouseUp", 'resetForm(["card7"]);');
this.getField("reset8.8").setAction("MouseUp", 'resetForm(["card8"]);');

 

 

 


If you really want to use a loop you can do it like this:

 

var j = 0;
for (var i = 1; i <=8; i++) {
	if (i%2==1) j+=2;
	var cReset = this.getField("reset" + i + "." + j);
	cReset.setAction("MouseUp", "resetForm([\"card" + i + "\"]);");
}

1 reply

Bernd Alheit
Community Expert
Community Expert
June 17, 2021

You will get null when the field doesn't exists.

Does you use field names with "." or without it?

MADink_Designs27
Inspiring
June 17, 2021

The field names are:

  • reset1.2, reset2.2
  • reset3.4, reset4.4
  • reset5.6, reset6.6
  • reset7.8, reset8.8

 

This code works, but it's not going to be the end code. Several other buttons/fields will be getting removed when the form is complete. I wanted something that could be automated without calling out each individual field. The variables are document level, so that's why you won't see the field names.

reset1.setAction("MouseUp", 'resetForm(["card1"]);');
reset2.setAction("MouseUp", 'resetForm(["card2"]);');
reset3.setAction("MouseUp", 'resetForm(["card3"]);');
reset4.setAction("MouseUp", 'resetForm(["card4"]);');
reset5.setAction("MouseUp", 'resetForm(["card5"]);');
reset6.setAction("MouseUp", 'resetForm(["card6"]);');
reset7.setAction("MouseUp", 'resetForm(["card7"]);');
reset8.setAction("MouseUp", 'resetForm(["card8"]);');
Bernd Alheit
Community Expert
Community Expert
June 17, 2021

In the second script you use the field names reset12, reset14, reset16, reset18, reset22, reset24, and so on.