开发者

How can I avoid the null object reference error

I have function init, which runs on the creationComplete of the application. The init calls get_login_share_object function, in which objects are created, which are null.

Now my problem is that, I get a null object reference error on开发者_StackOverflow the Alert in "init()". How can I avoid that. Is it possible that I can have a check to see, if the objects are null the program should just skip reading the objects.

private function init():void
        {               
            var stored_credentials:Object = get_login_share_object();
            Alert.show(stored_credentials.check_remember +" "+ stored_credentials.alias +" "+ stored_credentials.password );                
        }

        private function get_login_share_object():Object            
        {               
            //create or retrieve the current shared object
            var so:SharedObject = SharedObject.getLocal("loginData","/");       

            var dataToLoad:ByteArray = so.data.ws_creds;
            if(!dataToLoad)
                return null;

            //read in our key
            var aes_key:String = ServerConfig.aes_key;
            var key:ByteArray = new ByteArray();
            key = Base64.decodeToByteArray(aes_key);                 

            //read in our encryptedText
            var encryptedBytes:ByteArray = new ByteArray();
            dataToLoad.readBytes(encryptedBytes);

            //decrypt using 256b AES encryption
            var aes:ICipher = Crypto.getCipher("simple-aes128-ctr", key, Crypto.getPad("pkcs5"));
            aes.decrypt(encryptedBytes);

            encryptedBytes.position = 0;

            var obj:Object = new Object();
            obj.alias = encryptedBytes.readUTF();               
            obj.password = encryptedBytes.readUTF();
            obj.check_remember = encryptedBytes.readUTF();                              

            return obj;
        }


You could check for the null like this:

var stored_credentials:Object = get_login_share_object();
if (stored_credentials)
    Alert.show(stored_credentials.check_remember +" "+ stored_credentials.alias +" "+ stored_credentials.password );
else
    trace('No Shared Object');


You should find out why those values are null and fix that first. Generally speaking, if you are expecting a value, it should not be null.

If it really is expected that some of those values are null then yes, you can check them first in two ways:

if(value != null) value.doSomething();

or

try{
    Alert.show(stored_credentials.check_remember +" "+ stored_credentials.alias +" "+ stored_credentials.password );
}
catch(e:Error){
    // do something else here if the statement under the try failed.
    // most likely log the error message and see what it is
}


Your problem is here:

var dataToLoad:ByteArray = so.data.ws_creds;
if(!dataToLoad)
    return null;

If there isn't any data to load, you're returning a null. So when you try and access the returned object's properties later, you'll get the null object reference error because you're referencing a null object. :)

There are a couple of easy solutions to this. You can check if the return value is null before you try to reference any properties like so:

if (stored_credentials != null) {
    Alert.show(stored_credentials.check_remember +" "+ stored_credentials.alias +" "+ stored_credentials.password );
}

Or you can stop returning a null from your get_login_share_object function. What you return instead is totally up to you, just make sure it returns an object with all the properties you're referencing.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜