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

Function is only nil when passing in parameters?

Started by ElvishJerricco, 05 August 2012 - 03:50 AM
ElvishJerricco #1
Posted 05 August 2012 - 05:50 AM
I'm fairly good at this computer craft stuff usually. Lua? No problem. Procedural programming concepts? No problem! But the issue i'm having now has stumped me like nothing has ever stumped me before.

I've got an API called networkApplication. I load the API via os.loadAPI, then call a function in it called init(netInfo). Now the issue arises with that parameter (it's a table, btw) in the init function. when i call the function with networkApplication.init(myNetInfo), it gives me an "attempt to call nil" error, as if the function doesn't exist. However, if i simply leave out the parameter when calling the function (so my call looks like "networkApplication.init()"), it calls the function just fine. Obviously, i get an error for trying to index the nil value that is the parameter. So what the heck is going on here?
kotorone1 #2
Posted 05 August 2012 - 06:35 AM
can we see the code please?
Lyqyd #3
Posted 05 August 2012 - 06:35 AM
Post the code. Include the declaration and assignment of myNetInfo (the variable being passed to the function).
ElvishJerricco #4
Posted 05 August 2012 - 07:28 AM
Post the code. Include the declaration and assignment of myNetInfo (the variable being passed to the function).


local port = math.random(80000)
myNetInfo = {dest="3.2", toPort=80, port=port, callback = myCallback}
truth = networkApplication.init(myNetInfo)

There is no doubt in my mind that myCallback (which is a function) exists, as it works when i try to use it before and after the myNetInfo declaration. The line that calls init is the one that gives me an error. here's the init function and the function it jumps into.


function init(netInfo)
netInfo.finished = {}
netInfo.genericCallback = genericCallback
return openConnection(netInfo)
end
local function openConnection(netInfo)
if network.bindOpenPort(netInfo) then
  network.send(netInfo, k.ApplicationOpen)
  return true
else
  netInfo.error = k.ApplicationPortBindFailure
  return false
end
end

the k.x variables are constants. I have an API loaded that holds several constants for my programs. I can assure that they also do exist as expected.
Lyqyd #5
Posted 05 August 2012 - 08:47 AM
Okay, let's see the whole code, then. That all looks good so far. Declaration and assignment of genericCallback and network (including bindOpenPort and send).

Also, which line does it indicate that the attempt to call nil is happening on? My guess is that it's on the network.bindOpenPort call, or a call inside of it.

As an aside, you can assign port in the table declaration directly to math.random(80000), without the need for an intermediate variable.
ElvishJerricco #6
Posted 05 August 2012 - 09:02 AM
Okay, let's see the whole code, then. That all looks good so far. Declaration and assignment of genericCallback and network (including bindOpenPort and send).

Also, which line does it indicate that the attempt to call nil is happening on? My guess is that it's on the network.bindOpenPort call, or a call inside of it.

As an aside, you can assign port in the table declaration directly to math.random(80000), without the need for an intermediate variable.

There's well over a thousand lines of code for this API i'm building. Do you really wanna sift through all that? =P Plus what i'm showing here encompasses more than the whole chunks of code that declare the variables and functions that could be causing problems.

But i can assure you, as i've said before, the error is occurring on the line that calls my init function. But it doesn't throw the error when i don't include the parameter in the call (Edit: obviously, it throws errors when the init function starts trying to use a nonexistent table, but the error i'm not understanding is the one that says init doesn't exist, but only when i include the parameter in my call). As for declaring port in the table declaration, i need the port variable later in my code.
Cloudy #7
Posted 05 August 2012 - 11:11 AM
Without seeing the whole code we can't see what is wrong.
MysticT #8
Posted 05 August 2012 - 04:23 PM
I think this line is the problem:

myNetInfo = {dest="3.2", toPort=80, port=port, callback = myCallback}
The keys should be strings, like this:

myNetInfo = { ["dest"] = "3.2", ["toPort"] = 80, ["port"] = port, ["callback"] = myCallback }
If the error continues, you'll have to post the whole code or we can't help.
Lyqyd #9
Posted 05 August 2012 - 06:23 PM
I think this line is the problem:

myNetInfo = {dest="3.2", toPort=80, port=port, callback = myCallback}
The keys should be strings, like this:

myNetInfo = { ["dest"] = "3.2", ["toPort"] = 80, ["port"] = port, ["callback"] = myCallback }
If the error continues, you'll have to post the whole code or we can't help.

Nah, you don't have to make them strings explicitly. They're already treated as string names, just like any other declaration. See Table Constructors.
MysticT #10
Posted 05 August 2012 - 09:34 PM
I think this line is the problem:

myNetInfo = {dest="3.2", toPort=80, port=port, callback = myCallback}
The keys should be strings, like this:

myNetInfo = { ["dest"] = "3.2", ["toPort"] = 80, ["port"] = port, ["callback"] = myCallback }
If the error continues, you'll have to post the whole code or we can't help.

Nah, you don't have to make them strings explicitly. They're already treated as string names, just like any other declaration. See Table Constructors.
Yeah, I thought so. I wasn't sure, so I thought it could be the problem. Thanks for the correction.

After looking at the code again, I think I found the problem. The openConnection function is defined as local, so it will be visible to anything after it. The init function takes it as nil, since it's not defined yet (when that function is defined).