1

I have included HTML page inside codename one application, I need to modify the content (I am trying to modify the html page using JavascriptContext) and when I try to do that, it always gives me null pointer on the object. I doubled checked the id for the component that I am trying to modify.

So can you provide any code snippet or example on the correct way to modify the html page from inside the code?

WebBrowser b = new WebBrowser() { 
    @Override 
    public void onLoad(String url) { 
        // TODO Auto-generated method stub super.onLoad(url); 
        jsObject = (JSObject)ctx.get("testing"); 
        System.out.println(jsObject.get(0)); 
    } 
}; 
browser = (BrowserComponent)b.getInternal(); 
ctx = new JavascriptContext(browser); 
url="jar:///"+formName+".html"; 
b.setURL(url);

The html body has

<p id = "testing" >hello there what's up</p> 

as per Shai request adding the stack trace :

document.getElementById('testing').innerHTML = 'set from eclipse';
Exception in thread "JavaFX Application Thread" netscape.javascript.JSException: TypeError: null is not an object (evaluating 'document.getElementById('testing').innerHTML = 'set from eclipse'')
    at com.sun.webkit.dom.JSObject.fwkMakeException(Unknown Source)
    at com.sun.webkit.WebPage.twkExecuteScript(Native Method)
    at com.sun.webkit.WebPage.executeScript(Unknown Source)
    at javafx.scene.web.WebEngine.executeScript(Unknown Source)
    at com.codename1.impl.javase.SEBrowserComponent$9.run(SEBrowserComponent.java:215)
    at com.sun.javafx.application.PlatformImpl.lambda$null$174(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$175(Unknown Source)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$149(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
null
AhMaD AbUIeSa
  • 805
  • 1
  • 12
  • 21
  • Could you please provide a code snippet showing what you have done yet? – Sen Jacob Dec 14 '16 at 07:09
  • `WebBrowser b = new WebBrowser(){ @Override public void onLoad(String url) { // TODO Auto-generated method stub super.onLoad(url); jsObject = (JSObject)ctx.get("testing"); System.out.println(jsObject.get(0)); } }; browser = (BrowserComponent)b.getInternal(); ctx = new JavascriptContext(browser); url="jar:///"+formName+".html"; b.setURL(url); } ` the html have this `

    hello there what's up

    ` in the body
    – AhMaD AbUIeSa Dec 14 '16 at 08:42
  • I don't think you can modify an html page inside a jar by loading the page in a WebBrowser and taking it's JavaScript context. – Sen Jacob Dec 14 '16 at 09:19
  • Related: [Modifying a file inside a jar](http://stackoverflow.com/questions/1224817/); [Can a Jar File be updated programmatically without rewriting the whole file?](http://stackoverflow.com/questions/32353423/); [Updating config file inside a jar programatically](http://stackoverflow.com/questions/28054155/) – Sen Jacob Dec 14 '16 at 09:25
  • I am with you that is for a jar file [ but in my case the file is html ] but to include that html inside the application I put jar to make the app understand the html file that reside in the src folder – AhMaD AbUIeSa Dec 14 '16 at 09:34
  • What's the stack of the exception? I'm guessing here but get is probably not the same as find. The id is in the local but not the global context where get is looking. – Shai Almog Dec 15 '16 at 04:58
  • @ShaiAlmog I added the stacktrace if you would like to check it out I printed out the javascript that I want to execute before the exception – AhMaD AbUIeSa Dec 15 '16 at 07:07
  • I was asking about the previous stack trace but in this case the error message is quite clear, you are trying to execute code instead of get a context. To do what you are doing here you need to declare and call a function. There is a section describing that in the javascript section of the javadocs: https://www.codenameone.com/javadoc/com/codename1/javascript/package-summary.html – Shai Almog Dec 16 '16 at 05:02

0 Answers0