Skip to main content
Legend
October 26, 2016
Answered

TypeError: getField(...) is null

  • October 26, 2016
  • 2 replies
  • 6612 views

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];

This topic has been closed for replies.
Correct answer Bernd Alheit

Hmm.. I've tried that, that still gives the same error. Have updated the attached sample PDF file as well. This is the latest I've tried:

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] + arrayResults1[1];

It's doing my head in, I'm sure it's related to how I pass the filename into the getField function.

If I were to write it it out 'long-hand' I'd use

var QValue = getField("Q1").value;

Which made me think it is to do with passing those quotes somehow.

I don't script too often to be honest so always rusty whenever I do need to resort to my scripting skills


Use Name, not QName. QName is an internal function.

2 replies

Legend
October 26, 2016

Still puzzling on this one, I'm thinking the issue is definitely related to the QName variable.

Tried to incorporate the quotes as part of the variable value, so that varQValue  getField("Q1").value etc.  but still getting the same error

Here's latest JavaScript I tried:

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] + arrayResults1[1];

Bernd Alheit
Community Expert
Community Expert
October 26, 2016

Use:

QName = "Q" + QNum;

Legend
October 26, 2016

Hmm.. I've tried that, that still gives the same error. Have updated the attached sample PDF file as well. This is the latest I've tried:

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] + arrayResults1[1];

It's doing my head in, I'm sure it's related to how I pass the filename into the getField function.

If I were to write it it out 'long-hand' I'd use

var QValue = getField("Q1").value;

Which made me think it is to do with passing those quotes somehow.

I don't script too often to be honest so always rusty whenever I do need to resort to my scripting skills

Legend
October 26, 2016

This is a small sample PDF I put together for testing and development purposes. It uses the second version of the script that I added to this post. https://dl.dropboxusercontent.com/u/28545642/forums-radio-value-export-to-field-small-sample.pdf