Access MidiOutputDevice in a table

I want to be able to send midi cc to multiple Launchpads if available. Therefore I would like to store the MidiOutput devices of each Launchpad in a table.

-- traversing every midi input  
for i=1, #inputs do  
 if string.find(inputs[i], device_name) then  
 -- create midi device  
 device_name = inputs[i]  
 print ('Launchpad found: ' .. device_name)  
 midi_device = renoise.Midi.create_output_device(device_name)  
 -- add every detected launchpad to the launchpads table with device_name as key  
 launchpads[device_name] = midi_device  
-- later in the code, right dev_name is given  
midi_device = launchpads[dev_name]  
print ('is open:' .. tostring(midi_device.is_open))  

The last line in the above snippet “midi_device:send(message)” causes the following error:
MidiDevice=6- Launchpad
is open:true
*** .\launchpad.lua:168: attempt to index global ‘midi_device’ (a nil value)
*** stack traceback:
*** .\launchpad.lua:168: in function ‘set_light’

I am a bit puzzled why the midi_device is nil? The two print statements before deliver the right output.
Making midi_device to a local variable does not help, the same error appears with local instead global.

You first do this:
midi_device = renoise.Midi.create_output_device(device_name)

Later you do this:
midi_device = launchpads[dev_name]

I suspect you might be causing a conflict by overwriting a device you already have created.
So perhaps waiting with that until you really need it might do the trick.

Thanks, vV, I have found the error; the dev_name was nil and the debug output was quite puzzling first but made sense finally ;)

Ah yeah indeed, i overlooked that dev_name <> device_name