9 Replies Latest reply on Dec 15, 2008 10:43 AM by Newsgroup_User

    NullPointerException error in canvas

    JordyvanKuijk
      Hey all,
      I have a main MXML app that contains a canvas. I have another MXML Component, called viewBestelling, wich I load into this root canvas. Simply by using: rootcanvas.addChild(viewBestelling). That works fine. In this Canvas, (viewBestelling) are some other components. There is a viewstack wich contains another canvas, wich contains 2 panels. Now in one of these panels I put some Labels.
      In my viewBestelling component I also have some Actionsript. One of my labels in one of the panels has id: klantnummer. When I then use for example:
      klantnummer.text = "blabla"; in my actionscript,
      I get a NullpointerException pointing to that line of code. I really don't know why.
      I have another label on my main canvas in viewBestelling. I can change the text of this one via actionscript that is no problem.
      By the way, I call he function initsialiseer(best_in) from my main app, maybe there is something wrong.
      I'll include the code so that maybe someone can find an answer and hopefully a solution to my problem.

      Thanks in advance,

      Jordy
        • 1. Re: NullPointerException error in canvas
          Level 7

          "JordyvanKuijk" <webforumsuser@macromedia.com> wrote in message
          news:gi4ceu$t3j$1@forums.macromedia.com...
          > Hey all,
          > I have a main MXML app that contains a canvas. I have another MXML
          > Component,
          > called viewBestelling, wich I load into this root canvas. Simply by using:
          > rootcanvas.addChild(viewBestelling). That works fine. In this Canvas,
          > (viewBestelling) are some other components. There is a viewstack wich
          > contains
          > another canvas, wich contains 2 panels. Now in one of these panels I put
          > some
          > Labels.
          > In my viewBestelling component I also have some Actionsript. One of my
          > labels
          > in one of the panels has id: klantnummer. When I then use for example:
          > klantnummer.text = "blabla"; in my actionscript,
          > I get a NullpointerException pointing to that line of code. I really don't
          > know why.
          > I have another label on my main canvas in viewBestelling. I can change the
          > text of this one via actionscript that is no problem.
          > By the way, I call he function initsialiseer(best_in) from my main app,
          > maybe
          > there is something wrong.

          How are you handling the timing in the main application to make sure the
          component is fully initialized and all of its children are built before you
          call the function?


          • 2. Re: NullPointerException error in canvas
            Gregory Lafrance Level 6
            You may have to set the ViewStack creationPolicy property to "all", though if you have a lot of content in the ViewStack it could affect performance.
            • 3. Re: NullPointerException error in canvas
              JordyvanKuijk Level 1
              Hey,
              I have some other problems regarding handling the timing of functions. For example I have a constructor that calls a httpservice and on the result initialises the local variabels, however I cannot call these variables because there is a latency in the httpservice.
              Is there any documentation available?
              Regards,

              Jordy
              • 4. Re: NullPointerException error in canvas
                JordyvanKuijk Level 1
                Hey,
                I have some other problems in my application regarding handling the timing. For example I have a constructor that calls a httpservice and on the result it initialises some local variables. However i cannot call these from another class because there is a latency in the httpservice. Is there any documentation on how to do this?
                Kind regards,
                Jordy
                • 5. Re: NullPointerException error in canvas
                  Level 7

                  "JordyvanKuijk" <webforumsuser@macromedia.com> wrote in message
                  news:gi5dfe$9e1$1@forums.macromedia.com...
                  > Hey,
                  > I have some other problems in my application regarding handling the
                  > timing.
                  > For example I have a constructor that calls a httpservice and on the
                  > result it
                  > initialises some local variables. However i cannot call these from another
                  > class because there is a latency in the httpservice. Is there any
                  > documentation
                  > on how to do this?

                  Look in the help under "addEventListener" and/or "event handling".

                  Here's a post on how to set up your help to get the most useful results:
                  http://flexdiary.blogspot.com/2008/07/getting-help-in-flex-builder.html


                  • 6. Re: NullPointerException error in canvas
                    Gregory Lafrance Level 6
                    You could always test the variables for a certain value. You really can't rely on the data being there as Flex is asynchronous.

                    But as Amy alluded to, you could have the HTTPService result handler dispatch a custom event informing the application that the data has been processed and that the variables have been populated. Some other part of your app should be listening for that event, and respond when it hears it.
                    • 7. Re: NullPointerException error in canvas
                      Level 7

                      "Greg Lafrance" <webforumsuser@macromedia.com> wrote in message
                      news:gi5rm1$q3l$1@forums.macromedia.com...
                      > You could always test the variables for a certain value. You really can't
                      > rely
                      > on the data being there as Flex is asynchronous.
                      >
                      > But as Amy alluded to, you could have the HTTPService result handler
                      > dispatch
                      > a custom event informing the application that the data has been processed
                      > and
                      > that the variables have been populated. Some other part of your app should
                      > be
                      > listening for that event, and respond when it hears it.

                      I'm suspecting he doesn't actually have an HTTPService handler. I think his
                      problem is he hasn't quite "gotten" the event model yet, so that's why I
                      sent him to learn about it.


                      • 8. Re: NullPointerException error in canvas
                        JordyvanKuijk Level 1
                        Hey all,
                        I do understand how o work with eventHandlers, I also make use of it. I'll attach the code and try to explain my problem more in detail.

                        So I have a class called Product. You can see that when A new Product Object is initialzed, the constructor calls a HTTPService wich on the result sets the local variabels Omschrijving and Bestelnummer.
                        However when I call these variables from another class (productx.getOmschrijving fro ex.), the variables are still empty because there is a delay in the Httpservice.
                        I have no clue how to fix this. Maybe my approach of the programming is wrong here.
                        I hope this can make things more clear.
                        Greeintgs,

                        Jordy
                        • 9. Re: NullPointerException error in canvas
                          Level 7

                          "JordyvanKuijk" <webforumsuser@macromedia.com> wrote in message
                          news:gi66sf$aq1$1@forums.macromedia.com...
                          > Hey all,
                          > I do understand how o work with eventHandlers, I also make use of it. I'll
                          > attach the code and try to explain my problem more in detail.
                          >
                          > So I have a class called Product. You can see that when A new Product
                          > Object
                          > is initialzed, the constructor calls a HTTPService wich on the result sets
                          > the
                          > local variabels Omschrijving and Bestelnummer.
                          > However when I call these variables from another class
                          > (productx.getOmschrijving fro ex.), the variables are still empty because
                          > there
                          > is a delay in the Httpservice.
                          > I have no clue how to fix this. Maybe my approach of the programming is
                          > wrong
                          > here.
                          > I hope this can make things more clear.
                          > Greeintgs,
                          >

                          One way to do this is to dispatch a "gotVars" event, or just use binding,
                          which handles this stuff for you.
                          >
                          > package DataModel
                          > {
                          > import mx.rpc.events.ResultEvent;
                          > import mx.rpc.http.HTTPService;
                          >
                          >
                          [Event="gotVars"]
                          > public class Product extends EventDispatcher
                          > {
                          > private var artikelnummer:String;
                          > private var aantal:int;
                          > private var bestelnummer:String;
                          > private var omschrijving:String;
                          > private var connection:HTTPService = new HTTPService();
                          > private var status:String = "Out of date";
                          >
                          >
                          > public function Product(artikelnummer_in:String, aantal_in:int)
                          > {
                          > this.connect();
                          > artikelnummer = artikelnummer_in;
                          > aantal = aantal_in;
                          super(this);
                          > }
                          >
                          > public function getArtikelnummer():String{
                          > return this.artikelnummer;
                          > }
                          >
                          > public function getAantal():int{
                          > return this.aantal;
                          > }
                          >
                          > public function klaar(event:ResultEvent):void{
                          > setStatus("updated");
                          > this.setOmschrijving(event.result.omschrijving);
                          > this.setBestelnummer(event.result.bestelnummer);

                          dispatchEvent(new Event('gotVars')
                          > }
                          >
                          > public function setOmschrijving(omschr:String):void{
                          > this.omschrijving = omschr;
                          > }
                          >
                          > public function getOmschrijving():String{
                          > return this.omschrijving;
                          > }
                          >
                          > public function setBestelnummer(best:String):void{
                          > this.bestelnummer = best;
                          > }
                          >
                          > public function getBestelnummer():String{
                          > return this.bestelnummer;
                          > }
                          >
                          > public function getStatus():String{
                          > return this.status
                          > }
                          >
                          > public function setStatus(stat:String):void{
                          > this.status = stat;
                          > }
                          >
                          > public function connect():void{
                          >
                          > var object:Object = new Object();
                          > object.artikelnummer = this.getArtikelnummer();
                          >
                          > connection.url =
                          > " http://www.studex.be/Flex/ManagerPHP/getProductbyArtikelnummer.php"
                          > connection.method = "POST";
                          > connection.resultFormat = "e4x";
                          > connection.addEventListener(ResultEvent.RESULT, klaar);
                          >
                          > connection.useProxy = false;
                          > connection.send(object);
                          >
                          > }
                          >
                          > }
                          > }

                          Note that I changed a few things to make the event dispatching work.

                          HTH;

                          Amy