Search code examples
luarpc

LUA Error: attempt to index a nil value @6:16


I am trying to retrieve the Entryhandle UUID by sending a request. However, I am getting this error every time. Can anyone help me solve it or point out where I am making a mistake?

local config={}

config.mcast_mac = "00:0a:cd:16:da:f1"


function rpc:epm()                                                                    


 local pkt = CreateFromPath("ethernet/ip/udp/dcerpc/epm")

 --[[data is put here]]


 SendAndWait(pkt, function(res)
local epm = res.get_layer("epm")
--[[data is put here--]]
handle = epm.EntryHandleUUID.to_string()

print("EntryHandleUUID:",handle)

 end



 end,2000)

  return handle

  end 

Solution

  • This code not valid Lua code. Suppose remove end from line after print. To findout where you try get access to nil value you can just add assert before each index operation.

    local config={}
    
    config.mcast_mac = "00:0a:cd:16:da:f1"
    
    assert(rpc, 'rpc is NULL')
    
    function rpc:epm()
      local pkt = CreateFromPath("ethernet/ip/udp/dcerpc/epm")
      --[[data is put here]]
      SendAndWait(pkt, function(res)
        assert(res, 'res is NULL')
        local epm = res.get_layer("epm")
        --[[data is put here--]]
        assert(epm, 'epm is NULL')
        local uuid = epm.EntryHandleUUID
        assert(uuid, 'epm.EntryHandleUUID is NULL')
        handle = uuid.to_string()
        print("EntryHandleUUID:",handle)
      end, 2000)
      return handle
    end