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

[Lua][Error]Attempt to index? (a null value)

Started by Machinecraft-server, 23 September 2012 - 01:10 PM
Machinecraft-server #1
Posted 23 September 2012 - 03:10 PM
Ok, So I have been working on an A.T.M for our server bank, but keep on getting the same error, and I don't understand it

code: http://pastebin.com/jNg6PZrK

EDIT: The error is on line 41, and updated code to latest version

Please get back to me soon, thanks in advance, dvd604

Ps, Who ever figures this outis a genius :P/>/>
Machinecraft-server #2
Posted 23 September 2012 - 04:26 PM
I need help, I'm stumped :P/>/>
sjele #3
Posted 23 September 2012 - 04:34 PM
Try to change line 41 too:

file1.close()

Allso change line 51 too

file.close()

Allso there is no need to bumb a post.
MysticT #4
Posted 23 September 2012 - 04:38 PM
Ok, I changed some of the code and added comments on the errors:

--Global Vars
local version = "0.1 Beta (Broken)"

--Startup code for directory

if fs.exists("/bank/") then
  print("Bank Directory exsits, skipping Create code!")
else
  fs.makeDir("/bank")
end
sleep(2)

--end

--Money declorations
local money = "0"
local account_money = money
--end

--main loop
while true do
  if money ~= "0" then
    account_money = money
    money = "0"
  end
  term.clear()
  term.setCursorPos(1,1)
  print("Money Server ", version)
  print("")
  print("Please note, this is monitored 24/7")
  write("Account name: ")
  local account = read()
  if fs.exists(fs.combine("bank/", account)) then
    local file = fs.open(fs.combine("bank/", account), "w") -- open file in write mode
    file.writeLine("1000") -- write "1000" to the file
    if file.readLine() ~= "" then -- read a line from file. ERROR: file is in write mode, you can't read
	  account_money1 = file1.readLine() -- read another line from the file. Same error here
    end
  end
  file.close() -- close the file
  print(account.." contains $"..account_money1)
  write("Edit? ")
  local ans = read()
  if ans == "yes" then
    write("Amount to withdraw: ")
    money = money - read() -- ERROR: trying to subtract two strings
    local file = fs.open(fs.combine("bank/", account), "w")
    file.writeLine(money)
    file.close()
  end
end
The main problem is the file handling. Try learning how the fs api works to solve this.
Machinecraft-server #5
Posted 23 September 2012 - 05:08 PM
Thanks very much to both of you, and sorry for the bump. I will get back to you if it fixes it :P/>/>