我正在尝试连接到位于项目的assets文件夹中的SQLite数据库。 我写了一个类,这是抛出错误的部分:
public class Database
{
private var sqlConnection:SQLConnection;
public function connect(db:String):Object
{
var response:Object={status:false,message:''};
try
{
var dbFile:File=File.applicationDirectory.resolvePath(db);
sqlConnection.open(dbFile);
response.status=true;
response.message='';
}
catch(error:SQLError)
{
response.status=false
response.message=error.message;
}
return response;
}
}
我在creationComplete
处理程序上的MXML视图组件中调用该类:
private function init():void
{
var db:Database=new Database();
var connectResponse:Object=db.connect('assets/data.db');
if(connectResponse.status)
{
//getData() runs a simple select query and returns an array
acData=new ArrayCollection(db.getData());
}
else
{
//If the status is false I show a label control for debugging
labelError.text=connectResponse.message;
labelError.includeInLayout=true;
labelError.visible=true;
}
list.dataProvider=acData;
}
我得到的错误就是:TypeError: Error #1009: Cannot access a property or method of a null object reference.
。
答案 0 :(得分:1)
我认为您需要在使用之前实例化sqlConnection:
sqlConnection = new SQLConnection();
sqlConnection.open(dbFile);