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

attempt to index ? (a nil value) error

Started by DiamondOwner, 07 February 2013 - 10:29 AM
DiamondOwner #1
Posted 07 February 2013 - 11:29 AM
I'm trying to open a file, read the string, and save the string to the variable "pswdcheck". I used shell.resolve to try to find the absolute path. Error code: "logincheck:214: attempt to index ? (a nil value)". Note: The code below starts at line 212 and ends at line 215.

local storedpass=shell.resolve("/DiamondTNT-OS/"..username.."storedhashedpswd")
h=fs.open(storedpass,"r")
local pswdcheck=h.readAll()
h.close()
SuicidalSTDz #2
Posted 07 February 2013 - 12:25 PM
I'm trying to open a file, read the string, and save the string to the variable "pswdcheck". I used shell.resolve to try to find the absolute path. Error code: "logincheck:214: attempt to index ? (a nil value)". Note: The code below starts at line 212 and ends at line 215.

local storedpass=shell.resolve("/DiamondTNT-OS/"..username.."storedhashedpswd")
h=fs.open(storedpass,"r")
local pswdcheck=h.readAll()
h.close()

EDIT: I recommend using the io library instead of CC's fs library:

local storedpass = shell.resolve("/DiamondTNT-OS/"..username.."storedhashedpswd")
h = io.open(storedpass,"r")
local pswdcheck = h:read()
h:close()

If you are reading multiple lines, do this:

local storedpass = shell.resolve("/DiamondTNT-OS/"..username.."storedhashedpswd")
h = io.open(storedpass,"r")
local line = storedPass.readLine()
local data = {}
repeat
  table.insert(data,line)
  line = storedPass.readLine()
until line == nil
h:close()
fileData = data[1] --Most likely use a for loop to return all the data from the table

EDIT: The first code should work for you though ;)/> Have fun
Lyqyd #3
Posted 07 February 2013 - 12:55 PM
The fs file handles do expose a readAll method. Please do not put out inaccurate information.

OP, the problem is likely that the handle could not be opened. Try printing out the value of storedpass and make sure that the file exists. Obviously, you can't open a file for reading if it doesn't exist.
SuicidalSTDz #4
Posted 07 February 2013 - 01:04 PM
The fs file handles do expose a readAll method. Please do not put out inaccurate information.

OP, the problem is likely that the handle could not be opened. Try printing out the value of storedpass and make sure that the file exists. Obviously, you can't open a file for reading if it doesn't exist.

Im aware fs has a readAll method, but io does not and to my knowledge io is better since Lua beyond CC uses the io library. The fs library is only in CC therefore practically useless unless you want to play around with CC's fileSystem. It is better off to use the io library instead of using CC's since it only has certain "features" and is not extensive. It could very well be him not having an existing file that he is trying to read. So in my defense I am half right(Three quaters) The code I displayed DOES (or most likely) solve his error since it is attempting to index. However you could be right as well, that he does not have an existing file he is trying to read. Really it could be either way and we won't know until he is back online. Sorry for the misleading(but half-correct) information. I was just taught to use the io library. :)/>

EDIT: It is most likely that he is trying to open a non existing file since:

local storedpass=shell.resolve("/DiamondTNT-OS/"..username.."storedhashedpswd")
h=fs.open(storedpass,"r")

If he is getting the password of the user then he would have to create the file first then write the hashed Password. Open the file then read. Correct?
DiamondOwner #5
Posted 07 February 2013 - 01:34 PM
actually, i'm a bit sleepy today so i forgot i added the file to the disk instead of the computer :P/>. i accidentally broke the computer, so i will take a while to get the proper info back in.
SuicidalSTDz #6
Posted 07 February 2013 - 01:36 PM
actually, i'm a bit sleepy today so i forgot i added the file to the disk instead of the computer :P/>. i accidentally broke the computer, so i will take a while to get the proper info back in.
Lol I know the feeling, just glad Lyqyd and I could help you. Good luck with your(I'm guessing an OS)program!
Lyqyd #7
Posted 07 February 2013 - 02:05 PM
Im aware fs has a readAll method, but io does not and to my knowledge io is better since Lua beyond CC uses the io library. The fs library is only in CC therefore practically useless unless you want to play around with CC's fileSystem. It is better off to use the io library instead of using CC's since it only has certain "features" and is not extensive. It could very well be him not having an existing file that he is trying to read. So in my defense I am half right(Three quaters) The code I displayed DOES (or most likely) solve his error since it is attempting to index. However you could be right as well, that he does not have an existing file he is trying to read. Really it could be either way and we won't know until he is back online. Sorry for the misleading(but half-correct) information. I was just taught to use the io library. :)/>

Clearly, I should have quoted your previous post, since you have since removed the offending content. You were not "three quarters" correct, you were entirely wrong. It is, in my opinion, better to use the io library, but that is not worth correcting, especially when there are things that are actually likely to be wrong in the rest of the code. Stating flat-out that a method doesn't exist when it does is not three-quarters right by any means, it is entirely wrong. In addition, the second code snippet you posted uses period notation instead of colon notation and uses the readLine method with an io file handle, which is an fs file handle method. The correct io handle equivalent is handle:read("*l").
SuicidalSTDz #8
Posted 07 February 2013 - 03:24 PM
Im aware fs has a readAll method, but io does not and to my knowledge io is better since Lua beyond CC uses the io library. The fs library is only in CC therefore practically useless unless you want to play around with CC's fileSystem. It is better off to use the io library instead of using CC's since it only has certain "features" and is not extensive. It could very well be him not having an existing file that he is trying to read. So in my defense I am half right(Three quaters) The code I displayed DOES (or most likely) solve his error since it is attempting to index. However you could be right as well, that he does not have an existing file he is trying to read. Really it could be either way and we won't know until he is back online. Sorry for the misleading(but half-correct) information. I was just taught to use the io library. :)/>

Clearly, I should have quoted your previous post, since you have since removed the offending content. You were not "three quarters" correct, you were entirely wrong. It is, in my opinion, better to use the io library, but that is not worth correcting, especially when there are things that are actually likely to be wrong in the rest of the code. Stating flat-out that a method doesn't exist when it does is not three-quarters right by any means, it is entirely wrong. In addition, the second code snippet you posted uses period notation instead of colon notation and uses the readLine method with an io file handle, which is an fs file handle method. The correct io handle equivalent is handle:read("*l").

I'll remember that for next time then :)/> and I probably should have said that readAll does not exist in the IO LIBRARY, not in CC altogether. Sorry for the misunderstanding :(/>

EDIT: Is it or is it not better to use the io library in general, no matter the opinion? The io library is used universally, however the fs library is not. In conclusion that makes the io library the superior choice, correct? Anyway, is handle:read("*I") even in CC?
theoriginalbit #9
Posted 07 February 2013 - 03:31 PM
Anyway, is handle:read("*I") even in CC?
Yes, has been since the start I believe. Plus he wouldn't have said to use it if it didn't.
SuicidalSTDz #10
Posted 07 February 2013 - 03:50 PM
Anyway, is handle:read("*I") even in CC?
Yes, has been since the start I believe. Plus he wouldn't have said to use it if it didn't.
I thought he was referring to the io library outside of CC, i'll have to check this out then. Thanks
Lyqyd #11
Posted 07 February 2013 - 07:59 PM
The io library outside of ComputerCraft uses "*line" instead, I believe. A minor difference. I personally use the io library each time, mostly because I always use it when reading files if I'm going to be iterating through lines, and it's simply easier to use it in all cases. If code here has correct usage of the fs library, it's not particularly worth advising them to switch (and vice versa), since time should be devoted to answering the question at hand or fixing the actual problems with the code. Neither choice is particularly superior in and of itself, though the io library does have the :lines() iterator added to it, and the fs library does not.
LBPHacker #12
Posted 08 February 2013 - 08:24 AM
actually, i'm a bit sleepy today so i forgot i added the file to the disk instead of the computer :P/>. i accidentally broke the computer, so i will take a while to get the proper info back in.

BTW You can restore a computer (or at least its files) even if you've broken it. Find the save you are playing on, (suppose it's named "world"). saves\world\computer\ stores all files of all computers - event the non-existent ones'.

Better late than never, isn't it?
DiamondOwner #13
Posted 08 February 2013 - 09:22 AM
ik that, thx though. i don't have access to the files because it's a server and i'm not host. i just restored it today by sending the files to the server host, who replaced the files.