I2c Read example does not work

The I2c example here does not work for me.
https://electricimp.com/docs/resources/i2c/

function read_sensor_adc_0(i2c)
{
local word = i2c.read(i2c_addr, TSL2561_REGISTER_ADC0_LSB, 2);
local lumo = (word[1] << 8) + word[0];
return lumo;
}

when I try to run this I get this in the log.
“the index ‘1’ does not exist”

Then (more confusing) when I look at this example
https://electricimp.com/docs/api/hardware/i2c/read/

It shows this:
local hi = _i2c.read(_addr, REG_LTHB, 1)[0];
local lo = _i2c.read(_addr, REG_LTLB, 1)[0];
local temp = (hi << 3) | (lo >> 5);

Now, what is the [0] at the end all about ?

Likely, the read is failing and returning an error code (vs a 2 byte string, which is what you’d expect).

Try adding this code:
local word = i2c.read(i2c_addr, TSL2561_REGISTER_ADC0_LSB, 2); if (word == null) server.log("i2c error "+i2c.readerror());

…which will tell you what went wrong.

The [0] at the end of i2c_read is just retrieving the first byte from the string object returned by i2c.read(). Really, this is an example that needs updating because it doesn’t deal with error cases where null is returned as the read failed.

@hugo, after I posted this, I then tried just that. It was as you said returning an error code. Got it working now. thanks