如何解决lua错误:“尝试索引?(零值)”

时间:2015-07-28 03:01:59

标签: java indexing lua null luaj

有很多关于这种错误的帖子,大多数人都说它与表和数组索引问题有关。但我根本不使用表,我只是试图调用我创建的库函数,我得到了这个错误。这是从java中调用的lua脚本:

String script = new String (  
             "function event_touch (  )"  
            + "   pb.open_form ('view_monster');"
            + "   print ('I ran the lua script');"
            + "end");

PBLUAengine.run_script(script, "event_touch");

这会在捕获异常时给出以下错误:

“function event_touch()pb.open_form('view_monster'); print('我运行lua脚本');结束:1尝试索引?(一个零值)”

run_script()函数调用这样的脚本(我正在使用luaj):

public static LuaValue run_script ( String script )
    {
        try
        {
            LuaValue chunk = s_globals.load( script );
            return chunk.call();
        }
        catch ( Exception e)
        {
           Gdx.app.error ("PBLUAengine.run_script()", e.getMessage() );
        }   

        return null;
    }

库方法是这样的,当从java调用时,同一段代码也可以工作:

static class open_form extends OneArgFunction
{
        public LuaValue call (LuaValue formname)
        {           
            String tmpstr = (String ) CoerceLuaToJava.coerce(formname, java.lang.String.class );

            try
            {               
                PBscreen_game.hide_subscreen(PBscreen_game.MENU_SUBS); 
                PBscreen_game.show_subscreen ( PBscreen_game.FORM_SUBS);
                PBsubscreen_form.open_form ( new PBform_regular ( tmpstr ) );   
            }
            catch (Exception e)
            {
                Gdx.app.error("PBLUAlibrary.open_form", e.getMessage());
            }

            return valueOf ( 0 );
        }
    }

它基本上将lua参数转换为string,创建一个新的from并传入参数字符串。

库函数的声明如下:

public LuaValue call( LuaValue modname, LuaValue env )
    {
        LuaValue library = tableOf();
        library.set( "open_form", new open_form() );
        library.set( "open_system_form", new open_system_form() );
        env.set( "pb", library );
        return library;
    }

这可能是我在整个系统中看到的唯一“表”。这通常用于将正确的类与正确的函数名称链接。

有人有想法吗?

2 个答案:

答案 0 :(得分:2)

  

大多数人都说它与表和数组索引问题有关

它与表和数组索引相关。如果您尝试索引对象且该对象为require(tidyr) countsdf <- stack(counts) countsdf <- separate(data = countsdf,col = ind,into = c('id','set'),sep = '\\.',) spread(data = countsdf,set,values) ,则会收到该错误:

  

我根本不使用表格[..]这是lua脚本:

nil

pb.open_form 正在编入索引。它可能是pb

答案 1 :(得分:0)

我似乎通过添加require行来包含库来解决问题。所以新脚本是:

String script = new String (
            "require 'com.lariennalibrary.pixelboard.library.PBLUAlibrary'"
            +  "function event_touch (  )"  
            + "   pb.open_form ('view_monster');"
            + "   print ('I ran the next button lua script');"
            + "end");

它要求包含我的库类,它将添加所有&#34; pb。*&#34;功能。我可能会错误地删除该行,或者设法使它在没有它的情况下以某种方式工作。由于所有脚本都需要此库,因此我可以在尝试运行的每个脚本之前默认添加它。

再次感谢