Jump to content




Error I cannot understand.


  • You cannot reply to this topic
35 replies to this topic

#1 brett122798

  • Members
  • 300 posts
  • LocationIn the TARDIS at an unknown place in time.

Posted 07 October 2012 - 12: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()


#2 Pinkishu

  • Members
  • 484 posts

Posted 07 October 2012 - 12:52 AM

Use file.write :3

#3 brett122798

  • Members
  • 300 posts
  • LocationIn the TARDIS at an unknown place in time.

Posted 07 October 2012 - 12:55 AM

View PostPinkishu, on 07 October 2012 - 12:52 AM, said:

Use file.write :3
Still have same error.

#4 Pinkishu

  • Members
  • 484 posts

Posted 07 October 2012 - 12:58 AM

Does the directory exist?

#5 brett122798

  • Members
  • 300 posts
  • LocationIn the TARDIS at an unknown place in time.

Posted 07 October 2012 - 01:01 AM

View PostPinkishu, on 07 October 2012 - 12:58 AM, said:

Does the directory exist?
No, it does not. I thought writing would automatically create the directories, doesn't it?

#6 Pinkishu

  • Members
  • 484 posts

Posted 07 October 2012 - 01:10 AM

Seems it doesn't xD atleast it worked for me once I created it

#7 brett122798

  • Members
  • 300 posts
  • LocationIn the TARDIS at an unknown place in time.

Posted 07 October 2012 - 01:55 AM

View PostPinkishu, on 07 October 2012 - 01:10 AM, said:

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!

#8 Pinkishu

  • Members
  • 484 posts

Posted 07 October 2012 - 01:59 AM

hmm strange, it fixed the error for me XD
So you made a ProgramData folder and inside of it a Chat folder?

#9 brett122798

  • Members
  • 300 posts
  • LocationIn the TARDIS at an unknown place in time.

Posted 07 October 2012 - 02:01 AM

View PostPinkishu, on 07 October 2012 - 01:59 AM, said:

hmm strange, it fixed the error for me XD
*Sigh..

#10 chiloxsan

  • Members
  • 56 posts
  • LocationVictoria, AUS

Posted 07 October 2012 - 02:28 AM

Try using io.open instead of fs.open.

#11 brett122798

  • Members
  • 300 posts
  • LocationIn the TARDIS at an unknown place in time.

Posted 07 October 2012 - 02:47 AM

View Postchiloxsan, on 07 October 2012 - 02:28 AM, said:

Try using io.open instead of fs.open.
Tried that earlier, got the same exact error.

#12 Watcher7

  • Members
  • 25 posts
  • LocationEquestria, TX

Posted 07 October 2012 - 02: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.

#13 chiloxsan

  • Members
  • 56 posts
  • LocationVictoria, AUS

Posted 07 October 2012 - 02: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"

#14 brett122798

  • Members
  • 300 posts
  • LocationIn the TARDIS at an unknown place in time.

Posted 07 October 2012 - 03:14 AM

View Postchiloxsan, on 07 October 2012 - 02:56 AM, said:

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


#15 chiloxsan

  • Members
  • 56 posts
  • LocationVictoria, AUS

Posted 07 October 2012 - 03:22 AM

On line 149, replace

file = username

with

username = file:readAll()


#16 brett122798

  • Members
  • 300 posts
  • LocationIn the TARDIS at an unknown place in time.

Posted 07 October 2012 - 03:34 AM

View Postchiloxsan, on 07 October 2012 - 03:22 AM, said:

On line 149, replace

file = username

with

username = file:readAll()
Ah, yes. One last thing, the username has some random name.. like "table: 73424782"

#17 chiloxsan

  • Members
  • 56 posts
  • LocationVictoria, AUS

Posted 07 October 2012 - 03: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.

#18 Luanub

    Lua Nub

  • Members
  • 1,135 posts
  • LocationPortland OR

Posted 07 October 2012 - 03: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()


#19 chiloxsan

  • Members
  • 56 posts
  • LocationVictoria, AUS

Posted 07 October 2012 - 03:51 AM

View Postluanub, on 07 October 2012 - 03:46 AM, said:

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.

#20 Luanub

    Lua Nub

  • Members
  • 1,135 posts
  • LocationPortland OR

Posted 07 October 2012 - 03:53 AM

Not in the case of the fs api.

A good tutorial on using fs is here





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users