admin管理员组

文章数量:1405339

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 = {

    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()

Share asked Jun 9, 2011 at 20:05 John CooperJohn Cooper 7,67333 gold badges83 silver badges102 bronze badges 4
  • 2 I dont get any errors running it. – Petah Commented Jun 9, 2011 at 20:07
  • @Petah: Is it because, i am including this js file in another class... which is causing this issue. when i include this file, will the function get called without using it... ??? – John Cooper Commented Jun 9, 2011 at 20:08
  • 1 Well give us a plete code sample – Petah Commented Jun 9, 2011 at 20:09
  • Isn't Obj.getItemRow(itemValue) the same as Obj.StateValues.indexOf(itemValue)? – Aadit M Shah Commented Nov 24, 2011 at 20:48
Add a ment  | 

2 Answers 2

Reset to default 2
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"));

本文标签: Null Pointer exception in JavaScriptStack Overflow