Error with displaying localstorage content

Uncaught TypeError: Cannot read property '0' of null

var sorted_high_scores = get_saved_data();
var table = document.getElementById("highscores");

for (var i = 0; i < 10; i++) {
    var item = sorted_high_scores[i];
    var row = document.createElement("tr");
    var placeCell = document.createElement("td");
    var dayCell = document.createElement("td");
    var scoreCell = document.createElement("td");

    placeCell.appendChild(document.createTextNode(i+1));
    dayCell.appendChild(document.createTextNode(ms_to_seconds(item.time).toString(10)));
    scoreCell.appendChild(document.createTextNode(item.day));

    row.appendChild(placeCell);
    row.appendChild(dayCell);
    row.appendChild(scoreCell);
    table.appendChild(row);
}

Been having this problem for a little while, help is appreciated.

Posted May 17, 2017, 11:04 AM UTC

213
zappsg•

what's get_saved_data doing?

1
FakeAdminAccountOP•
return JSON.parse(localStorage.getItem("high_scores"));
1
zappsg•

and the value of the sorted_high_scores variable? or of localStorage.getItem("high_scores")

1
FakeAdminAccountOP•
    var high_scores = get_saved_data() || [];

    high_scores.push({"time": final_score, "day": day});

    var sorted_high_scores = high_scores.sort(sort_scores);
1
zappsg•

do you stringify the value that you put into the localStorage?

1
adavidmiller•

What's the value of your high scores the first time through?

This code assumes that you've retrieved an array of scores. If you haven't previously saved any scores, that's not going to be true.

If you have saved the scores prior to this, have you confirmed that they're in the format you intended? Regardless the problem is pretty clear in that sorted_high_scores is null when you start the loop, which likely means something is off with how you're setting the data.

1
FakeAdminAccountOP•

at the very beginning?

export let start_score = 0;



export const setStart = function (score) {
    start_score = score;
};
1
adavidmiller•

I don't know what that's relating to. I'm saying, your code assumes you're getting an array out of local storage. You aren't.

So, start with manually inspecting your local storage (application tab in the chrome dev tools) and seeing if what you want is even there.

1
FakeAdminAccountOP•

Yeah what I want is exactly there, but I get this error when the localstorage is completely empty.

1
adavidmiller•

Of course you do. If nothing is there, all you have is null. Then, you try to loop through it anyways, what would you expect to happen?

Decide what you want to display in you high scores when it's empty and go from there. Check if it's null, if so, do something else.

2
TheOneRavenous•

I haven't seen any code that actually stores the score into storage. I see retrival code from storage. But if you haven't initialized it then it'll be null. Null values won't exist as an array. You need to initialize the local storage with zero. Because even if you initialize with an empty array the array values length is zero. If the array length is zero than position [0] will also be null. So you have to make sure it's initialized with a zero in the beginning.

OR when you check to see if the local storage exists have a statement that checks if the value is "null". That must be done before your for loop because it allow you to escape the error. (i.e. give you an opportunity to mitigate a known error).

1
FakeAdminAccountOP•

OR when you check to see if the local storage exists have a statement that checks if the value is "null"

That did it, I checked it if was undefined and that did the trick, thanks

1