John Cooper
John Cooper

Reputation: 7631

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

Upvotes: 0

Views: 10952

Answers (2)

Fredrik
Fredrik

Reputation: 2016

This works for me:

console.debug(Obj.getItemRow("AK"));

Upvotes: 1

Stefan Kendall
Stefan Kendall

Reputation: 67802

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'

Upvotes: 2

Related Questions