变量返回为nil。

我对这个错误感到困惑。我正在为 mocp 编写一个 lua 脚本,在 conky 中显示信息。当我在“/media/Stuff/old-music”中播放音乐文件时,一切都正常,但当我在“/home/tony/Music”中播放音乐文件时,就会出现错误。我的脚本中没有硬编码的目录。错误显示一个变量被返回为nil。我检查了音乐文件,它具有所需的信息。为什么我会遇到这个错误?

错误信息:

Conky: llua_do_call: function conky_main execution failed: /home/tony/.conky/lua-test/albumart.lua:68: attempt to concatenate global 'album' (a nil value)

代码:

....
totaltime,totaltimesecs,song,artist,album,albumart=newsong(update_num)
....
function newsong()
    local f = io.popen("mocp -Q %album")
    album= f:read("*a")
    f:close()
    album=string.gsub(album,"[\n]","")
    local f = io.popen("mocp -Q %artist")
    artist= f:read("*a")
    f:close()
    artist=string.gsub(artist,"[\n]","")
    local f = io.popen("mocp -Q %song")
    song= f:read("*a")
    f:close()
    song=string.gsub(song,"[\n]","")
    local f = io.popen("mocp -Q %tt")
    totaltime= f:read("*a")
    f:close()
    totaltime=string.gsub(totaltime,"[\n]","")
    local f = io.popen("mocp -Q %ts")
    totaltimesecs= f:read("*a")
    f:close()
    totaltime=string.gsub(totaltime,"[\n]","")
return totaltime,totaltimesecs,song,artist,album,albumart

原文链接 https://stackoverflow.com/questions/9142318

点赞
stackoverflow用户221509
stackoverflow用户221509

你编写的代码似乎与实际的错误代码无关。错误代码具体描述了这个问题:

.../albumart.lua:68: attempt to concatenate global 'album' (a nil value)

这意味着你正试图使用连接运算符“..”连接变量album,而它的值恰好为nil。

你编写的代码表明这不应该是这种情况(尽管你可能想尝试将函数中的所有变量都设为“local”)。请检查你文件的第68行,找到问题所在。

2012-02-04 17:04:59