0

I created a script that is supposed to go to each individual link, check how much it is selling for, and if it is selling for equal to or under the price I typed in, it will say it on the title string. For whatever reason, when executing it on Chrome, the following error pops up 95% of the time: Uncaught TypeError: Cannot read property '1' of null

JShint doesn't help either, because no matter what I change, it always says the same error. If it helps, I think the error is around the regex line.

The script:

var IDs = [136758649, 116770724, 136171998, 113325603, 111903483, 116786719, 136172656, 139152118, 121926643, 120759721] //Shortened down to a few IDs
var PriceWanting = Number(prompt("How much is the max you would do?"))
document.write('<p id = "title">Total number bought: 0 items for a total of 0</p>')
var buys = 0
var totalrobuxspent = 0
console.log("Bot started")
var loop = setInterval(function () {
    for (var i = 0; i < IDs.length; i++) {
        $.get(" http://m.roblox.com/items/" + IDs[i] + "/privatesales", function (data) {
            var Regex = /\<span class="currency-robux">([\d,]+)\<\/span\>/
            var PriceSelling = data.match(Regex)[1]
            PriceSelling = Number(PriceSelling.replace(",", ""))
            PriceSelling = PriceSelling * 1
            totalrobuxspent = totalrobuxspent + PriceSelling
            var remaining = PriceWanting - PriceSelling
            if (remaining >= -0.1) {
                buys = buys + 1
                document.getElementById("title").innerHTML = "Total number of items bought: " + buys + " for a total of " + totalrobuxspent
            }
        })
    }
}, 3636) //Long intervals so it doesn't lag too badly; mainly for me testing
random_user_name
  • 25,694
  • 7
  • 76
  • 115
  • 2
    Where are the `;`s... (I know they are not required but it's a good habit to use them) – Derek 朕會功夫 Feb 22 '14 at 21:30
  • [`String#match` returns *an `Array` or **`null`** if there were no matches.*](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/match#Return_value). Check if it returned a null before you read `[1]` from it. – DCoder Feb 22 '14 at 21:30
  • 1
    DCoder you should make that a full answer, as I suspect you are correct :) – Collin Grady Feb 22 '14 at 21:32
  • Second thought, I have no clue how to check it for null. Can anyone show me how? In LUA, it would have been if Regex == null then... But I know this is very different. – user3341815 Feb 22 '14 at 21:32
  • your `data.match(Regex)[1]` did not find any matches, hence it is returning null and giving you an error – stackErr Feb 22 '14 at 21:32
  • possible duplicate of [Checking if Null](http://stackoverflow.com/questions/21961009/checking-if-null) – Toothbrush Feb 22 '14 at 22:07

1 Answers1

0

You can check that it's not null like this:

if (!x) {
    // x is not null; do something here
}
Toothbrush
  • 2,080
  • 24
  • 33