Uncaught error: cannot set property 'src' of null | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Uncaught error: cannot set property 'src' of null

what's the problem in 'src'?

16th Mar 2017, 10:41 AM
ZeroOne;
ZeroOne; - avatar
4 Answers
+ 5
Variable randomImg is wrong. Because math.random generate any type of no e.g. 4 and array length is 4, then the execution will be 4*4=16 and 16th val or 15th array val(array start with 0) is not present so it cause an null value as error.
16th Mar 2017, 11:02 AM
Animesh
Animesh - avatar
+ 4
Share code
16th Mar 2017, 10:52 AM
Animesh
Animesh - avatar
0
var myImge = document.getElementById('myImg'), arrImgs = [ "https://placehold.it/100/#fff", "https://placehold.it/100/#f00", "https://placehold.it/100/#00f", "https://placehold.it/100/#ddd" ]; function changeImg(myImge, arrImgs) { 'use strict'; setInterval(function () { var randomImg = Math.floor(Math.random() * arrImgs.length); myImge.src = arrImgs[randomImg]; }, 1000); }; window.onload = changeImg(myImge, arrImgs);
16th Mar 2017, 10:56 AM
ZeroOne;
ZeroOne; - avatar
0
I checked that Code in Netbeans many times ,and it's totaly Correct..but in Sololearn ..I don't what's up here..
16th Mar 2017, 11:09 AM
ZeroOne;
ZeroOne; - avatar