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

Java exception thrown when writing a file

Started by ItsRodrick, 23 January 2015 - 11:59 PM
ItsRodrick #1
Posted 24 January 2015 - 12:59 AM
Hello, first, I'm new to Lua. So I'm doing a installer for a little program I'm making and I get a Java exception thrown when trying to save a online file to a local one.

Code:
function install()
advWrite("Trying to get file",colors.black,colors.white,(termX-18)/2,subTitleH+3)
advWrite("1/3",colors.blue,colors.white,(termX-22)/2,subTitleH+3)
for i = 1,3 do
  if mode == "client" then local onlineFile = http.get(gitUrl.."lordLoginC").readAll() end
  if mode == "auth" then local onlineFile = http.get(gitUrl.."lordLoginA").readAll() end
  if mode == "server" then local onlineFile = http.get(gitUrl.."lordLoginS").readAll() end
  if onlineFile ~= nil then break end
end
term.setCursorPos(1,subTitleH+3)
term.setBackgroundColor(colors.white)
term.clearLine()
advWrite("Success",colors.green,colors.white,(termX-7)/2,subTitleH+3)
advWrite("Writing into local file",colors.gray,colors.white,(termX-23)/2,subTitleH+4)
sleep(1)
localFile = fs.open("lordLogin", "w")
print(onlineFile)
localFile.write(onlineFile)
localFile.close()
sleep(1)
end

It says "Trying to get file" and even "Success, Writing into local file", but then it gives a "Java exception thrown". Note, I'm using CCEmu Redux, and I couldn't test the program in Minecraft. Could it be that?
HPWebcamAble #2
Posted 24 January 2015 - 03:43 AM
I'm not sure what the problem is. I know the most common reason for a Java error is when a function calls itself more than 256 times.
I don't really see a place where that would happen here but its good to know.

Try commenting out

print(onlineFile)

If that doesn't work then try referencing the pastebin program that comes with Computer Craft to see how that works.
KingofGamesYami #3
Posted 24 January 2015 - 04:35 AM
I don't see anything that should cause an error - you might want to try running it in minecraft or mimic. If you run it on mimic, show the javascript console for details about what's going wrong.
Bomb Bloke #4
Posted 24 January 2015 - 05:53 AM
I'm going to guess that it's failing to open the file handle when you call fs.open(). Under ComputerCraft this'd result in the function returning nil instead, but a similar report (just yesterday I think?) suggests that CCEmu Redux outright errors under this circumstance.

One reason it might fail to open the file is because you've already got that file attached to an open handle in one of your non-localised variables that're probably still floating around in memory (bearing in mind that global variables are not automatically wiped when your program exits). Fully shutting down the emulator than restarting it may be all it takes to sort it out.
ItsRodrick #5
Posted 24 January 2015 - 03:05 PM
So, I tried print(onlineFile) and it printed "nil". Then, I simply did:
if mode == "client" then local onlineFile = http.get(gitUrl.."lordLoginC").readAll() end
to
if mode == "client" then onlineFile = http.get(gitUrl.."lordLoginC") end
onlineFile = onlineFile.readAll()

and it worked. I still couldn't test it on Minecraft, but tested in Mimic and the .readAll() in the first example kind that worked… But basically, my problem is solved. Thank you guys! It seems to be a CCEmuRedux error… but thanks!

(A moderator can close this thread :)/>)
Bubba #6
Posted 24 January 2015 - 06:54 PM
(A moderator can close this thread :)/>)

We don't often close threads – we usually leave them open so that you can keep your questions in this thread if you have any, or in case someone else has the same issue or a better solution.
ItsRodrick #7
Posted 24 January 2015 - 09:23 PM
Oh, ok, srry… I'm new to the forums… :)/>
Bomb Bloke #8
Posted 24 January 2015 - 11:19 PM
Come to think of it, you do need to use that sort of syntax in-game as well, though I still doubt you'd get the same error. I could be wrong. Certainly, localising onlineFile to those "if" blocks was always going to cause trouble!

Truth be told, you should also be closing those handles. In fact this block:

for i = 1,3 do
  if mode == "client" then local onlineFile = http.get(gitUrl.."lordLoginC").readAll() end
  if mode == "auth" then local onlineFile = http.get(gitUrl.."lordLoginA").readAll() end
  if mode == "server" then local onlineFile = http.get(gitUrl.."lordLoginS").readAll() end
  if onlineFile ~= nil then break end
end

… might be better re-written as something like:

local URL = {client = "lordLoginC", auth = "lordLoginA", server = "lordLoginS"}
URL = http.get(gitUrl..URL[mode])
local onlineFile = URL.readAll()
URL.close()