• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

TypeError: getField(...) is null

Guide ,
Oct 25, 2016 Oct 25, 2016

Copy link to clipboard

Copied

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

TOPICS
Acrobat SDK and JavaScript

Views

4.9K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Oct 26, 2016 Oct 26, 2016

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

Votes

Translate

Translate
Guide ,
Oct 25, 2016 Oct 25, 2016

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Oct 25, 2016 Oct 25, 2016

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 25, 2016 Oct 25, 2016

Copy link to clipboard

Copied

Use:

QName = "Q" + QNum;

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Oct 25, 2016 Oct 25, 2016

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 26, 2016 Oct 26, 2016

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Oct 26, 2016 Oct 26, 2016

Copy link to clipboard

Copied

LATEST

Wow you are a legend @Bernd!!!!  I would have never figured that one out by myself. Thank you so, so much.

Interesting also to see that this was specific to Acrobat DC, I indeed failed to mention which version of Acrobat I was using.

Thanks so much to all who responded.

Working Script looks like this:

var arrayQuestions1 = ["Text from Q1", "Text from Q2"]  //read 

var arrayResults1 = new Array();  // write 

for (q=0; q < 2; q++) { 

var QuestionNum = q + 1;        // is a number  

var QuestionName = "Q" + QuestionNum;  // is a string that matches field name (Q1, Q2 etc.) 

var QuestionValue = getField(QuestionName).value;  // get field Q1, Q2 value 

  if (QuestionValue < 3) { 

  arrayResults1 = arrayQuestions1 + "\n"; 

  } else { 

  arrayResults1 = ""; 

  } 

this.getField("Results1").value = arrayResults1[0] + arrayResults1[1];

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 26, 2016 Oct 26, 2016

Copy link to clipboard

Copied

It's working fine for me... After pressing the button the text field is populated with:

Text from Q1

Text from Q2

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 26, 2016 Oct 26, 2016

Copy link to clipboard

Copied

It doesn't work with version DC.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 26, 2016 Oct 26, 2016

Copy link to clipboard

Copied

It does in XI. So the problem is most likely a bug in DC...

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 26, 2016 Oct 26, 2016

Copy link to clipboard

Copied

If you do as Bernd suggested and change the name of the QName variable to something else (e.g., "QName1"), it will work. What's happening is Acrobat is not allowing you to overwrite the existing function named QName, which is a good thing and not a bug.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines