10 Replies Latest reply on Oct 26, 2016 9:16 AM by Cari Jansen

    TypeError: getField(...) is null

    Cari Jansen Adobe Community Professional

      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[q] = arrayQuestions1[q] + "\n";
        } else {
        arrayResults1[q] = "";
        }
      }
      
      this.getField("Results1").value = arrayResults1[0] + arrayResults[1];
      
        • 1. Re: TypeError: getField(...) is null
          Cari Jansen Adobe Community Professional

          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-samp le.pdf

          • 2. Re: TypeError: getField(...) is null
            Cari Jansen Adobe Community Professional

            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[q] = arrayQuestions1[q] + "\n";
              } else {
              arrayResults1[q] = "";
              }
            }
            this.getField("Results1").value = arrayResults1[0] + arrayResults1[1];
            
            • 3. Re: TypeError: getField(...) is null
              Bernd Alheit Adobe Community Professional & MVP

              Use:

              QName = "Q" + QNum;

              • 4. Re: TypeError: getField(...) is null
                Cari Jansen Adobe Community Professional

                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[q] = arrayQuestions1[q] + "\n";
                  } else {
                  arrayResults1[q] = "";
                  }
                }
                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

                • 5. Re: TypeError: getField(...) is null
                  Bernd Alheit Adobe Community Professional & MVP

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

                  • 6. Re: TypeError: getField(...) is null
                    try67 MVP & Adobe Community Professional

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

                    Text from Q1

                    Text from Q2

                    • 7. Re: TypeError: getField(...) is null
                      Bernd Alheit Adobe Community Professional & MVP

                      It doesn't work with version DC.

                      • 8. Re: TypeError: getField(...) is null
                        try67 MVP & Adobe Community Professional

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

                        • 9. Re: TypeError: getField(...) is null
                          George_Johnson MVP & Adobe Community Professional

                          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.

                          1 person found this helpful
                          • 10. Re: TypeError: getField(...) is null
                            Cari Jansen Adobe Community Professional

                            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[q] = arrayQuestions1[q] + "\n";  
                              } else {  
                              arrayResults1[q] = "";  
                              }  
                            }  
                            this.getField("Results1").value = arrayResults1[0] + arrayResults1[1];