4 Replies Latest reply on Jul 22, 2011 3:15 AM by flexx0r

    how to resolve this error : Cannot access a property or method of a null object reference.

    Venkatesan Balasubramanian Level 1

      hi friends,

       

       

      I am using flex 4 application, inthat i am using string validator concept,when i enter values in Text input box then click add button means it shows error like this,

      error:Cannot access a property or method of a null object reference.

       

      code:

       

       

      public var stringVal:StringValidator = new StringValidator();
                  public function validateForm():void
                  {
                      var valResult:ValidationResultEvent;
                      stringVal.source=telNumber_txt;
                      stringVal.source=faxNumber_txt;
                      stringVal.property = "text";
                      stringVal.minLength= 10;
                      valResult = stringVal.validate();
                      if(valResult.type == "invalid")
                      {
                          Alert.show("Invalid option");
                      }

      }

       

       

      Thanks,

       

       

      B.venkatesan

        • 1. Re: how to resolve this error : Cannot access a property or method of a null object reference.
          _spoboyle Level 4

          I cannot debug your code without the whole file here is a working example

           

          <?xml version="1.0" encoding="utf-8"?>
          <s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
                                      xmlns:s="library://ns.adobe.com/flex/spark" 
                                      xmlns:mx="library://ns.adobe.com/flex/mx"
                                      minWidth="955" minHeight="600">
          
               <fx:Script>
                    <![CDATA[
                         import mx.controls.Alert;
                         import mx.validators.Validator;
          
          
                         protected function validateForm(event:MouseEvent):void
                         {
                              var result:Array = Validator.validateAll([phoneValidator, faxValidator]);
                              
                              if (result.length == 0)
                              {
                                   // no errors
                              }
                              else
                              {
                                   Alert.show("invalid option");
                              }
                         }
          
                    ]]>
               </fx:Script>
               
               <fx:Declarations>
                    <mx:StringValidator id="phoneValidator"
                                             source="{phoneInput}"
                                             property="text"
                                             minLength="10"/>
                    <mx:StringValidator id="faxValidator"
                                             source="{faxInput}"
                                             property="text"
                                             minLength="10"/>
               </fx:Declarations>
               
               <s:layout>
                    <s:VerticalLayout/>
               </s:layout>
               
               <s:TextInput id="phoneInput"/>
               <s:TextInput id="faxInput"/>          
               <s:Button label="submit"
                           click="validateForm(event)"/>
               
          </s:WindowedApplication>
          
          

           

          but a couple of things about your code:

           

          setting source twice will ignore the first assignment

          you'll need to use 2 seperate validators instead

          I dont think its a good idea to be setting the property of your validator in the validateForm function

          use ValidationResultEvent.INVALID const instead of "invalid"

          and there is a PhoneNumberValidator class you might find usefull aslo

          • 2. Re: how to resolve this error : Cannot access a property or method of a null object reference.
            flexx0r

            Everything works fine. You only forgot the "" at the Alert Command.

             

            should be like this: Alert.show("invalid option");

            • 3. Re: how to resolve this error : Cannot access a property or method of a null object reference.
              _spoboyle Level 4

              i didn't forget it its just that xml syntax highlighting removes them