TL;DR;
- You are accessing a property of an object that is null. For example, document.getElementById('stuff') returns null. So adding
.value
will cause the error. - You are trying to access a DOM element before the DOM is ready. Use onload or DOMContentLoaded.
- Test if an object is valid before accessing its property.
There are a few variations of this error depending on the property you are trying to access. Sometimes instead of null
it will say undefined
. An example will be:
Uncaught TypeError: Cannot read property 'value' of null
Uncaught TypeError: Cannot read property 'innerHTML' of null
All this means is that you are trying to access a property of an object that is undefined. These usually happens when we don't test an object before using it. Here is a common scenario.
// We want to get the value of an input.
var inputVal = document.getElementById("input").value;
This will result in Uncaught TypeError: Cannot read property 'value' of null
. The reason will be that the element with id input
does not exist. Let me break it down in simpler steps;
var input = document.getElementById("input");
input // when this fails, it returns null. input = null
var inputVal = input.value;
// this is the same as the following.
var inputVal = null.value;
// null does not have the property 'value'
When you break it down, the error actually makes sense. To make sure that you don't get this error, you have to make sure that btn
, or any object you use, is not null before you use it. For our example:
var input = document.getElementById("btn");
var inputVal = "";
if (input) {
inputVal = input.value;
}
Sometimes, your object is nested deeper like Tree.folder.path
. You just have to make sure that if you need to access folder
, than Tree
has to be defined. And if you need to access path
, then folder needs to be defined.
In some cases, this error is a symptom of another issue. Why would getElementById
return null
if the element actually exists on the page? Probably because the function is called before the DOM is ready. Always be careful when accessing a DOM element before it is ready.
Sign up for the Newsletter.
Comments(48)
Denisander Vivan :
Please, tell me where are de error.
Ibrahim :
You have an error in this line:
Your correct id is
txtvel
but you wrotetxt#vel
. Change it to:You also have an error here
resp.InnerText
. The correct way is with a lowercasei
. Like this:resp.innerText
Also you didn't close the double quotes on your lang attribute in the html tag:
<html lang="pt-br>
should be<html lang="pt-br">
Good luck
Ícaro :
Where's the error? Please Help.
HTML
Javascript
Ibrahim :
The problem is that you haven't debugged your code. You can never find an answer like this. Instead, ask yourself these questions:
These questions will not only help you find the solution, it will also help you get some help.
Shahnila Fahmi :
Cannot read property 'salt' of null at aes.js
kelvin :
please why im i getting this error:
NOTE: i have a css file that corresponds with the animation code i the js file
Ibrahima Diallo :
Hi Kelvin
Looking at your code, I assume that the javascript you wrote is in GNG.js. Right?
The issue is you are running your script before the page is done downloading. So when you call
document.querySelector('.burger');
inside GNG.js, the html element with class burger doesn't exist yet. You have to wait for the DOM (Document object model) to be ready before you can access it. Since you have jQuery on the page, you can do that by wrapping your code in a Jquery Function. Example:Try it out and let me know if it works.
Renan :
Please, where is the error at the following code?
Renan :
Found the bug, thanks! Sorry, once again, for the mess.
Ibrahima Diallo :
No worries @Renan I fixed the formatting. Also I'm glad that you also found a solution.
For anyone else wondering in the future, the error was;
Meaning,the element with id
iii
did not exist. The reason was that Renan had added an HTML comment around that element.Thank you for contributing.
Renan :
Yes, man, actually, the 'main' bug that was bothering me was in call the "popularCombo" function. There, I got the exception by passing the line
because var 'ele' used a 'sel' parameter that didn't exist (the combobox I did had a name = "sel" parameter, not id). I don't know if I make myself clear, but that's what happened! Hehe.
Thanks!
Alan :
It is my code can you tell me where is the error:
Ibrahim Diallo :
Hi Alan. You misspelled
textnum1
,txtnum2
andtextans
. Instead you are missing thee
. and You forgot the=
sign inname"textans"
. it should bename="textans"
.When all this is fixed, you'll realize that your code doesn't work. If you enter the values 1 and 2, the answer will be
12
. The reason is you are adding strings together. You need to change the values to numbers. In your case, use parsefloat. Here is what your code will look like.Good luck.
Schady :
every time i try to run this, it shows up saying "Cannot read property 'id' of null" can somebdody help me please?
I am using visual studio code by the way if that helps
Ibrahima Diallo :
Hi Schady
You checked if
muterole
is valid, but you haven't checked the value ofmainrole
. Just add the following.Note that you have to try to debug your code. Use
console.log(variable)
to find what is currently in your variables.Good Luck.
Schady :
alright i put in the code and im debugging it now. thanks for your help!
Fatima Martinez :
Hi!
I'm new on web developing and i'm very bad on scripts... I got the same error: “Uncaught TypeError: Cannot read property 'submit' of null”... Does anybody knows which is the error?
ibrahim :
Hi Fatima
It looks like the code you posted is not the generated html. Run your code on the browser and check the page source. You can also check on the console (Press F12 to open the console).
Since I cannot run your code, all I can recommend is to take the time to debug. What the error is telling you is that the form with id
frmImport
does not exist. There could be too many reasons for this. But let's rewriteimportarDoc()
like this:I hope this helps.
Frank :
Hello,
I have a problem with mi page, i debugged and got this error: "Uncaught TypeError: Cannot set property 'disabled' of null at ImprimeLista".
Ibrahima Diallo :
Hi @Frank
I redacted your message because it was too much code. The error you are getting is exactly what I have described in the article.
It says it cannot set property
disabled
of null. Where is disabled used? They are the elements with id btConsulta, btDescargar and btHist. In your javascript, they are null because you are calling your script too early, or because the elements are not on the page.Also, you are using
document.write()
this will not work for anything that is loaded after the page is rendered.I hope this helps.
diamond :
It says there is a problem (Uncaught TypeError: Cannot read property 'checked' of null at d2O). How do I fix it?
Ibrahima Diallo :
First of all, why would you write your code like that? You are making it hard for yourself to debug. Indent your code. Unminify it. Help yourself.
Then read the code. The error is obvious. One of the ids you are trying to get does not exist.
As :
Do you know where the error is? Thanks
document.getElementsByClassName("grade gradient-8-bg")[0].getElementsByTagName("input")[0].value = "ABC";
di :
i get the error code cannot read property "options" of null in my code below :
jefryarch :
This error occurs when you read a property or call a method on a null object . That's because the DOM API returns null for object references that are blank. An object is expected somewhere and wasn't provided. So, you will get null is not an object error if the DOM elements have not been created before loading the script. In JavaScript , null is not an object; and won't work. You must provide a proper object in the given situation.
We can resolve this type of issues by adding an event listener that will notify us when the page is ready. Once the addEventListener is fired, the init() method can make use of the DOM elements.
document.addEventListener('readystatechange', function() { if (document.readyState === "complete") { init(); }
Michael Wahome :
const colorBtn = document.querySelector('.colorBtn'); const bodyBcg =document.querySelector('body');
const colors=['yellow', 'red','green', '#3b5998'];
colorBtn =addEventListener('click',changeColor);
function changeColor(){ let random=Math.floor(Math.random()*colors.length) bodyBcg.style.backgroundColor =colors(random); }
What is the problem with this line?
colorBtn =addEventListener('click',changeColor);
Gives "Uncaught TypeError:assignment to constant variable"
Anitha Ngarajan :
Hello please help with the uncaught null error in the below:
fucntions defined later in the code...
The error says that the above:
Thanks!
Azumie :
Hello @Michael Wahome
The problem is that the addEventListener here is placed as a value of the colorBtn variable. That's not how the addEventListener works...
for more information about this: https://www.w3schools.com/JSREF/met_document_addeventlistener.asp
aznan ramin :
Code:
uncaught-typeerror-cannot-read-property-of-null,happens for line 4, please tell me what to do?
Shruti Mishra :
Can you please help me solve this error:
https://github.com/shrutimishra1214/error/blob/main/index.html
Find my code here
Chibuzo :
Am getting tired of everything about the error am getting after trying to run my code in javascript. I have being trying to build a quiz app and am get an error "TypeError: Cannot set property 'innerHTML' of null". The more i try to get solution from the internet concerning the problem the more frustrated i become. I created an external js file but all the solution i have been getting were based on internal file. please someone should attend to my problem because its urgent
mukty :
My code:
Please what's wrong with this code...I having the same error message at ##password2Value = password2.value.trim();
Ibrahim :
Hi @mukty
It looks like you are defining the form inputs before the DOM is ready. You can move all your code inside a function that will be called onload (or DOMContentLoaded).
Ex:
This way, you are accessing the form inputs after they have been loaded on the page.
mukty :
Please I'm trying to a simple validation of an email address that will display an error message when a wrong address is entered. However, I'm having issue displaying the error when the regular expression is used to test the email entered. Below is my code.
HTML:
javascripts code:
Rafay :
Hello IBRAHIM
Thanks for the Post
I am also facing a similar situation and can't find a answer
Here is my code:
I am trying to apply color on each card. My project is a note-taking website.
Here is the code used for applying color on card:
The issue is
elm1.options
line.Everytime i get this error
Thanks
Shohel :
The program is running before the calling fo
But when I call
Store.displayBooks
the error shows:The code:
Jeremiah :
Hello ibrahim, I can't find the error in my code. Could you kindly help me? Here is my Javascript code:
With my HTML code:
Very easy code actually, but I don't find the error. I appreciate your help.
GAYATRI :
I am getting error at line no. 16 as Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'length'). can you help solving it.
Ibrahim Diallo :
Hi @Gaytari
You just have to remove the
.json
on line 16 because res is the json object.Good luck
OSMmapper :
I am trying to display a polygon on OSM:
});
In the console row.Polygon shows the correct values but I still get the following error:
Uncaught TypeError: Cannot read properties of null (reading '0') at i.projectLatlngs (leaflet.js:5:79693) at i.projectLatlngs (leaflet.js:5:79838) at i.projectLatlngs (leaflet.js:5:79838) at i.project (leaflet.js:5:79369) at i.reset (leaflet.js:5:75435) at i.onAdd (leaflet.js:5:74837) at i.layerAdd (leaflet.js:5:63932) at i.whenReady (leaflet.js:5:42041) at i.addLayer (leaflet.js:5:64307) at i.addTo (leaflet.js:5:63275)
Can you please help me?
Valeri :
Regards. Excellent article is short and easy to read. I can understand what are my problem in my code. Thank you
yeli :
I just wanna know what is wrong in this code
Ibrahim Diallo :
Hi @yeli
It looks like your variable
contenedorServicios
is null. Look for the place where you define it. It's possible you defined it before the DOM was ready.Emmanuel :
Hello Please I use Jquery and mvc trying to save data into sqlServer but the data doesn't get save kindly assist me, my table name is tblProductStock.
jlamador :
I have this code, not function with Edge, Chrome only IE. can you help me please?
Ibrahim Diallo :
Hi jlamador
On this line:
Where does createForm come from? If it is the form element on the page, then you will have to do this:
Emmanuel :
Hello Sir, Please this code is giving me syntax error especially with this => and my SubTotal is not showing the result:
Alan :
Hey, here is my code:
let toggle = document.querySelector("#header.toggle-button"); let collapse = document.querySelectorAll("#header .collapse");
toggle.addEventListener('click', function(){ collapse.forEach(col => col.classList.toggle("collapse-toggle")); })
here is my error:
main.js:4 Uncaught TypeError: Cannot read properties of null (reading 'addEventListener'), any help?
Let's hear your thoughts