29 Replies Latest reply: Apr 4, 2012 11:16 AM by sinious RSS

    Help with ERROR...

    fsoender Community Member

      I get these two errors, can anyone help me out?

       

       

      Flex1.jpg

       

      Error 1:

       

      Flex2.jpg

       

      Error 2:

      Flex3.jpg

        • 1. Re: Help with ERROR...
          kglad MVP

          i don't know if the switch command is different in flex than flash, but that's incorrect syntax for using switch in flash.  look in the api to see the correct way to use switch in flex.

          • 2. Re: Help with ERROR...
            _spoboyle Community Member

            switch statements in flex and flash are exactly the same

            • 3. Re: Help with ERROR...
              kglad MVP

              thank you, _sp.

               

              op:

               

              correct use is:

               

              switch(o.command){

              case somevalue:

              //some code

              break;

              case someothervalue:

              //some other code

              break;

              //etc

              }

              • 4. Re: Help with ERROR...
                fsoender Community Member

                Thanks,that helped me with the first error, but I cant get the second error away, any suggestions?

                 

                 

                Flex4.jpgFlex5.jpg

                • 5. Re: Help with ERROR...
                  sinious MVP

                  -

                  • 6. Re: Help with ERROR...
                    fsoender Community Member

                    Tried that, doesnt work... anything else?

                    • 7. Re: Help with ERROR...
                      sinious MVP

                      Man I hate when these forums won't let you edit. I read the post wrong.

                       

                      try needs a catch or finally. It needs to know what to "do" when it fails.

                       

                      e.g.:

                       

                      try

                      {

                           // this

                      }

                      catch (e)

                      {

                           // error occured, do something if you want

                      }

                      • 8. Re: Help with ERROR...
                        fsoender Community Member

                        Thanks Sinious, got it to work!

                         

                        One more question, how do I make the "Var NewValue" global, so I can read and use it in an other function..etc?

                         

                        Flex6.jpg

                         

                         

                         

                         

                        Flex7.jpg 

                         

                         

                         

                         

                         

                         

                         

                         

                        • 9. Re: Help with ERROR...
                          sinious MVP

                          kglad initially helped you so you should mark him as correct answer and you can just mark mine as helpful.

                           

                          You should declare them at the top of your class and they all need an access modifier. Such as private, protected, internal, public.

                           

                          e.g.

                           

                          package {

                           

                              import flash.display.Sprite;

                           

                              public class MyClass extends Sprite {

                           

                                  // define your variables here

                                   private var NewValue:String;  // create a variable only this class can use by marking it private

                           

                                  // constructor

                                  public function MyClass() {

                                          NewValue = "Some String"; // set the initial value

                                   }

                           

                                  private function SomeFunction():void

                                   {

                                         // this function is in this class so I can modify NewValue

                                          NewValue = "Some Other Value"; // work fine

                                   }

                              }

                          }

                          • 10. Re: Help with ERROR...
                            kglad MVP

                            you can't assign an int cast variable to be an Object cast object.

                             

                            you should be using something like:

                             

                            var o:Object = event.data.readObject();

                            switch(o.command){

                            case one_of_the_values_that_o.command_can_be:

                            // code for this case

                            break;

                            case another_value_that_o.command_can_be:

                            // code for this other case

                            break;

                            .

                            .

                            }

                             

                            if you don't understand how to apply that pseudo code to your situation, paste the possible values of o.command

                            • 11. Re: Help with ERROR...
                              fsoender Community Member

                              Thanks, I changet it to private var NewValue:Object; and that worked

                               

                               

                               

                               

                              • 12. Re: Help with ERROR...
                                sinious MVP

                                Glad you got it working

                                • 13. Re: Help with ERROR...
                                  fsoender Community Member

                                  Thanks guys, I learn a lot like this

                                   

                                  I will bug you guys again when I get more challenges!!

                                  • 14. Re: Help with ERROR...
                                    fsoender Community Member

                                    Hi guys,

                                     

                                    I got the code running on my phone but i got one issue. It might be that the phone doesnt listen to the port as it should, but im also wondering

                                    if the code is wrong since the code is stuck at "catch(e:Error)".

                                    The "NewValue = o.command;" seems not to work, is this a wrong way to write it?

                                    The function is listening on port 1000, and catch message with "event.data.readObject();" and get stored in "o:Object"

                                     

                                    The "NewValue" is a public Var NewValue:Object;"

                                     

                                    So any suggestions?

                                     

                                    Code:

                                     

                                    Flex9.jpg

                                     

                                     

                                     

                                    Flex10.jpg

                                    • 15. Re: Help with ERROR...
                                      kglad MVP

                                      that continues to make no sense.

                                       

                                      there's no reason you should be using a switch statement in that code.  use:

                                       

                                      protected function udpDataHandler(event.DatagramSocketDataEvent):void{

                                      var o:Object = event.data.readObject();

                                      if(o.command){

                                      NewValue=o.command;

                                      } else {

                                      NewValue=0;

                                      }

                                      }

                                       

                                      // and i have no idea what you're doing in timeHandler but myArray must be a very poor choice of names because that can't be an array.

                                      • 16. Re: Help with ERROR...
                                        sinious MVP

                                        Indeed your switch will only ever do 1 thing so there's no reason to put the switch in there. Unless you plan on eventually doing "different things" if o.command differs, ditch the switch as kglad said.

                                         

                                        You're also casting e:Error to the base Error class but there may be a fragment of a chance that whatever is happening may not conform to that. I went with the safest approach and left e untyped (*, or dynamic) so it would catch anything at all if it came in. Try adjusting it back to "catch (e)" instead of "catch (e:Error)". ANEs introduce a new layer of complexity with not everything is predictably going to be an error. For full safety you can even provide a default value, "catch (e = null)" but this is what flash does at the moment anyhow. Down the line if type checking gets stricter you'll get an argument error otherwise.

                                         

                                        In timerHandler() you probably should check the value of NewValue. If it equals zero then there's no real reason to push it onto what I assume is a stack you're creating from received UDP packets.

                                        • 17. Re: Help with ERROR...
                                          fsoender Community Member

                                          The graph still wont respond to anything coming in on the port with the code from kglad. With the original code, the graph went in the "catch" right away I started sending values to the port. Now it wont even plot the "else" statement..

                                          For me it seems that the "NewValue" is not being updated...

                                          • 18. Re: Help with ERROR...
                                            sinious MVP

                                            try {} catch() {} has the job of attempting to run code you know may fail. It's a way of gracefully handling problems without your app exiting.

                                             

                                            You need to see what is coming in the catch (e). trace(e) and see what you get. There has to be some information in there as to what is going wrong.

                                            • 19. Re: Help with ERROR...
                                              fsoender Community Member

                                              This might help better, here is most of the code:

                                               

                                              The UDP anf Graph works independently when tested as they are attended, but when I put them together, they dont "work" together.

                                               

                                               

                                               

                                              Flex12.jpg

                                              Flex11.jpg

                                              • 20. Re: Help with ERROR...
                                                sinious MVP

                                                From looking at your code most of it looks fine, I have only basic questions. Further, I am as mentioned not familiar with the UDP ANE at all.

                                                 

                                                One tiny thing is why not run initTimer() from your applicationComplete event? At least you guarantee that udpSocket is created already. So the last line of applicationCompleteHandler() can be initTimer();. Just a small thing to make sure the timer doesn't hit before a socket is made.

                                                 

                                                Second, as far as I know DatagramSocketDataEvent is part of flash.events (beta in that link at that). Is that really what the UDP ANE is sending back for an event? It could be perfectly fine, I'm just wondering why it doesn't have its own event. Or did they name their version of the event the same thing?

                                                 

                                                Also have you trace()d what happens on the if (o.command) { trace(o.command); } ? Just to see if anything ever comes in.

                                                 

                                                Also, in timerHandler(), is it really a good idea to add to myArray if NewValue == 0? Is that 0 useful to you? I presume you're making myArray bindable because whatever graph your using can use the values in it to plot something or such but is 0 useful? To me if it's ever set to 0 then nothing came in over the socket and you shouldn't really do anything, but I don't ultimately understand what you're doing with the socket anyhow.

                                                 

                                                0 may very well come over the socket so maybe you want to set it to something to flag yourself that nothing came over the socket, like -1. If you ever sense NewValue == -1 then you know to ignore that timer. 

                                                 

                                                I don't see the graph portion so I presume as mentioned that it's just reading the binded data.

                                                • 21. Re: Help with ERROR...
                                                  fsoender Community Member

                                                  For me it seems that the DatagramSocketDataEvent is named the same way, and the code work when I run it by itself.

                                                  Yes, I use 0 to have a default so I can see if something comes in on the port.

                                                  I tried to move the initTimer() in my applicationCompleteHandler() but it doesnt change anything.

                                                  • 22. Re: Help with ERROR...
                                                    sinious MVP

                                                    Elaborate more on what you mean by "they don't work together". What exactly isn't working? Not receiving any UDP packets? Graph not reading the binded array?

                                                    • 23. Re: Help with ERROR...
                                                      fsoender Community Member

                                                      I cant trace the code since it uses ANE. That is not suported. I wish there was a way to check if im receiving UDP packages.

                                                      Its either not receiving UDP or the graph is not updating the new value.

                                                       

                                                      Im stuck...

                                                      • 24. Re: Help with ERROR...
                                                        fsoender Community Member

                                                        Well I got one way to see if im receiving UDP, I will add a text area and read the incomming UDP....

                                                         

                                                        I will try now..

                                                         

                                                        Ok, I have no tried it, and there is no UDP packages coming in. That wierd, it works alone without the graph code..

                                                         

                                                         

                                                        OMG...I think I got it....Stupid me . I will post it after I tried it...

                                                        • 25. Re: Help with ERROR...
                                                          sinious MVP

                                                          Haha probably just a bind or invalid type issue.

                                                          • 26. Re: Help with ERROR...
                                                            fsoender Community Member

                                                            Got it, made a new var:string and now it works

                                                             

                                                            Anyone know anything about graphs?

                                                             

                                                            I want the graph to run from right to left, like real time charting. The graph I have now, collects everything on the chart.Hope this explain it

                                                            • 27. Re: Help with ERROR...
                                                              sinious MVP

                                                              You made NewValue an object. That means you need to assign a property to it. If you want to read a string, simply convert NewValue to a String instead of an object.

                                                               

                                                              Do one of these two things:

                                                               

                                                              1) Add a property to NewValue if you keep it an object, e.g.

                                                               

                                                              NewValue.value = o.command;

                                                               

                                                              2) Change NewValue to a String

                                                               

                                                              // at the top of the class

                                                              public var NewValue:String;

                                                               

                                                              // assignment, just how you have it now

                                                              NewValue = o.command;

                                                               

                                                              Besides that, you really need to stop adding the switch in there. That switch is absolutely pointless. There is no difference between that switch and this:

                                                               

                                                              if (true) {

                                                                   messageArea.appendText(o.command);

                                                                   NewValue = o.command;

                                                              }

                                                               

                                                              The code above is pointless. It will happen 100% of the time. So ditch the switch. Just make it:

                                                               

                                                              try

                                                              {

                                                                   var o:Object = event.data.readUTFBytes(event.data.bytesAvailable);

                                                                  

                                                                   if (o.command)

                                                                   {

                                                                        messageArea.appendText(o.command);

                                                                        NewValue = o.command;

                                                                   }

                                                                   else

                                                                   {

                                                                        NewValue = ""; // set to empty string (if you change NewValues type to String)

                                                                   }

                                                              }

                                                              catch (e)

                                                              {

                                                                   event.data.position = 0;

                                                                   messageArea.appendText(event.data.readUTFBytes(event.data.bytesAvailable));

                                                              }

                                                               

                                                              You can use a switch, but if you only provide a default it will do that every single time. That's why it's 100% useless. You need to provide cases for the switch statement for it to do different things based on what comes in.

                                                               

                                                              e.g.

                                                               

                                                              switch(o.command)

                                                              {

                                                                   case "": // contains nothing

                                                                        event.data.position = 0;

                                                                        messageArea.appendText(event.data.readUTFBytes(event.data.bytesAvailable)); // just write out whatever the bytes were (this is kinda useless too)

                                                                        break;

                                                               

                                                                   default: // it contains SOMETHING, so use it, but containing nothing was handled above

                                                                        messageArea.appendText(o.command);

                                                                        NewValue = o.command; // assuming this is type String

                                                                        break;

                                                                       

                                                              }

                                                              • 28. Re: Help with ERROR...
                                                                fsoender Community Member

                                                                He he, got it just before you posted Sinious!

                                                                 

                                                                Thanks for all the help!!!

                                                                 

                                                                Do you know the charts?

                                                                • 29. Re: Help with ERROR...
                                                                  sinious MVP

                                                                  Haha yep. Just get that default-only switch statement out of there . It's not doing anything at all.

                                                                   

                                                                  Glad you got it fixed.

                                                                   

                                                                  Sorry I don't know the chart components but that should be kept in another thread anyhow. That's more of a flex question.