Null Pointer exception in JavaScript
var Obj = {
StateValues: ['AL','AK','AL','AK','AZ','AR','CA','CO','CT','DE','FL','GA','HI','ID','IL','IN','IA',
'KS','KY','LA','开发者_如何学运维ME','MD','MA','MI','MN','MS','MO','MT','NE','NV','NH','NJ','NM','NY','NC','ND',
'OH','OK','OR','PA','RI','SC','SD','TN','TX','UT','VT','VA','WA','WV','WI','WY'],
getItemRow: function(itemValue) {
var myPosition=-1
for (var i=0;i<Obj.StateValues.length;i++) {
if(Obj.StateValues[i]==itemValue) {
myPosition = i;
break;
}
}
return myPosition;
}
}
When i add the function in the code, i get Null Pointer Expection
. This piece of code is in a sep file... somename.js and which i include
I am not even using this function anywhere in my other js file... like Obj.getItemRow()
var Obj = new function(){
var StateValues = ['AL','AK','AL','AK','AZ','AR','CA','CO','CT','DE','FL','GA','HI','ID','IL','IN','IA',
'KS','KY','LA','ME','MD','MA','MI','MN','MS','MO','MT','NE','NV','NH','NJ','NM','NY','NC','ND',
'OH','OK','OR','PA','RI','SC','SD','TN','TX','UT','VT','VA','WA','WV','WI','WY'];
this.getItemRow = function(itemValue) {
var myPosition=-1
for (var i=0;i<StateValues.length;i++) {
if(StateValues[i]==itemValue) {
myPosition = i;
break;
}
}
return myPosition;
};
}
This is an easier way to create objects.
var blah = 'this is private'
this.blah = 'this is public'
This works for me:
console.debug(Obj.getItemRow("AK"));
精彩评论