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

Error I cannot understand.

Started by brett122798, 06 October 2012 - 11:44 PM
brett122798 #1
Posted 07 October 2012 - 01:44 AM
Hey, I'm getting this error that I just cannot figure out, here it is:


chat:120: attempt to index ? (a nil value)

My Code:


file = fs.open("ProgramData/Chat/Username", "w")
file:write(newusername)			   -- This is line 120
file:close()
Pinkishu #2
Posted 07 October 2012 - 01:52 AM
Use file.write :3
brett122798 #3
Posted 07 October 2012 - 01:55 AM
Use file.write :3
Still have same error.
Pinkishu #4
Posted 07 October 2012 - 01:58 AM
Does the directory exist?
brett122798 #5
Posted 07 October 2012 - 02:01 AM
Does the directory exist?
No, it does not. I thought writing would automatically create the directories, doesn't it?
Pinkishu #6
Posted 07 October 2012 - 02:10 AM
Seems it doesn't xD atleast it worked for me once I created it
brett122798 #7
Posted 07 October 2012 - 02:55 AM
Seems it doesn't xD atleast it worked for me once I created it
I created the path and it STILL has the same error!
Pinkishu #8
Posted 07 October 2012 - 02:59 AM
hmm strange, it fixed the error for me XD
So you made a ProgramData folder and inside of it a Chat folder?
brett122798 #9
Posted 07 October 2012 - 03:01 AM
hmm strange, it fixed the error for me XD
*Sigh..
chiloxsan #10
Posted 07 October 2012 - 03:28 AM
Try using io.open instead of fs.open.
brett122798 #11
Posted 07 October 2012 - 03:47 AM
Try using io.open instead of fs.open.
Tried that earlier, got the same exact error.
Watcher7 #12
Posted 07 October 2012 - 03:56 AM
Pastebin the entire thing or be sure to thoroughly re-read your source.
It's possible that 'newusername' is not assigned (possibly from mis-typing the variable) and points to nil.
It's far easier to help you if you let us see the entire source.
chiloxsan #13
Posted 07 October 2012 - 03:56 AM
Also, when using file paths in ComputerCraft programs, the path is not relative to where the program is being run, everything is based off the root directory.

So, for example I had a directory tree like this:

/
|
+ - Folder1
...|
...+- Program
...+- ProgramData
....+- Chat
......+- Username


If I ran my program in Folder1 and it tried to open "ProgramData/Chat/Username", it wouldn't open "Folder1/ProgramData/Chat/Username", it would open "/ProgramData/Chat/Username"
brett122798 #14
Posted 07 October 2012 - 04:14 AM
Also, when using file paths in ComputerCraft programs, the path is not relative to where the program is being run, everything is based off the root directory.

So, for example I had a directory tree like this:

/
|
+ - Folder1
...|
...+- Program
...+- ProgramData
....+- Chat
......+- Username


If I ran my program in Folder1 and it tried to open "ProgramData/Chat/Username", it wouldn't open "Folder1/ProgramData/Chat/Username", it would open "/ProgramData/Chat/Username"
Okay, the slash in front of ProgramData fixed 1 problem, now when i start the program it says:


chat:150: attempt to call nil

Now, here's the code(not finished..):


-- BETA CONFIG


username = "Default"

WirelessModemSide = "left"  -- Define the side
-- your wireless modem is on

-- Available Sides: top, bottom, left, right,
-- front, back


-- END OF BETA CONFIG







line1 = " "
line2 = " "
line3 = " "
line4 = " "
line5 = " "
line6 = " "
line7 = " "
line8 = " "
line9 = " "
line10 = " "
line11 = " "
line12 = " "
line13 = " "
line14 = " "
line15 = " "
line16 = " "
line17 = " "

rednet.open(WirelessModemSide)
function receiver()
id, message = rednet.receive()
term.clear()
term.setCursorPos(1, 1)
line1 = line2
line2 = line3
line3 = line4
line4 = line5
line5 = line6
line6 = line7
line7 = line8
line8 = line9
line9 = line10
line10 = line11
line11 = line12
line12 = line13
line13 = line14
line14 = line15
line15 = line16
line16 = line17
line17 = message
print(line1)
print(line2)
print(line3)
print(line4)
print(line5)
print(line6)
print(line7)
print(line8)
print(line9)
print(line10)
print(line11)
print(line12)
print(line13)
print(line14)
print(line15)
print(line16)
print(line17)
end

function chat()
term.setCursorPos(1, 18)
write("> ")
chatmessageraw = read()
chatmessage = (username..": "..chatmessageraw)
rednet.broadcast(chatmessage)
line1 = line2
line2 = line3
line3 = line4
line4 = line5
line5 = line6
line6 = line7
line7 = line8
line8 = line9
line9 = line10
line10 = line11
line11 = line12
line12 = line13
line13 = line14
line14 = line15
line15 = line16
line16 = line17
line17 = chatmessage
print(line1)
print(line2)
print(line3)
print(line4)
print(line5)
print(line6)
print(line7)
print(line8)
print(line9)
print(line10)
print(line11)
print(line12)
print(line13)
print(line14)
print(line15)
print(line16)
print(line17)
end

term.clear()
term.setCursorPos(1, 1)
if fs.exists("ProgramData/Chat") == false then
print("-------------------------------------------------")
print("|			    Choose Username			    |")
print("-------------------------------------------------")
print("|											   |")
print("|											   |")
print("|											   |")
print("-------------------------------------------------")
term.setCursorPos(3, 5)
write("Username: ")
newusername = read()
term.clear()
term.setCursorPos(1, 2)
print("Writing..")
sleep(0.5)
fs.makeDir("ProgramData")
fs.makeDir("ProgramData/Chat")
file = fs.open("/ProgramData/Chat/Username", "w")
file:write(newusername)
file:close()
term.clear()
term.setCursorPos(1, 1)
end

file = fs.open("/ProgramData/Chat/Username", "r")
file = username
file:close()			  -- Line 150

while true do
parallel.waitForAny(receiver, chat)
end
chiloxsan #15
Posted 07 October 2012 - 04:22 AM
On line 149, replace


file = username

with


username = file:readAll()
brett122798 #16
Posted 07 October 2012 - 04:34 AM
On line 149, replace


file = username

with


username = file:readAll()
Ah, yes. One last thing, the username has some random name.. like "table: 73424782"
chiloxsan #17
Posted 07 October 2012 - 04:45 AM
Are you sure you are not assigning username to a file object or something else that's not a string. The 'random name' you posted is actually the string thats the output of converting a table to a string.
Luanub #18
Posted 07 October 2012 - 04:46 AM
The reason you are getting this error "chat:150: attempt to call nil" is your using the fs API but using :'s when using the functions. fs uses .'s and io uses :'s.

the command h:close() does not exist for the fs AP so it says your trying to call nil. the syntax for fs is h.close()


file:close()
--should actually be
file.close()
--and
file:readAll()
--should be
file.readAll()
chiloxsan #19
Posted 07 October 2012 - 04:51 AM
The reason you are getting this error "chat:150: attempt to call nil" is your using the fs API but using :'s when using the functions. fs uses .'s and io uses :'s.

the command h:close() does not exist for the fs AP so it says your trying to call nil. the syntax for fs is h.close()


file:close()
--should actually be
file.close()
--and
file:readAll()
--should be
file.readAll()

That's very interesting, I thought to access functions inside an object in Lua you use colons and not periods.
Luanub #20
Posted 07 October 2012 - 04:53 AM
Not in the case of the fs api.

A good tutorial on using fs is here
Kingdaro #21
Posted 07 October 2012 - 04:54 AM
It's actually both. You can call functions inside tables with colons and periods - the combination in which they are defined and called usually depends on if the user wants to use the "self" keyword.

In this case though, you can do both without any problems (I think).
Watcher7 #22
Posted 07 October 2012 - 04:56 AM
Also, please learn to indent your code with atleast 2 spaces (4 spaces or 1 tab is more common) per block, otherwise people can't read large scripts you make and you might get lost in your own code (and get laughed at by less understanding programmers).

Example of indentation:

local indentation = {}

function indentation.example()
	local correct = true
	if correct then
		for counter = 1, 10 do
			print('This is an example of "correct" indentation.')
		end
	end
end

indentation.example()
brett122798 #23
Posted 07 October 2012 - 04:58 AM
Not in the case of the fs api.

A good tutorial on using fs is here
Both ways work, that is currently not my problem atm. Look above. EDIT: Last Page


EDIT: Ninja'd

Also, please learn to indent your code with atleast 2 spaces (4 spaces or 1 tab is more common) per block, otherwise people can't read large scripts you make and you might get lost in your own code (and get laughed at by less understanding programmers).

Example of indentation:

local indentation = {}

function indentation.example()
	local correct = true
	if correct then
		for counter = 1, 10 do
			print('This is an example of "correct" indentation.')
		end
	end
end

indentation.example()

I know, my code looks horrible, but the reason I don't is cause I think it's wasting time.
Watcher7 #24
Posted 07 October 2012 - 05:01 AM
Not in the case of the fs api.

A good tutorial on using fs is here
Both ways work, that is currently not my problem atm. Look above. EDIT: Last Page


EDIT: Ninja'd

Also, please learn to indent your code with atleast 2 spaces (4 spaces or 1 tab is more common) per block, otherwise people can't read large scripts you make and you might get lost in your own code (and get laughed at by less understanding programmers).

Example of indentation:

local indentation = {}

function indentation.example()
	local correct = true
	if correct then
		for counter = 1, 10 do
			print('This is an example of "correct" indentation.')
		end
	end
end

indentation.example()

I know, my code looks horrible, but the reason I don't is cause I think it's wasting time.
Hitting the tab key isn't that hard nor is it time consuming.
brett122798 #25
Posted 07 October 2012 - 05:05 AM
Hitting the tab key isn't that hard.
You cannot tab on CC computers, and half my code was done on one.
Luanub #26
Posted 07 October 2012 - 05:09 AM
Try using io.open() and the just file:read() and see what that does. Since your storing a single word string in the file it should properly read it.

Other things to check:
What do the contents of the file look like after the write?
You can also do something like this to check what the variable type is:

sType = type(newusername)
print(sType)
Do the same thing for username.
Also print them on the screen when they are being written or read so that you can see what it is doing. That should give you an idea as to what is going wrong.
chiloxsan #27
Posted 07 October 2012 - 05:09 AM
Hitting the tab key isn't that hard.
You cannot tab on CC computers, and half my code was done on one.

If you are playing any version under 1.4 (such as the version that is included on Tekkit), the text editor does not tab. Any version above and when you hit tab the line is indented by 2 spaces.
Watcher7 #28
Posted 07 October 2012 - 05:12 AM
Hitting the tab key isn't that hard.
You cannot tab on CC computers, and half my code was done on one.
But CC is now able to pull from your clip-board, you should not be doing anything but quick edits in-game with edit. All your work should be done outside in your editor/IDE of choice and transfered over (or edited directly if the files are local).
brett122798 #29
Posted 07 October 2012 - 05:20 AM
Hitting the tab key isn't that hard.
You cannot tab on CC computers, and half my code was done on one.

If you are playing any version under 1.4 (such as the version that is included on Tekkit), the text editor does not tab. Any version above and when you hit tab the line is indented by 2 spaces.
I'm probably gonna get flamed for this, but, I'm never updating to 1.3 or anything above, I'm sick of the horrible updates. So there is no way I'll get that feature.
brett122798 #30
Posted 07 October 2012 - 05:23 AM
Try using io.open() and the just file:read() and see what that does. Since your storing a single word string in the file it should properly read it.

Other things to check:
What do the contents of the file look like after the write?
You can also do something like this to check what the variable type is:

sType = type(newusername)
print(sType)
Do the same thing for username.
Also print them on the screen when they are being written or read so that you can see what it is doing. That should give you an idea as to what is going wrong.
Hey, thanks. io.open worked! Time to move on and get more annoying errors!
Luanub #31
Posted 07 October 2012 - 05:23 AM
So I tested it and the contents of the file are "table: 73424782".

Even though the var username was correct and was a string.

So I changed your :'s to .'s and it works like a charm. As I said before with fs you need .'s not :'s…. You're error will go away if you replace them.

EDIT:Changing to io works too since it uses :'s that you have in the code.
Edited on 07 October 2012 - 03:24 AM
Cloudy #32
Posted 07 October 2012 - 09:21 AM
I bet you're one of those kind of people who complain if updates don't happen and also if they do.

I'm curious, what makes 1.3.2 a horrible update?
1lann #33
Posted 07 October 2012 - 09:43 AM
Hitting the tab key isn't that hard.
You cannot tab on CC computers, and half my code was done on one.
But CC is now able to pull from your clip-board, you should not be doing anything but quick edits in-game with edit. All your work should be done outside in your editor/IDE of choice and transfered over (or edited directly if the files are local).

Actually when you paste from your clipboard in cc, it only pastes 1 line of your clipboard. Unless your program is one line, it's pretty much useless to copy and paste. Also since CTRL is the key to open the edit menu, it kinda stuffs up.
brett122798 #34
Posted 07 October 2012 - 09:51 AM
I bet you're one of those kind of people who complain if updates don't happen and also if they do.

I'm curious, what makes 1.3.2 a horrible update?
No, I like updates not to come, unless they're good. They also cause to have modders to update their mods, and stuff like that.

All updates have had something bad ever since Beta 1.8(which is what I call "The Death Update"). 1.3 just had so many poor changes since it took like 3+ months to update, and very little good. About all changes that were made were bad because I say "If it's fixed, don't try to fix it and end up breaking it." Most additions in 1.3 I just plain didn't like except like the ender chest. Oh, and 1.4 is just as bad too. Sorry, I come from alpha(probably you too) when things were simple, yet more fun.
Cloudy #35
Posted 07 October 2012 - 10:16 AM
1.3 was a necessary rewrite to allow the modding API. Well worth it in the long run. I'm from alpha too - but I like the changes - I guess that's where we differ - but I'm off topic - bad Cloudy!
brett122798 #36
Posted 07 October 2012 - 10:47 AM
1.3 was a necessary rewrite to allow the modding API. Well worth it in the long run. I'm from alpha too - but I like the changes - I guess that's where we differ - but I'm off topic - bad Cloudy!
Are the clouds gonna be mad at you now? J:

Did I hear thunder?