2 Replies Latest reply: Sep 6, 2011 5:18 PM by baftos RSS

    Invoke applet method from javascript problem

    886586
      Applet code:
      class someapplet extends JApplet
      {
           ByteArrayOutputStream docstream;
      
           public void init()
           {
             
           }
       
           public void start()
           {
               docstream = ... //fill stream with some data
           }
            
            public void stop()
            {
             
            }
      
            public String foo()
            {
               try
               {
                     return docstream.toString()     
                }
               catch (Exception ex)
               {
                     return "bad!";
                }
            }
      }
      I get null pointer exception, when I invoke method foo() from javascript in HTML page, but docstream is not null. I don't recive an exception if I call foo() from start() method!
        • 1. Re: Invoke applet method from javascript problem
          DrClap
          If you're getting a NullPointerException in the foo() method then the only possibility is that docstream is null. Are you stating that it isn't null based on some evidence, or on the assumption that the start() method completed successfully?
          • 2. Re: Invoke applet method from javascript problem
            baftos
            There can be quite a few reasons. Please post the full stack trace of the exception. Please also place strategic System.out.println()'s in your methods so we can see what is called when.
            The timing of when the page starts loading, is fully loaded, the applet is init()-ed and start()-ed is not very predictable. As DrClap says, what is the evidence that docstream is non-null?
            From what kind of javascript event do you call the applet? If your call comes before start(), like from the onLoad event, of course docstream is null. Or maybe you do not call correctly into java and the exception is thrown by some javascript-to-java plumbing (the Java plug-in itself).