This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Ian-Moone's profile picture

[lua] [error]Attempt to index ? (a nil value)

Started by Ian-Moone, 22 March 2012 - 09:43 AM
Ian-Moone #1
Posted 22 March 2012 - 10:43 AM
i want to make it so using the string util s API if it finds certen key pieces of code in the start-up file it deletes the start-up.(its for my antivirus)
im also using a gui loadbar function called findvirus it is at the top.


-- Partial/All Code courtesy of Vandie
function findvirus(sx, fx, sym)
x,y = term.getCursorPos()
if y >= 18 then
term.clear()
term.setCursorPos(sx,1)
x,y = sx,1
end
term.setCursorPos(sx,y)
write("[")
term.setCursorPos(sx,y)
term.setCursorPos(fx-1,y)
write("]")
mdp = math.floor((sx + fx)/2) - 3
for i = (sx+1),(fx-2) do
term.setCursorPos(i,y)
write(sym)
sleep(0.4) --CHANGE THIS TO EDIT TIME
term.setCursorPos(mdp,y+1)
				write(string.format("%d",i/(fx-2) * 100))
write("%")
end
term.setCursorPos(1,y+2)
end
term.clear()
term.setCursorPos(1,1)
print("welcome to Vandie's advanced antivirus")
sleep(2)
if fs.exists("startup") then
term.clear()
term.setCursorPos(1,1)
print("checking for virus a1")
findvirus(1,20,"/")
sleep(1)
local file = io.open( "startup", "r" )
if StrUtils.StrUtils.contains("os.shutdown()","os.shutdown()")
then
file:close()
fs.delete("startup")
term.clear()
term.setCursorPos(1,1)
print("virus-a1 was found")
sleep(2)
print("virus-a1 was removed")
sleep(2)
else
file:close()
end
else
file:close()
end


and i get this error
43: attempt to index ? (a nil value)
Casper7526 #2
Posted 22 March 2012 - 10:45 AM
Fix your post :(/>/>
Ian-Moone #3
Posted 22 March 2012 - 10:51 AM
soz fixed the post
Liraal #4
Posted 22 March 2012 - 11:55 AM
your last else seems to refer to the 'if fs.exists()' line, but it is supposed to close a file that is not opened
Ian-Moone #5
Posted 22 March 2012 - 12:05 PM
so if i remove that it will work?
Liraal #6
Posted 22 March 2012 - 12:19 PM
i think it should. btw, you still have no protection against my virus. :(/>/> if you want, i can write it for you.
Wolvan #7
Posted 22 March 2012 - 12:24 PM
i think it should. btw, you still have no protection against my virus. :(/>/> if you want, i can write it for you.
And what are the functions of this "great" virus? Show me >:)/>/>
Advert #8
Posted 22 March 2012 - 12:24 PM
i want to make it so using the string util s API if it finds certen key pieces of code in the start-up file it deletes the start-up.(its for my antivirus)
im also using a gui loadbar function called findvirus it is at the top.


-- Partial/All Code courtesy of Vandie
function findvirus(sx, fx, sym)
x,y = term.getCursorPos()
if y >= 18 then
term.clear()
term.setCursorPos(sx,1)
x,y = sx,1
end
term.setCursorPos(sx,y)
write("[")
term.setCursorPos(sx,y)
term.setCursorPos(fx-1,y)
write("]")
mdp = math.floor((sx + fx)/2) - 3
for i = (sx+1),(fx-2) do
term.setCursorPos(i,y)
write(sym)
sleep(0.4) --CHANGE THIS TO EDIT TIME
term.setCursorPos(mdp,y+1)
				write(string.format("%d",i/(fx-2) * 100))
write("%")
end
term.setCursorPos(1,y+2)
end
term.clear()
term.setCursorPos(1,1)
print("welcome to Vandie's advanced antivirus")
sleep(2)
if fs.exists("startup") then
term.clear()
term.setCursorPos(1,1)
print("checking for virus a1")
findvirus(1,20,"/")
sleep(1)
local file = io.open( "startup", "r" )
if StrUtils.StrUtils.contains("os.shutdown()","os.shutdown()")
then
file:close()
fs.delete("startup")
term.clear()
term.setCursorPos(1,1)
print("virus-a1 was found")
sleep(2)
print("virus-a1 was removed")
sleep(2)
else
file:close()
end
else
file:close()
end


and i get this error
43: attempt to index ? (a nil value)


My thoughts:
* Comment #s denote line #s.
1) You are using globals, which is bad:


function findvirus(sx, fx, sym) -- 2
x,y = term.getCursorPos() -- 3
mdp = math.floor((sx + fx)/2) - 3 -- 14

2) You are trying to close the file in an else statement, where the file doesn't exist:


else -- 49
	file:close()
end


3) You are doing stuff in branches (if statements) you could do before you branch:


local file = io.open( "startup", "r" ) -- 35
->
		file:close() -- 38
		file:close() -- 47

4) Recursive call, you should try to do this in another way:


	findvirus(1,20,"/") -- 33

5) Weird function call?

You're not passing this function any data, and you're using StrUtils twice, and capitalized. I'm assuming you're using
thomass' strutils here.

	if StrUtils.StrUtils.contains("os.shutdown()","os.shutdown()") -- 36
	then

I'm not 100% sure, but I think it should look something more like:

        local sFileData = file:read("*a") -- read all the contents of the file
	if StrUtils.contains(sFileData,"os.shutdown()") -- 36
	then
Ian-Moone #9
Posted 22 March 2012 - 12:25 PM
actualy ive been keeping pt 2 secret for now ive nicknamed yourvirus a2
Ian-Moone #10
Posted 22 March 2012 - 12:27 PM
thanks guys big help…
damn credits are gona get looooooooooooooong!
Liraal #11
Posted 22 March 2012 - 12:27 PM
and better add an integrity check for APIs and/or an antivir thread in the background. I can help you with that.
Ian-Moone #12
Posted 22 March 2012 - 12:30 PM
yes please like i said any help is appreciated

and by the way have you and wolvan seen my signiture yet
Ian-Moone #13
Posted 22 March 2012 - 01:10 PM
new problem now its deleting the start-up file without even checking for the code

heres the new code

-- Partial/All Code courtesy of Vandie
term.clear()
term.setCursorPos(1,1)
print("welcome to Vandie's advanced antivirus")
sleep(2)
if fs.exists("startup") then
term.clear()
term.setCursorPos(1,1)
print("checking for virus a1")
sleep(1)
local file = io.open( "startup", "r" )
local sFileData = file:read("*a") -- read all the contents of the file
	    if StrUtils.contains(sFileData,"os.shutdown()") -- 36
	    then
file:close()
fs.delete("startup")
term.clear()
term.setCursorPos(1,1)
print("virus-a1 was found")
sleep(2)
print("virus-a1 was removed")
sleep(2)
else
file:close()
end
else
end
Liraal #14
Posted 22 March 2012 - 01:12 PM
but now it checks only if the startup file contains os.shutdown() anywhere. try running tests of StrUtils.contain to determine how exactly does it behave.
Ian-Moone #15
Posted 22 March 2012 - 01:14 PM
what sort of tests?
Liraal #16
Posted 22 March 2012 - 01:17 PM
with prepared startups, like clean (no code), a simple instruction (but not shutdown), virus a1, a code containing os.shutdown() somewhere, to determine, when the deletion takes part.