Android : error in javascript loop

on Friday, March 20, 2015


hi I am developing an android app using cordova in which I am fetching contacts of my android phone and trying to store in sqllite database. So I am using a loop to store contacts into database,while executing that loop an error is coming that "Cannot read property '0' of null" .I am fetching only displayname,emailid,phoneno from my contacts using org.apache.cordova.contacts plugin. I have inserted all the check conditions for it but still getting error


my loop code is shown below



{

var phoneno = "";
var displayname="";
var email = "";
if (contact[i] != "")
{


if (typeof contact.displayName !== 'undefined'){
if (contact[i].displayName != null){
displayname = contact[i].displayName;
}
}
if (typeof contact[i].phoneNumbers !== 'undefined')
{
//alert()
if (contact[i].phoneNumbers != null && contact[i].phoneNumbers.length > 0)
{

if (typeof contact[i].phoneNumbers[j].value !== 'undefined')
{
var ph = contact[i].phoneNumbers;
for(var j=0;j<ph.length;i++)
{
if (contact[i].phoneNumbers[j].value != null)
{

//alert(contact[i].phoneNumbers[j].value);
phoneno = phoneno+""+contact[i].phoneNumbers[j].value+":::";
}
}
}

}
}


if(typeof contact[i].emails != 'undefined')
{
//alert(typeof contact[i].emails);
if(contact[i].emails != null && contact[i].emails.length > 0)
{
//alert(contact[i].emails);
var em = contact[i].emails;

for(var j=0;j<em.length;i++)
{
//alert(typeof contact[i].emails[j].value);
if(typeof contact[i].emails[j].value != 'undefined')
{
if(contact[i].emails[j].value != null )
{
email = email+""+contact[i].emails[j].value+":::";
}
}

}

}

}

//----------------------inserting into sql lite table-------------------//

database.transaction(NewEntry,errorReg,successReg);
function NewEntry(tx)
{

tx.executeSql("INSERT INTO contacts (contactid,displayname,phoneno,email) values ('"+ contact[i].id +"' ,'"+ displayname +"' , '"+ phoneno +"' , '"+ email +"');");

}
function errorReg(error)
{
navigator.notification.alert(error,null,"Got an Error","OK");
}
function successReg(){
localStorage.setItem('phonebook', JSON.stringify(contact));
navigator.notification.alert("Student is Registered Successfully",null,"Information","OK");
}

//----------------------inserting into sqllite end-------------------//




}


}
}


Thankyou.


0 comments:

Post a Comment