Uncaught TypeError Cannot read property value of null

0 votes

I have been getting an ERROR in the code as I'm trying to do an event where when the page is loaded, it will do the event. But the problem is when I go to another function, but on the same page, I get an error of null on that variable. It has no problem when I execute this code, but when I'm on other parts of my code this error occurs:-

Uncaught TypeError: Cannot read property 'value' of null
$(document).ready(function(){ 



              var str = document.getElementById("cal_preview").value; 
              var str1 = document.getElementById("year").value; 
              var str2 = document.getElementById("holiday").value; 
              var str3 = document.getElementById("cal_option").value; 

            if (str=="" && str1=="" && str2=="" && str3=="" ) 
              { 
                document.getElementById("calendar_preview").innerHTML=""; 
                return; 
  } 
if (window.XMLHttpRequest) 
{// code for IE7+, Firefox, Chrome, Opera, Safari 
    xmlhttp=new XMLHttpRequest(); 
} 
else 
{// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }    xmlhttp.onreadystatechange=function() 
{ 
      if (xmlhttp.readyState==4 && xmlhttp.status==200) 
  { 
      document.getElementById("calendar_preview").innerHTML=xmlhttp.responseText; 

    } 
} 

var url = calendar_preview_vars.plugin_url + "?id=" + str +"&"+"y="+str1+"&"+"h="+str2+"&"+"opt="+str3; 
xmlhttp.open("GET",url,true); 
xmlhttp.send(); });
Feb 22, 2022 in Java by Rahul
• 9,670 points
5,391 views

1 answer to this question.

0 votes

As you have not shared your HTML code, I am unsure as to which of them is wrong, but one of these does not exist:

var str = document.getElementById("cal_preview").value; 
var str1 = document.getElementById("year").value; 
var str2 = document.getElementById("holiday").value; 
var str3 = document.getElementById("cal_option").value;

There is either no element with the id cal_preview, year, holiday, cal_option, or some combination. Therefore, JavaScript is unable to read the value of something that does not exist. However, if you want to check that the element exists first, you could use an if statement for each:

var str, 
element = document.getElementById('cal_preview'); 
if (element != null) { 
    str = element.value; 
} 
else { 
      str = null; 
}

You could obviously change the else statement if you want or have no else statement at all, but this will vary with preference.

answered Feb 22, 2022 by Aditya
• 7,680 points

Related Questions In Java

0 votes
1 answer

Cannot read property 'push' of undefined when combining arrays

The reason why you get this ERROR ...READ MORE

answered Feb 11, 2022 in Java by Rahul
• 9,670 points
1,789 views
0 votes
1 answer
0 votes
1 answer

In which class is the "length" property of array defined ?

It's defined in the Java language specification: The members ...READ MORE

answered May 8, 2018 in Java by Rishabh
• 3,620 points
874 views
0 votes
1 answer
0 votes
1 answer

Presenting docket dtates inside html page by javascript

Use the Docker Engine Api:Docker Engine API ...READ MORE

answered Jun 20, 2018 in Docker by DareDev
• 6,890 points
484 views
0 votes
1 answer

Migrating proxy npm repo in nexus 3

I don't think you can achieve this ...READ MORE

answered Jun 22, 2018 in DevOps Tools by DareDev
• 6,890 points
1,203 views
+1 vote
1 answer

What is the difference between JavaScript and Java

This quote rightly explains that 2 totally ...READ MORE

answered Jun 29, 2018 in Java by Daisy
• 8,120 points
565 views
0 votes
1 answer

jQuery fix for "Uncaught TypeError: $ is not a function" error

 Use the following lines of code in ...READ MORE

answered Feb 16, 2022 in Java by Aditya
• 7,680 points
1,649 views
0 votes
1 answer

Cannot open local file - Chrome: Not allowed to load local resource

To answer your query, start with opening ...READ MORE

answered Feb 18, 2022 in Java by Aditya
• 7,680 points
7,849 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP