LuaState.lua_setfield - IllegalArgumentException

I use codes below to dispatch event.


CoronaEnvironment.getCoronaActivity().runOnUiThread(new Runnable()

{

    @Override

    public void run()

    {

        try

        {

            CoronaLua.newEvent(L, “nativeEvent”);

            if (parameters != null) {

                CoronaLua.pushHashtable(L, parameters);

                L.setField(-2, “data”);

            }

            CoronaLua.dispatchRuntimeEvent(L, 0);

        }

        catch (Exception ex)

        {

            ex.printStackTrace();

        }

    }

});


Android throws an error below.


java.lang.IllegalArgumentException: illegal index

                                                                         at com.naef.jnlua.LuaState.lua_setfield(Native Method)

                                                                         at com.naef.jnlua.LuaState.<init>(Unknown Source)

                                                                         at com.ansca.corona.CoronaEnvironment.invokeLuaErrorHandler(CoronaEnvironment.java:422)

                                                                         at com.ansca.corona.NativeToJavaBridge.callInvokeLuaErrorHandler(NativeToJavaBridge.java:446)

                                                                         at com.ansca.corona.JavaToNativeShim.nativeRender(Native Method)

                                                                         at com.ansca.corona.JavaToNativeShim.render(JavaToNativeShim.java:182)

                                                                         at com.ansca.corona.Controller.updateRuntimeState(Controller.java:347)

                                                                         at com.ansca.corona.graphics.opengl.CoronaGLSurfaceView$CoronaRenderer.onDrawFrame(CoronaGLSurfaceView.java:425)

                                                                         at com.ansca.corona.graphics.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1623)

                                                                         at com.ansca.corona.graphics.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1378)


Probably something weird is going on with parameters. You’re getting “illegal index” exception, which means that -2 is illegal index. I would recommend to print out L.getTop() to see if  newEvent or pushHashtable actually are actually working. Also, you don’t have to check for null, pushHashtable/pushValue would do it. Try

            CoronaLua.newEvent(L, “nativeEvent”);

            CoronaLua.pushValue(L, parameters);

            L.setField(-2, “data”);

            CoronaLua.dispatchRuntimeEvent(L, 0);

 

 

EDIT:

To debug you can try inserting Log(w, "LUA TOP (1): " + Integer.toString(L.getTop())) after each line

It should go 0 1 2 1 0

Thank you very much!
So can I change my code like below?

L.setField(-L.getTop(), “data”);

Probably not a good idea. This wouldn’t do what you want. You should figure out what is going wrong in your code.

It throws still errors many times.

How can I use safe dispatchRuntimeEvent?

My app crashed. )-:

It is very hard to solve your problem with knowing specific issues. Try something like this:

        CoronaLua.newEvent(L, “nativeEvent”);

        try {

            CoronaLua.pushValue(L, parameters);

            L.setField(-2, “data”);             

        } catch (Exception e) {

            Log.e(“CORONA”, “There was error pushing value to stack”);

        }

        CoronaLua.dispatchRuntimeEvent(L, 0);

I do not know what is going on in your code. I would suggest just dispatching specific fields, instead of making pushValue for hash table. It may make your code easier. Also, you can check out more precisely what is failing by tracing stack top as I mentioned before by inserting simple instructions printing stack top after each line.

OK. Thank you.

I consider Corona Cards.

But when I used it, it had many problems.

Touch issue after changing orientation, screen capture issue, issue for using plugins, …

I believe that is becoming better. And I trust you, Coronalabs team. (-:

Unfortunately Corona Cards is rather buggy and doesn’t support plugins officially. I suggest sticking to Native, but feel free to try Cards.

About particular issue. My suggestion is instead of dispatching HashMap try to set individual fields. Make several different events instead of single “data” one. I’m pretty sure that what causing error is that your HashMap contain some datastructure/custom class which can not be transparently pushed to Lua.

Thank you very much!

And would you give me an answer for my question below?

https://forums.coronalabs.com/topic/71440-ipad-orientation-issue/

Probably something weird is going on with parameters. You’re getting “illegal index” exception, which means that -2 is illegal index. I would recommend to print out L.getTop() to see if  newEvent or pushHashtable actually are actually working. Also, you don’t have to check for null, pushHashtable/pushValue would do it. Try

            CoronaLua.newEvent(L, “nativeEvent”);

            CoronaLua.pushValue(L, parameters);

            L.setField(-2, “data”);

            CoronaLua.dispatchRuntimeEvent(L, 0);

 

 

EDIT:

To debug you can try inserting Log(w, "LUA TOP (1): " + Integer.toString(L.getTop())) after each line

It should go 0 1 2 1 0

Thank you very much!
So can I change my code like below?

L.setField(-L.getTop(), “data”);

Probably not a good idea. This wouldn’t do what you want. You should figure out what is going wrong in your code.

It throws still errors many times.

How can I use safe dispatchRuntimeEvent?

My app crashed. )-:

It is very hard to solve your problem with knowing specific issues. Try something like this:

        CoronaLua.newEvent(L, “nativeEvent”);

        try {

            CoronaLua.pushValue(L, parameters);

            L.setField(-2, “data”);             

        } catch (Exception e) {

            Log.e(“CORONA”, “There was error pushing value to stack”);

        }

        CoronaLua.dispatchRuntimeEvent(L, 0);

I do not know what is going on in your code. I would suggest just dispatching specific fields, instead of making pushValue for hash table. It may make your code easier. Also, you can check out more precisely what is failing by tracing stack top as I mentioned before by inserting simple instructions printing stack top after each line.

OK. Thank you.

I consider Corona Cards.

But when I used it, it had many problems.

Touch issue after changing orientation, screen capture issue, issue for using plugins, …

I believe that is becoming better. And I trust you, Coronalabs team. (-:

Unfortunately Corona Cards is rather buggy and doesn’t support plugins officially. I suggest sticking to Native, but feel free to try Cards.

About particular issue. My suggestion is instead of dispatching HashMap try to set individual fields. Make several different events instead of single “data” one. I’m pretty sure that what causing error is that your HashMap contain some datastructure/custom class which can not be transparently pushed to Lua.

Thank you very much!

And would you give me an answer for my question below?

https://forums.coronalabs.com/topic/71440-ipad-orientation-issue/