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

attempt to perform arithmetic __sub on nil and number

Started by Mr_Programmer, 09 December 2017 - 11:07 PM
Mr_Programmer #1
Posted 10 December 2017 - 12:07 AM
Okay so i have this code that erroring out "attempt to perform arithmetic __sub on nil and number" i have no clue, tried eerything i knoe, ive had a break from cc so alittle rusty atm


function configGet()
clientInfo = fs.open("Server_Cfg/ServerConfig/ClientUsage/ClientID", "r")
  for line in clientInfo.readLine do
   clientConfigGet[ #clientConfigGet + 1 ] = line
  end
clientInfo.close()
end
term.setCursorPos(math.floor(w-#"Client ID: "..tostring(clientConfigGet[1])),7)

And this is also erroring "attempt to index? (a nil value)


print("Client ID: "..tostring(clientConfigGet[1]))
Edited on 09 December 2017 - 11:35 PM
Bomb Bloke #2
Posted 10 December 2017 - 12:46 AM
The first error is telling you that "w" is undefined at that point in your code. The second error is telling you that "clientConfigGet" is undefined at that point in your code. If you need more explanation than that, then show the rest of your code.
Mr_Programmer #3
Posted 10 December 2017 - 12:57 AM
The first error is telling you that "w" is undefined at that point in your code. The second error is telling you that "clientConfigGet" is undefined at that point in your code. If you need more explanation than that, then show the rest of your code.

Cheers for the reply, i managed to sort the first issue out, i juts missed the w statement and i sorted half of the secpnd issue, i forgot to call the function hence why it was "undefined" now that i have called the function im getting a error saying "attempt to get lenght of nil" on line 49 which is clientConfigGet[ #clientConfigGet + 1 ] = line, there is the number 1 sorted in the file
Edited on 10 December 2017 - 12:40 AM
Mr_Programmer #4
Posted 10 December 2017 - 12:46 PM
.
SquidDev #5
Posted 10 December 2017 - 12:54 PM
.
Please don't bump the thread like this. People will respond when they've got time/willpower.

I'm getting a error saying "attempt to get length of nil" on line 49 which is clientConfigGet[#clientConfigGet + 1 ] = line, there is the number 1 sorted in the file
One of the many joys of computer programming is reading error messages and working out what they mean. Let's look at a couple of earlier error messages we've seen:
  • attempt to perform arithmetic __sub on nil and number
  • attempt to index? (a nil value)
Notice in both cases, we're trying to do something on nil. And in both cases, the issue was that one of the variables was undefined. Now then, let's go back to our previous error message: see that pesky nil again? We must be trying to get the length of some variable which hasn't been defined: namely clientConfigGet. If we define it, I'm sure all our issues will go away!

One useful trick to try is "printf debugging" (named after a C function of the same name). Basically one scatters print calls everywhere to see if values are what you expect them to be. For instance, one could do something like this:

for line in clientInfo.readLine do
  print("clientConfigGet =", clientConfigGet) --# Will print out "clientConfigGet = nil"
  clientConfigGet[ #clientConfigGet + 1 ] = line
end
Mr_Programmer #6
Posted 10 December 2017 - 01:16 PM
.
Please don't bump the thread like this. People will respond when they've got time/willpower.

I'm getting a error saying "attempt to get length of nil" on line 49 which is clientConfigGet[#clientConfigGet + 1 ] = line, there is the number 1 sorted in the file
One of the many joys of computer programming is reading error messages and working out what they mean. Let's look at a couple of earlier error messages we've seen:
  • attempt to perform arithmetic __sub on nil and number
  • attempt to index? (a nil value)
Notice in both cases, we're trying to do something on nil. And in both cases, the issue was that one of the variables was undefined. Now then, let's go back to our previous error message: see that pesky nil again? We must be trying to get the length of some variable which hasn't been defined: namely clientConfigGet. If we define it, I'm sure all our issues will go away!

One useful trick to try is "printf debugging" (named after a C function of the same name). Basically one scatters print calls everywhere to see if values are what you expect them to be. For instance, one could do something like this:

for line in clientInfo.readLine do
  print("clientConfigGet =", clientConfigGet) --# Will print out "clientConfigGet = nil"
  clientConfigGet[ #clientConfigGet + 1 ] = line
end

Thanks for the tips, squidDev however why is it getting a nil value if its reading the file and the file has something init? its probs something so simple but i cant get my head around it
SquidDev #7
Posted 10 December 2017 - 01:18 PM
Thanks for the tips, squidDev however why is it getting a nil value if its reading the file and the file has something init? its probs something so simple but i cant get my head around it
It's not the file which is nil (or anything file related), but the clientConfigGet variable. You'll need to define it somewhere: putting clientConfigGet = {} before your loop should be enough.
Mr_Programmer #8
Posted 10 December 2017 - 01:20 PM
Thanks for the tips, squidDev however why is it getting a nil value if its reading the file and the file has something init? its probs something so simple but i cant get my head around it
It's not the file which is nil (or anything file related), but the clientConfigGet variable. You'll need to define it somewhere: putting clientConfigGet = {} before your loop should be enough.

See something so simple, ive always done my work without tables xD and im only just starting to use them so still getting my head around them, thanks very much :)/>