title: In javascript, after i pass a non null object to a funtion it says the object is null
body: <p>i was trying to implement the A* algorithm and followed the wikipedia pseudo code to make this. </p>
  
  <p>when i pass a predefined object pixel to the a funtion getG() it says that the object is null. I'm sorry if i am not pointing to a specific problem but i am not even sure how to really specify the problem by name. i have tried commenting out the code to increase readability. </p>
  
  <p>git repository link of the whole project - <a href="https://github.com/NirobNabil/WhirlWind" rel="nofollow noreferrer">https://github.com/NirobNabil/WhirlWind</a></p>
  
  <p>(things are a little messy here because i didn't use github at first and i uploaded it just a little ago for posting the problem)</p>
  
  <p>[ i'm actually making this to use a* to find path for my bot which is powered by arduino. thats why i'm using involt. ]</p>
  
  <p>here goes the code,</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>$(function() {
    // define the height, width and bot size in centemeter
    total_width = 200;
    total_height = 200;
    bot_size = 20;
    total_box = (total_height / bot_size) * (total_width / bot_size);
    box_in_x = total_width / bot_size;
    box_in_y = total_height / bot_size;
    //populating the pixels array
    populate(total_width / bot_size, total_height / bot_size, "UND");
    pathfind(pixels, pixels[13], pixels[pixels.length - 1]);
  })
  
  var pixels = []; //an array to hold all the objects(the grid) 
  
  var obstacles = []; //array to hold the obstacles
  
  function pixel(X, Y, obs) {
    this.X_co_ordinate = X;
    this.Y_co_ordinate = Y;
    this.state = obs; //availale states OPN, UND, OBS, DIS, NULL
    this.g = 0;
    this.h = 0;
    this.f = 0;
    this.last = null;
  } //every block in the grid is a pixel
  
  //01719372596
  
  function populate(height, width, obs_val = "UND") {
  
    pixels[0] = new pixel(0, 10, obs_val);
  
    for (h = height, i = 0; h &gt;= 0; h--) {
      for (w = 0; w &lt; width; w++, i++) {
        var temp_obs = new pixel(w, h, obs_val);
        temp_obs.last = pixels[0];
        pixels[i] = temp_obs; //saving temp_pixel object to pixels array
      }
    }
  
  } //populating the grid AKA pixels with pixel objects or blocks
  
  // this funtion is where the problem shows up
  function getG(current, start) {
    let g = 1;
    while (current != start &amp;&amp; current.last != start &amp;&amp; current) {
      current = current.last;
      g++;
    }
  
    return g;
  } //get the g val(cost to come to this pixel from the start) of the current pixel
  
  
  function getH(current, end) {
    let I = Math.abs(current.X_co_ordinate - end.X_co_ordinate) + Math.abs(current.Y_co_ordinate - end.Y_co_ordinate);
    return I;
  } //get the h val(heuristic) of the current pixel
  
  function getF(start, current, end) {
    let G = getG(current, start);
    let H = getH(current, end);
    return G + H;
  } //get the f val(total) of the current pixel
  
  function lowFinArray(arr, start, end) {
    // here arr is the grid/pixel
    let current_low = arr[0];
    for (let i = 0; i &lt; arr.length; i++) {
      let getF1 = getF(start, current_low, end);
      let getF2 = getF(start, arr[i], end);
      if (getF1 &lt; getF2) {
        current_low = arr[i];
      }
    }
  
    console.log("current low");
    console.log(current_low);
  
    return current_low;
  }
  
  function getneighbours(grid, current) {
  
    let neightbours = [];
  
    neightbours.push(grid[getIndex(current.X_co_ordinate - 1, current.Y_co_ordinate)]);
    neightbours.push(grid[getIndex(current.X_co_ordinate + 1, current.Y_co_ordinate)]);
    neightbours.push(grid[getIndex(current.X_co_ordinate, current.Y_co_ordinate - 1)]);
    neightbours.push(grid[getIndex(current.X_co_ordinate, current.Y_co_ordinate + 1)]);
  
    /*
    for(i=0; i&lt;neightbours.length; i++){
        neightbours[i].last = current;
    }*/
    console.log("neightbours");
    console.log(neightbours);
    return neightbours;
  } //get the neighbour pixels of the current pixel
  
  
  
  //main algo
  function pathfind(grid, start, end) {
  
    let closedSet = [];
    let openSet = [];
    openSet.push(start);
    let current = start;
  
    //trying to debug
    console.log("low F in arr");
    console.log(lowFinArray(grid, start, end));
    console.log(start);
    console.log(current);
    console.log(end);
    console.log(grid);
  
  
    let x = 0;
    while (openSet.length &gt; 0) {
  
      //trying to debug
      console.log("executed " + (x++));
      console.log("openset");
      console.log(openSet);
  
  
      current = lowFinArray(grid, start, end); //assigning the pixel with lowest f val to current
      console.log("current");
      console.log(current);
  
      if (current == end) {
  
        console.log(getPath(current));
      }
  
      let neighbours = getneighbours(grid, current);
      for (let i = 0; i &lt; neighbours.length; i++) {
  
        let neighbour = neighbours[i];
  
        if (closedSet.includes(neighbour)) {
          continue;
        }
  
        if (!openSet.includes(neighbours)) {
          openSet.push(neighbours);
        }
  
        //console.log(current);
        let getg = getG(current, start);
        let geth = getH(current, end);
        //console.log(getg);
        let tGscore = getg + geth; //here getH is being used as a distance funtion
  
        if (tGscore &gt;= getg) {
          continue;
        }
  
        neighbour.last = current;
        neighbour.g = tGscore;
        neighbour.f = getF(neighbour);
  
      }
      if (x &gt; 10) {
        return 0;
      }; //the loop was running forever so i tried this to stop the loop after 10 iterations
    }
  
  }
  
  function getPath(current) {
  
    let path = [current];
    while (current.last != null) {
      path.push(current.last);
    }
  
    return path;
  }</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"&gt;&lt;/script&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>and here is what the console says,</p>
  
  <pre><code>Uncaught TypeError: Cannot read property 'last' of null
      at getG (app.js:226)
      at getF (app.js:241)
      at lowFinArray (app.js:249)
      at pathfind (app.js:292)
      at HTMLDocument.&lt;anonymous&gt; (app.js:92)
      at mightThrow (jquery-3.1.1.js:3570)
      at process (jquery-3.1.1.js:3638)
  </code></pre>