最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Uncaught TypeError: Cannot read property 'toUpperCase' of null - Stack Overflow

programmeradmin0浏览0评论

My code seems right but I don't know why I am getting this error:

Uncaught TypeError: Cannot read property 'toUpperCase' of null

Here is my code:

    //The function is executed after someone clicks the "Take the Quiz" 
    function startquiz() {
        //The variable for the first question
        var FirstAnwser = prompt("Who posted the first youtube video?");
        //The if statement for the first question
        if (FirstAnwser.toUpperCase()  === 'JAWED KARIM') {
            //If the person is correct a dialog box that says correct pops up
           alert("Correct");
            //The Variable for the second question
            var SecondAnwser = prompt("When was the domain name youtube     activated?");
            if (SecondAnwser.toUpperCase() === 'FEBUARY 14, 2005') {
                alert("Correct");
                var ThirdAnwser = prompt("What was the first video on youtube called?");
                if (ThirdAnwser.toUpperCase() === 'ME AT THE ZOO') {
                    alert("Correct");
                } else {
                    alert("Sorry, That is Wrong");
                }

            } else {
                alert("Sorry, That is Wrong");
            }
        } else {
            //If the person is wrong a dialog box pops up which says "Sorry,  That is wrong"
            alert("Sorry, That is Wrong");
        }
    }

the error is on the line that says if (SecondAnwser.toUpperCase() === 'FEBUARY 14, 2005') {

My code seems right but I don't know why I am getting this error:

Uncaught TypeError: Cannot read property 'toUpperCase' of null

Here is my code:

    //The function is executed after someone clicks the "Take the Quiz" 
    function startquiz() {
        //The variable for the first question
        var FirstAnwser = prompt("Who posted the first youtube video?");
        //The if statement for the first question
        if (FirstAnwser.toUpperCase()  === 'JAWED KARIM') {
            //If the person is correct a dialog box that says correct pops up
           alert("Correct");
            //The Variable for the second question
            var SecondAnwser = prompt("When was the domain name youtube.     activated?");
            if (SecondAnwser.toUpperCase() === 'FEBUARY 14, 2005') {
                alert("Correct");
                var ThirdAnwser = prompt("What was the first video on youtube called?");
                if (ThirdAnwser.toUpperCase() === 'ME AT THE ZOO') {
                    alert("Correct");
                } else {
                    alert("Sorry, That is Wrong");
                }

            } else {
                alert("Sorry, That is Wrong");
            }
        } else {
            //If the person is wrong a dialog box pops up which says "Sorry,  That is wrong"
            alert("Sorry, That is Wrong");
        }
    }

the error is on the line that says if (SecondAnwser.toUpperCase() === 'FEBUARY 14, 2005') {

Share Improve this question edited Aug 24, 2016 at 3:46 davidhu 10.5k7 gold badges35 silver badges57 bronze badges asked Aug 24, 2016 at 1:17 TheRandomSwag 101TheRandomSwag 101 311 gold badge1 silver badge3 bronze badges 5
  • 2 First pedantic thing: the word is spelled "answer" – Pointy Commented Aug 24, 2016 at 1:20
  • 3 and FEBUARY is spelled wrong – epascarello Commented Aug 24, 2016 at 1:21
  • And to top it off, I can't reproduce the problem: jsfiddle/jt319dj0. Would have helped if I thought about clicking Cancel. – Drew Kennedy Commented Aug 24, 2016 at 1:22
  • As an(other) aside, even ignoring the misspelling, do you think your users are likely to input the date in exactly that format? – nnnnnn Commented Aug 24, 2016 at 1:51
  • Well nnnnnn, than you for correcting my spelling errors (I probably should use grammarly) but I have no idea how to make my program recognize the date with or without the ma. – TheRandomSwag 101 Commented Aug 24, 2016 at 15:52
Add a ment  | 

3 Answers 3

Reset to default 5

The prompt() method returns the input value if the user clicks "OK". If the user clicks "cancel" the method returns null and your script report an error, because there is no function on null object.

Solution: check if answer isn't null, before you call toUpperCase()

if (SecondAnswer != null && SecondAnwser.toUpperCase() === 'FEBUARY 14, 2005') 

i think the error message is indicating the mistake of the code. This happens when SecondAnswer is null.

To avoid this error you can just include a check on top of

if (SecondAnwser.toUpperCase() === 'FEBUARY 14, 2005') 

That is

if (SecondAnwser !== null) {
  if (SecondAnwser.toUpperCase() === 'FEBUARY 14, 2005') {
   //
  }
}

or

if (SecondAnwser !== null && SecondAnwser.toUpperCase() === 'FEBUARY 14, 2005') {
 //
}

Very Strange, sometimes the java-script console says there is an error but sometimes it doesn't. Anyways, my program seems to be working correctly, so I will ignore the error. Thank you for all of you're help though (this is my first time asking a question on stackoverflow and i'm amazed by how quickly people answer my questions.)

发布评论

评论列表(0)

  1. 暂无评论