Database Problems (results != null) doesn't work
I'm having a problem with an Air database when there's no data in the table. The "if (result2 != null) {" statement always evaluates to true and then when I ask for the length on the next line I get the null object reference. I would think the if statement would be false because there was nothing to search and therefore nothing was found. All my problems go away once there's some data in "result2". What am I doing wrong? This result!=null code is the code I see in the database literature.
This is what I get when I run this (making sure there's no db file...i just keep deleting it):
[SWF] databaseTrial01forForum.swf - 35852 bytes after decompression
C:\Users\Jeff\AppData\Roaming\databaseTrial01forForum\Local Store\DBSample.db
Error message: Error #1009: Cannot access a property or method of a null object reference.
Details: undefined
Test Movie terminated.
import flash.data.SQLConnection;
import flash.data.SQLResult;
import flash.data.SQLStatement;
import flash.errors.SQLError;
import flash.events.SQLErrorEvent;
import flash.events.SQLEvent;
import flash.filesystem.File;
var folder:File = File.applicationStorageDirectory;
var dbFile:File = folder.resolvePath("DBSample.db");
trace(dbFile.nativePath);
var conn:SQLConnection = new SQLConnection();
openDataBase();
function openDataBase():void
{
conn.open(dbFile);
var createStmt:SQLStatement = new SQLStatement();
createStmt.sqlConnection = conn;
var sql:String =
"CREATE TABLE IF NOT EXISTS datetable (" +
" ID INTEGER PRIMARY KEY AUTOINCREMENT, " +
" date TEXT " +
")";
createStmt.text = sql;
try{
createStmt.execute();
var findTodaysDateInDB:SQLStatement = new SQLStatement();
findTodaysDateInDB.sqlConnection = conn;
findTodaysDateInDB.text = "SELECT * FROM datetable WHERE date = ?";
findTodaysDateInDB.parameters[0] = "Jeff";
findTodaysDateInDB.execute();
var result2:SQLResult = findTodaysDateInDB.getResult();
if (result2 != null) {
var numResult2:Number = result2.data.length;
}
} catch (error)
{
trace("Error message:", error.message);
trace("Details:", error.details);
}
conn.close();
}
