Question

I have an array list which is connected with a private static function and would like to make another array list on a different view (Flex builder, Action script) so I copied the private static function edited the name and the "Select" parts but I get an error saying:

"1061: call to a possibly undefined method members through a reference with static type class."

Here is the code of the AS:

package model
{
    import flash.data.SQLConnection;
    import flash.data.SQLResult;
    import flash.data.SQLStatement;
    import flash.events.SQLEvent;
    import flash.filesystem.File;
    import flash.filesystem.FileMode;
    import flash.filesystem.FileStream;

    import model.Dish;

    import mx.collections.ArrayCollection;

    public class SQLiteDatabase
    {
        private static var _sqlConnection:SQLConnection;

        public static function get sqlConnection():SQLConnection
        {
            if (_sqlConnection)
                return _sqlConnection;
            openDatabase(File.desktopDirectory.resolvePath("test.db"));
            return _sqlConnection;
        }

        public  function getNote(id:int):Dish
        {
            var sql:String = "SELECT id, title, time, message FROM notes WHERE id=?";
            var stmt:SQLStatement = new SQLStatement();
            stmt.sqlConnection = sqlConnection;
            stmt.text = sql;
            stmt.parameters[0] = id;
            stmt.execute();
            var result:Array = stmt.getResult().data;
            if (result && result.length == 1)
                return processRow(result[0]);
            else
                return null;
        }
        public function getmember(id:int):Dish
        {
            var sql:String = "SELECT id, title, time, message FROM notes WHERE id=?";
            var stmt:SQLStatement = new SQLStatement();
            stmt.sqlConnection = sqlConnection;
            stmt.text = sql;
            stmt.parameters[0] = id;
            stmt.execute();
            var result:Array = stmt.getResult().data;
            if (result && result.length == 1)
                return processRow(result[0]);
            else
                return null;
        }

        public function getMYBlist(id:int):Dish
        {
            var sql:String = "SELECT id, title, time, message FROM notes WHERE id=?";
            var stmt:SQLStatement = new SQLStatement();
            stmt.sqlConnection = sqlConnection;
            stmt.text = sql;
            stmt.parameters[0] = id;
            stmt.execute();
            var result:Array = stmt.getResult().data;
            if (result && result.length == 1)
                return processRow(result[0]);
            else
                return null;
        }

        public static function notes():ArrayCollection
        {
            var noteList:ArrayCollection = new ArrayCollection();

            var sql:String = "SELECT id, title, time, message FROM notes";
            var stmt:SQLStatement = new SQLStatement();
            stmt.sqlConnection = sqlConnection;
            stmt.text = sql;
            stmt.execute();
            var sqlResult:SQLResult = stmt.getResult();
            if (sqlResult) {
                var result:Array = sqlResult.data;
                if (result) {
                    for (var index:Number = 0; index < result.length; index++) {
                        noteList.addItem(processRow(result[index]));
                    }
                }
            }
            return noteList;
        }

        public static function members():ArrayCollection
        {
            var memberslist:ArrayCollection = new ArrayCollection();

            var sql:String = "SELECT id, name FROM members";
            var stmt:SQLStatement = new SQLStatement();
            stmt.sqlConnection = sqlConnection;
            stmt.text = sql;
            stmt.execute();
            var sqlResult:SQLResult = stmt.getResult();
            if (sqlResult) {
                var result:Array = sqlResult.data;
                if (result) {
                    for (var index:Number = 0; index < result.length; index++) {
                        memberslist.addItem(processRow(result[index]));
                    }
                }
            }
            return memberslist;
        }




        public static function addNote(note:Dish):void
        {
            var sql:String = 
                "INSERT INTO notes (title, time, message) " +
                "VALUES (?,?,?)";
            var stmt:SQLStatement = new SQLStatement();
            stmt.sqlConnection = sqlConnection;
            stmt.text = sql;
            stmt.parameters[0] = note.title;
            stmt.parameters[1] = note.time;
            stmt.parameters[2] = note.message;
            stmt.execute();
        }

        public static function addMember(note:Dish):void
        {
            var sql:String = 
                "INSERT INTO notes (title, time, message) " +
                "VALUES (?,?,?)";
            var stmt:SQLStatement = new SQLStatement();
            stmt.sqlConnection = sqlConnection;
            stmt.text = sql;
            stmt.parameters[0] = note.title;
            stmt.parameters[1] = note.time;
            stmt.parameters[2] = note.message;
            stmt.execute();
        }

        public static function deleteNote(note:Dish):void
        {
            var sql:String = "DELETE FROM notes WHERE id=?";
            var stmt:SQLStatement = new SQLStatement();
            stmt.sqlConnection = sqlConnection;
            stmt.text = sql;
            stmt.parameters[0] = note.id;
            stmt.execute();
        }



        public static function updateNote(note:Dish):void
        {
            var sql:String = "UPDATE notes SET title=?, time=?, message=? WHERE id=?";
            var stmt:SQLStatement = new SQLStatement();
            stmt.sqlConnection = sqlConnection;
            stmt.text = sql;
            stmt.parameters[0] = note.title;
            stmt.parameters[1] = note.time;
            stmt.parameters[2] = note.message;
            stmt.parameters[3] = note.id;
            stmt.execute();
        }



        protected static function processRow(o:Object):Dish
        {
            var note:Dish = new Dish();
            note.id = o.id;
            note.title = o.title == null ? "" : o.title;
            note.time = o.time == null ? "" :o.time;
            note.message = o.message == null ? "" : o.message;
            return note;
        }

        public static function openDatabase(file:File):void
        {
            var newDB:Boolean = true;
            if (file.exists)
                newDB = false;
            _sqlConnection = new SQLConnection();
            _sqlConnection.open(file);
            if (newDB)
            {
                createDatabase();
                populateDatabase();
            }
        }

        protected static function createDatabase():void
        {
            var sql:String = 
                "CREATE TABLE IF NOT EXISTS notes ( "+
                "id INTEGER PRIMARY KEY AUTOINCREMENT, " +
                "title VARCHAR(50), " +
                "time VARCHAR(50), " +
                "message VARCHAR(200))";
            var stmt:SQLStatement = new SQLStatement();
            stmt.sqlConnection = sqlConnection;
            stmt.text = sql;
            stmt.execute();         
        }

        protected static function populateDatabase():void
        {
            var file:File = File.applicationDirectory.resolvePath("assets/notes.xml");
            if (!file.exists) return;
            var stream:FileStream = new FileStream();
            stream.open(file, FileMode.READ);
            var xml:XML = XML(stream.readUTFBytes(stream.bytesAvailable));
            stream.close();
            for each (var n:XML in xml.note)
            {
                var note:Dish = new Dish();
                note.id = n.id;
                note.title = n.title;
                note.time = n.time;
                note.message = n.message;
                addNote(note);
            }
        }

    }
}

and the code of the MXML file:

<?xml version="1.0" encoding="utf-8"?>
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009" 
        xmlns:s="library://ns.adobe.com/flex/spark" title="Profile">
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>
    <fx:Script>
        <![CDATA[
            import model.Dish;
            import model.SQLiteDatabase;

            import spark.events.IndexChangeEvent;

            protected function onNote2Selected(event:IndexChangeEvent):void {
                var selectedNote:Dish = event.currentTarget.dataProvider[event.newIndex];
                navigator.pushView(MemberDetailsView, selectedNote);
            }


            protected function onAddButtonClicked(event:MouseEvent):void {
                navigator.pushView(AddMemberView);
            }

        ]]>
    </fx:Script>


    <s:VGroup gap="-1">
    </s:VGroup>
    **<s:List dataProvider="{SQLiteDatabase.members()}"  change="onNoteSelected(event)"**
            left="0" right="0" top="0" bottom="0">

        <s:itemRenderer>
            <fx:Component>
                <s:IconItemRenderer labelField="title" messageField="message"/>
            </fx:Component>
        </s:itemRenderer>
    </s:List>
</s:View>
Was it helpful?

Solution

The error is because you are accessing a static method to a non-static class. The meaning is that the instance has not been created yet, so while the linker should be able to locate the static member function, there is no way to resolve which instance of the object. If you declare the class static, it should solve this problem.

OTHER TIPS

public static function notes():ArrayCollection
        {
            var noteList:ArrayCollection = new ArrayCollection();

            var sql:String = "SELECT id, title, time, message FROM notes";
            var stmt:SQLStatement = new SQLStatement();
            stmt.sqlConnection = sqlConnection;
            stmt.text = sql;
            stmt.execute();
            var sqlResult:SQLResult = stmt.getResult();
            if (sqlResult) {
                var result:Array = sqlResult.data;
                if (result) {
                    for (var index:Number = 0; index < result.length; index++) {
                        noteList.addItem(processRow(result[index]));
                    }
                }
            }
            return noteList;
        }

        public static function members():ArrayCollection
        {
            var noteList:ArrayCollection = new ArrayCollection();

            var sql:String = "SELECT testid, Dish_Name FROM DishInventory";
            var stmt:SQLStatement = new SQLStatement();
            stmt.sqlConnection = sqlConnection;
            stmt.text = sql;
            stmt.execute();
            var sqlResult:SQLResult = stmt.getResult();
            if (sqlResult) {
                var result:Array = sqlResult.data;
                if (result) {
                    for (var index:Number = 0; index < result.length; index++) {
                        noteList.addItem(processRow2(result[index]));
                    }
                }
            }
            return noteList;
        }
protected static function processRow(o:Object):Dish
        {
            var note:Dish = new Dish();
            note.id = o.id;
            note.title = o.title == null ? "" : o.title;
            note.time = o.time == null ? "" :o.time;
            note.message = o.message == null ? "" : o.message;
            return note;
        }

        protected static function processRow2(o:Object):Dish
        {
            var note:Dish = new Dish();
            note.id = o.testID;
            note.title = o.Dish_Name == null ? "" : o.Dish_Name;

            return note;
        }

Still got to clean my coding though

The solution is:

private function sqlResult(res:SQLEvent):void {

var sqlResult:SQLResult = stmt.getResult();

if (sqlResult) {        
    var data:Array = sqlResult.data;

    contactList = new ArrayCollection();

    for each(var item:Object in data){
        contact = new Contact(item);
        contactList.addItem(contact);
    }
}

}

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top