PDA

View Full Version : Trying to find a bug in my code



Desolator
06-08-2007, 04:00 PM
I'm creating a database written in a XML file. Now I'm testing what I've done so far, and I get a "table index is nil" error in the following function.

I can't seem to find the error in it, can someone help, please? Here's the code:


function xdbOpenDB(strXdbPath)
-- if there's no database open
if tblXDB == nil then
-- set the number of open databases to zero
local numOpenXDBs = 0;

-- create the database table
tblXDB = {};

-- set the index of the database
local numXdbHandle = 1;
else
-- count the number of open databases
local numOpenXDBs = Table.Count(tblXDB);

-- if the number of open databases is zero
if numOpenXDBs == 0 then
-- create the database table
tblXDB = {};
end

-- set the index of the database
local numXdbHandle = numOpenXDBs + 1;
end

-- add the database path to the table
tblXDB[numXdbHandle] = strXdbPath;

-- return the database index
return numXdbHandle;
end

I attached the project.

Desolator
06-08-2007, 04:22 PM
I updated the function, but still the same error, the table doesn't get any item:


function xdbOpenDB(strXdbPath)
-- if there's no database open
if tblXDB == nil then
-- set the number of open databases to zero
local numOpenXDBs = 0;

-- create the database table
tblXDB = {};

-- set the index of the database
local numXdbHandle = 1;
else
-- count the number of open databases
local numOpenXDBs = Table.Count(tblXDB);

-- if the number of open databases is zero
if numOpenXDBs == 0 then
-- create the database table
tblXDB = {};

-- set the index of the database
local numXdbHandle = 1;
else
-- set the index of the database
local numXdbHandle = numOpenXDBs + 1;
end
end

-- add the database path to the table
Table.Insert(tblXDB, numXdbHandle, strXdbPath);

-- return the database index
return numXdbHandle;
end

Dermot
06-08-2007, 05:46 PM
Remove the local from local numXdbHandle = 1 and local numXdbHandle = numOpenXDBs + 1

When you use local inside an if statement, the variable will not be available outside the if statement.

Desolator
06-09-2007, 12:10 AM
:eek: Thank you! I should have declarde it as local at the beginning of the function...