フラッシュで sqlite を使用していますが、次のエラーが発生します: エラー #1009: null オブジェクト参照のプロパティまたはメソッドにアクセスできません。私はそれをデバッグしました、そしてこれはそれが86行目と100行目にあると言います:
stat.text = "UPDATE contacts SET fname=@fname, lname=@lname, phone=@phone WHERE id=" + itemList.selectedItem.data.id;
と
stat.text = "DELETE FROM contacts WHERE id=" + itemList.selectedItem.data.id;
コード全体:
import flash.filesystem.File;
import flash.data.SQLConnection;
import flash.data.SQLStatement;
import flash.net.Responder;
import flash.data.SQLResult;
import fl.data.DataProvider;
import flash.data.SQLMode;
import flash.errors.SQLError;
import flash.events.SQLErrorEvent;
import flash.events.SQLEvent;
var connection:SQLConnection;
openDatabase();
t_phone.restrict = "0-9";
function openDatabase():void
{
var dbFile:File = File.applicationStorageDirectory.resolvePath("database.db");
connection = new SQLConnection();
connection.addEventListener(SQLEvent.OPEN, onOpen);
connection.openAsync(dbFile, SQLMode.CREATE);
}
function onOpen(SQLEvent):void
{
var stat:SQLStatement = new SQLStatement();
stat.sqlConnection = connection;
stat.text = "CREATE TABLE IF NOT EXISTS contacts (id INTEGER PRIMARY KEY AUTOINCREMENT, fname TEXT, lname TEXT, phone INTEGER)";
stat.execute(-1, new Responder(selectItems));
}
function selectItems(SQLEvent):void
{
b_save.enabled = false;
b_delete.enabled = false;
var stat:SQLStatement = new SQLStatement();
stat.sqlConnection = connection;
stat.text = "SELECT id, fname, lname, phone FROM contacts ORDER BY id";
stat.execute(-1, new Responder(onSelected));
}
function onSelected(evt:SQLResult):void
{
if (evt.data != null)
{
var itemList:DataProvider = new DataProvider();
for (var i:int=0; i<evt.data.length; i++)
{
itemList.addItem({label:(evt.data[i].fname + " " + evt.data[i].lname), data:evt.data[i]});
}
}
}
b_new.addEventListener(MouseEvent.CLICK, createNew);
function createNew(MouseEvent):void
{
var stat:SQLStatement = new SQLStatement();
stat.sqlConnection = connection;
stat.text = "INSERT INTO contacts (fname, lname, phone) VALUES (@fname, @lname, @phone)";
stat.parameters["@fname"] = t_fname.text;
stat.parameters["@lname"] = t_lname.text;
stat.parameters["@phone"] = t_phone.text;
stat.execute(-1, new Responder(selectItems));
t_fname.text = t_lname.text = t_phone.text = "";
}
itemList.addEventListener(Event.CHANGE, onChange);
function onChange(evt:Event):void
{
b_save.enabled = true;
b_delete.enabled = true;
t_fname.text = evt.target.selectedItem.data.fname;
t_lname.text = evt.target.selectedItem.data.lname;
t_phone.text = evt.target.selectedItem.data.phone;
}
b_save.addEventListener(MouseEvent.CLICK, saveThis);
function saveThis(MouseEvent):void
{
var stat:SQLStatement = new SQLStatement();
stat.sqlConnection = connection;
stat.text = "UPDATE contacts SET fname=@fname, lname=@lname, phone=@phone WHERE id=" + itemList.selectedItem.data.id;
stat.parameters["@fname"] = t_fname.text;
stat.parameters["@lname"] = t_lname.text;
stat.parameters["@phone"] = t_phone.text;
stat.execute(-1, new Responder(selectItems));
t_fname.text = t_lname.text = t_phone.text = "";
}
b_delete.addEventListener(MouseEvent.CLICK, deleteThis);
function deleteThis(MouseEvent):void
{
var stat:SQLStatement = new SQLStatement();
stat.sqlConnection = connection;
stat.text = "DELETE FROM contacts WHERE id=" + itemList.selectedItem.data.id;
stat.execute(-1, new Responder(selectItems));
t_fname.text = t_lname.text = t_phone.text = "";
}