user3236841
user3236841

Reputation: 1258

lua error: attempt to call a nil value (field 'getn')

I have the following lua code snippet (that uses cairo)

 cairo_set_source_rgba(cr, COLOR_FONT_R, COLOR_FONT_G, COLOR_FONT_B, 1)
  cairo_set_font_size(cr, 0.011 * WINDOW_HEIGHT)
  local ps_str = conky_parse("${exec ps -Ao comm,pcpu,%mem  --sort=-pcpu | head -n 15}")
  local processes = {}
  for line in string.gmatch(ps_str, '([^\n]+)') do
    table.insert(processes, line)
  end
  for line = 1,table.getn(processes) do
    cairo_move_to(cr, 0.213 * WINDOW_WIDTH, 0.443 * WINDOW_HEIGHT + line * 0.014 * WINDOW_HEIGHT)
    cairo_show_text(cr, processes[line])
  end
  cairo_stroke(cr)

However, when I run it through conky, I get the following error (this is in the line, 5 lines from the end).

I get the error: attempt to call a nil value (field 'getn')

I have tried a few things suggested here, but I am not sure how to fix this so was wondering if there is an easy fix.

The suggested solution in the comments works beautifully for the above but not for the following:

function conky_geo_dotspiral(cx_str, cy_str, ...)
  local cx = conky_to_num(cx_str)
  local cy = conky_to_num(cy_str)
  local arms = math.ceil(24 / table.getn(arg)) * table.getn(arg)
  local rows = 10
  local radius0, radius1 = 50, 140
  local dotradius = 4
  for i,v_str in ipairs(arg) do
    v = conky_to_num(conky_parse(v_str))
    for j = i-1, arms - 1, table.getn(arg) do
      local p = j / arms
      for k = 0, v / rows  do
        local dx = cx + (radius0 + (radius1-radius0) * k/rows) * math.cos(p * 2*math.pi + k * math.pi/arms)
        local dy = cy + (radius0 + (radius1-radius0) * k/rows) * math.sin(p * 2*math.pi + k * math.pi/arms)
        cairo_arc (cr, dx, dy, dotradius, 0, 2 * math.pi)
        cairo_fill(cr)
      end
    end
  end
end

I get the error:

attempt to call a nil value (field 'getn')

I tried replacing table.getn(arg) with #arg but still get the error.

conky: llua_do_call: function conky_geo_dotspiral execution failed: conky_geometry.lua:155: attempt to get length of a nil value (global 'arg')

Upvotes: 4

Views: 6455

Answers (1)

user3236841
user3236841

Reputation: 1258

Here is the code snippet, fixed:

  cairo_set_source_rgba(cr, COLOR_FONT_R, COLOR_FONT_G, COLOR_FONT_B, 1)
  cairo_set_font_size(cr, 0.011 * WINDOW_HEIGHT)
  local ps_str = conky_parse("${exec ps -Ao comm,pcpu,%mem  --sort=-pcpu | head -n 15}")
  local processes = {}
  for line in string.gmatch(ps_str, '([^\n]+)') do
    table.insert(processes, line)
  end
  for line = 1,#processes do
    cairo_move_to(cr, 0.213 * WINDOW_WIDTH, 0.443 * WINDOW_HEIGHT + line * 0.014 * WINDOW_HEIGHT)
    cairo_show_text(cr, processes[line])
  end
  cairo_stroke(cr)

And the code snippet, fixed for the second question is:

function conky_geo_dotspiral(cx_str, cy_str, ...)
  local cx = conky_to_num(cx_str)
  local cy = conky_to_num(cy_str)
  local arms = math.ceil(24 / #arg) * #arg
  local rows = 10
  local radius0, radius1 = 50, 140
  local dotradius = 4
  for i,v_str in ipairs(arg) do
    v = conky_to_num(conky_parse(v_str))
    for j = i-1, arms - 1, #arg do
      local p = j / arms
      for k = 0, v / rows  do
        local dx = cx + (radius0 + (radius1-radius0) * k/rows) * math.cos(p * 2*math.pi + k * math.pi/arms)
        local dy = cy + (radius0 + (radius1-radius0) * k/rows) * math.sin(p * 2*math.pi + k * math.pi/arms)
        cairo_arc (cr, dx, dy, dotradius, 0, 2 * math.pi)
        cairo_fill(cr)
      end
    end
  end
  return ''
end

Thanks for all the suggestions that went into fixing this code.

Upvotes: 1

Related Questions