Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3395662
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T04:18:55+00:00 2026-05-18T04:18:55+00:00

When trying to add an object to the JS environment using Rhino 1.7 r2

  • 0

When trying to add an object to the JS environment using Rhino 1.7 r2 on Android 2.2 I always get a NullPointerException. I have boiled the class down to the bare minimum to get this exception, and I can’t figure out why. Here’s the code that always produces the exception:

public class StartActivity extends Activity
{
    /** Rhino context object */
    private Context mCX;
    /** Rhino script scope object */
    private Scriptable mScope;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        // Create our context and turn off compilation
        mCX = Context.enter();
        mCX.setOptimizationLevel( -1 );

        // Initialize the scope
        mScope = mCX.initStandardObjects();

        Object insertObj;

        // This line always throws the exception.
        insertObj = Context.javaToJS( this, mScope );

        // We never get here
        ScriptableObject.putProperty( mScope, "platform", insertObj );

        setContentView(R.layout.main);
    }
}

Changing the troublesome line to some else like insertObj = Context.javaToJS( null, mScope ); or really anything but this and the problem doesn’t occur.

Here is the stacktrace:

E/AndroidRuntime(  308): FATAL EXCEPTION: main
E/AndroidRuntime(  308): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.me/com.me.StartActivity}: java.lang.NullPointerException
E/AndroidRuntime(  308):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
E/AndroidRuntime(  308):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
E/AndroidRuntime(  308):    at android.app.ActivityThread.access$2300(ActivityThread.java:125)
E/AndroidRuntime(  308):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
E/AndroidRuntime(  308):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(  308):    at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime(  308):    at android.app.ActivityThread.main(ActivityThread.java:4627)
E/AndroidRuntime(  308):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(  308):    at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime(  308):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
E/AndroidRuntime(  308):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
E/AndroidRuntime(  308):    at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(  308): Caused by: java.lang.NullPointerException
E/AndroidRuntime(  308):    at org.mozilla.javascript.net.sf.retrotranslator.runtime.impl.MethodDescriptor.getInstance(MethodDescriptor.java:137)
E/AndroidRuntime(  308):    at org.mozilla.javascript.net.sf.retrotranslator.runtime.java.lang.reflect._Constructor.isVarArgs(_Constructor.java:83)
E/AndroidRuntime(  308):    at org.mozilla.javascript.jdk15.VMBridge_jdk15.isVarArgs(VMBridge_jdk15.java:66)
E/AndroidRuntime(  308):    at org.mozilla.javascript.MemberBox.init(MemberBox.java:86)
E/AndroidRuntime(  308):    at org.mozilla.javascript.MemberBox.<init>(MemberBox.java:72)
E/AndroidRuntime(  308):    at org.mozilla.javascript.JavaMembers.reflect(JavaMembers.java:667)
E/AndroidRuntime(  308):    at org.mozilla.javascript.JavaMembers.<init>(JavaMembers.java:76)
E/AndroidRuntime(  308):    at org.mozilla.javascript.JavaMembers.lookupClass(JavaMembers.java:838)
E/AndroidRuntime(  308):    at org.mozilla.javascript.NativeJavaObject.initMembers(NativeJavaObject.java:90)
E/AndroidRuntime(  308):    at org.mozilla.javascript.NativeJavaObject.<init>(NativeJavaObject.java:80)
E/AndroidRuntime(  308):    at org.mozilla.javascript.NativeJavaObject.<init>(NativeJavaObject.java:70)
E/AndroidRuntime(  308):    at org.mozilla.javascript.WrapFactory.wrapAsJavaObject(WrapFactory.java:149)
E/AndroidRuntime(  308):    at org.mozilla.javascript.WrapFactory.wrap(WrapFactory.java:105)
E/AndroidRuntime(  308):    at org.mozilla.javascript.Context.javaToJS(Context.java:1698)
E/AndroidRuntime(  308):    at com.me.android.StartActivity.onCreate(StartActivity.java:34)
E/AndroidRuntime(  308):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
E/AndroidRuntime(  308):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
E/AndroidRuntime(  308):    ... 11 more

Any insight as to why this is happening would be greatly appreciated.

[EDIT] After some serious googling and head smashing I discovered the ScriptableObject.defineClass method and tried using that instead, following the examples in the Rhino documentation, however now I get a RuntimeException “ReferenceError: ‘DeviceInterface’ is not defined.” I know this is possible, as Rhino is what the ASE uses for its JavaScript support.

Here is the new code which throws the RuntimeException:

public class StartActivity extends Activity
{
    /** Rhino context object */
    private Context mCX;
    /** Rhino script scope object */
    private Scriptable mScope;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        // Create our context and turn off compilation
        mCX = Context.enter();
        mCX.setOptimizationLevel( -1 );

        // Initialize the scope
        mScope = mCX.initStandardObjects();

        try{
            ScriptableObject.defineClass( mScope, DeviceInterface.class );
        } catch( Exception e ) {
            Log.e("StartActivity", e.toString());
        }

        // This is the line that throws the exception
        mCX.evaluateString(mScope,
                "var p = new DeviceInterface();", "",
                1, null);

        setContentView(R.layout.main);
    }
}

And the DeviceInterface class:

public class DeviceInterface extends ScriptableObject
{
    static private final long serialVersionUID = 1L;

    static public void init ( Scriptable scope )
    {
        Log.i( "JSInterface", "Init called" );
    }

    public DeviceInterface()
    {
        logger( "Created new JSInterface Object" );
    }

    @Override
    public String getClassName()
    {
        return "DeviceInterface";
    }

    public void jsFunction_logger( String message )
    {
        logger( message );
    }

    private void logger( String message )
    {
        if( message.contains("[alrt]") )
            Log.e( "JSInterface", message );
        else if( message.contains("[info]") )
            Log.i( "JSInterface", message );
        else
            Log.d( "JSInterface", message );
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T04:18:56+00:00Added an answer on May 18, 2026 at 4:18 am

    I have tried both of your examples:

    • Your first example executes without any exception on my project (in debug mode under Eclipse with ADK plugin) – how did you embed the Rhino library? I have been content with “Configure Build Path…/Add External JAR…”
    • Your second example throws the same exception on a standard JVM – meaning it’s not an Android problem.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to add a draggable object to to a simple html page.
I'm trying add a tab to my web page that looks like this: Using
Trying to add an onclick handler to my tabs, and can't seem to get
I am trying to add an object to an arraylist but when I view
I'm having a bit of trouble trying to add an object to the database
trying to add an ExpiresDefault ExpiresByType to content on my website so that way
I was trying to add a favicon to a website earlier and looked for
I'm trying to add support for stackoverflow feeds in my rss reader but SelectNodes
I am trying to add a project reference or swc to papervision in FlashDevelop
I am trying to add some custom build steps to my headless build process

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.