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

Attempt To Index (a Nil Value) On File.write()

Started by crazyadam0, 29 October 2013 - 10:03 PM
crazyadam0 #1
Posted 29 October 2013 - 11:03 PM
I am starting work on my graphical shell winMC again and well i'm having a problem. In the system requirements checker ran each time the OS starts it checks if there is a user. If there is not one it asks for information and creates a user profile for winMC but when writing to the loginInfo file it has an error.The error is a follows: checkOSReq:118:attempt to index? (a nil value). Line 118 is this: file.write(usn);
What am I doing wrong? OH AND BTW do you have any suggestions for how to make the code better?
Spoiler

local col = term.setTextColor;
local bcol = term.setBackgroundColor;
local w,h = term.getSize();
term.clear();
term.setCursorPos(1,1);
if term.isColor then
  col(colors.lime);
  print("DETECTED: COLOR MONITOR");
else
  error("ERROR: MUST BE RUN ON ADVANCED COMPUTER");
end
sleep(0.1);
if fs.isDir("/.winMC") then
  col(colors.lime);
  print("DETECTED: WINMC DIRECTORY");
else
  col(colors.red);
  print("ERROR NO WINMC DIRECTORY FOUND");
  fs.makeDir("/.winMC");
  print("  CREATED DIRECTORY");
end
sleep(0.1);
if fs.isDir("/.winMC/.OS") then
  col(colors.lime);
  print("DETECTED: OS DIRECTORY");
else
  col(colors.red);
  print("ERROR NO OS DIRECTORY FOUND");
  fs.makeDir("/.winMC/.OS");
  print("  CREATED DIRECTORY");
end
sleep(0.1);
if fs.isDir("/.winMC/.user") then
  col(colors.lime);
  print("DETECTED: USERS DIRECTORY");
else
  col(colors.red);
  print("ERROR NO USERS DIRECTORY FOUND");
  fs.makeDir("/.winMC/.user");
  print("  CREATED DIRECTORY");
end
sleep(0.1);
function createUser()
  bcol(colors.blue);
  term.clear()
  term.setCursorPos(10,6);
  bcol(colors.gray);
  col(colors.gray);
  print("							   ");
  term.setCursorPos(10,7);
  print("							   ");
  term.setCursorPos(10,8);
  print("							   ");
  term.setCursorPos(10,9);
  print("							   ");
  term.setCursorPos(10,10);
  print("							   ");
  term.setCursorPos(10,11);
  print("							   ");
  term.setCursorPos(10,12);
  print("							   ");
  col(colors.white);
  term.setCursorPos(19,6);
  print("USER CREATION");
  term.setCursorPos(13,7);
  write("USERNAME:");
  bcol(colors.lightGray);
  print("			   ");
  term.setCursorPos(13,9);
  bcol(colors.gray);
  write("PASSWORD:");
  bcol(colors.lightGray);
  print("			   ");
  term.setCursorPos(13,11);
  bcol(colors.gray);
  write("PWD HINT:");
  bcol(colors.lightGray);
  print("			   ");
  bcol(colors.lightGray);
  term.setCursorPos(21,12);
  write("CONFIRM");
  local valid
  local usn;
  local pwd;
  local hnt;
  local switchnum;
  while not valid do
	local event,button,x,y = os.pullEvent("mouse_click");
	col(colors.lime);
  
	if(y == 7 and x >= 22 and x <= 36) then
	  bcol(colors.lightGray);
	  col(colors.white);
	  term.setCursorPos(22,7);
	  usn = read();
	  switchnum = 2;
	end
	if(y == 9 and x >= 22 and x <= 36) or switchnum == 2 then
	  bcol(colors.lightGray);
	  col(colors.white);
	  term.setCursorPos(22,9);
	  pwd = read();
	  switchnum = 3;
	end
	if(y == 11 and x >= 22 and X <= 36) or switchnum == 3 then
	  bcol(colors.lightGray);
	  col(colors.white);
	  term.setCursorPos(22,11);
	  hnt = read();
	  switchnum = 0;
	end
	if(y == 12 and x >= 21 and x <= 27)then
	  if usn and pwd and hnt then
		valid = true;
		fs.makeDir("/.winMC/.user/"..usn.."/.system");
		local userinfo = fs.open("/.winMC/.user/"..usn.."/.system/loginInfo","w");
							   --################################################################################<<<<<<<<<<<<<<<<<<
		file.write(usn); --line with the error is here ############################################################<<<<<<<<<<<<<<<<<<LINE 118
		file.write(pwd);--################################################################################<<<<<<<<<<<<<<<<<<
		file.write(hnt);
	  end
	end
  end
end
if fs.exists("/.winMC/.OS/hasUser") then
  col(colors.lime);
  print("DETECTED: USER");
else
  col(colors.red);
  print("ERROR NO USER FOUND");
  print("  CREATING USER");
  sleep(2);
  createUser();
end
LBPHacker #2
Posted 30 October 2013 - 01:55 AM
Well, that's not a surprise. file is nil. The handle to loginInfo is stored in userinfo; you should use the methods of that table.
userinfo.write(usn)
-- and so on
And don't forget to flush or close the file.
userinfo.close()
crazyadam0 #3
Posted 30 October 2013 - 08:31 AM
OH MY GOODNESS I FEEL LIKE AN IDIOT lol. Thanks sometimes you need a person to not see what you think is there. I was sure i saw it say FILE = fs.open()………