TypeError: getField(...) is null
I know this is a commonly seen error where a JavaScript can't find a particular field (I've searched the forums 😉 )
Been staring at my code trying to figure out why it is generated, and hoping one of you will spot the issue immediately.
Scenario:
- Questionnaire.
- Each is question is radio button group named Q1, Q2 etc, consisting of four buttons.
- Each question has four values assigned to the With four values assigned 1, 2, 3, and 4
- User answers questions, and at the end clicks on a Results button which generates text (arrayQuestions1) into a Text Field (like a report).
What's happening:
- As part of the script I'm trying to generate the field name (Q1, Q2) 'automatically' so I can set this up for clusters of questions.
- When I test with the below script, I get the "TypeError: getField(...) is null" error.
Here's what I've got that generates the above error (doing a simple test with just two questions at the moment only, as easier for me to get the script sorted).
var arrayQuestions1 = ["Text from Q1", "Text from Q2"] //read
var arrayResults1 = new Array(); // write
for (q=0; q < 2; q++) {
var QNum = q + 1; // is a number
var QName = "Q" + QNum; // is a string that matches field name (Q1, Q2 etc.)
var QValue = getField("QName").value; // get field Q1, Q2 value
if (QValue < 3) {
arrayResults1
= arrayQuestions1+ "\n";} else {
arrayResults1
= "";}
}
this.getField("Results1").value = arrayResults1[0] + arrayResults[1];