TypeError: Cannot read property 'value' of null | reactjs |












0















I've 2 textboxes and 1 button as material UI components.



 </p>
<TextField id="chatidField" />
<br />
<p>
<code>Enter Your Name:</code>
<hr />
</p>
<TextField id="nameField" />
<br />
<br />
<Button
variant="contained"
color="secondary"
onClick={addToFirebase()}
>
Get Your Token
</Button>


the function that onClick calls is as follows:



  function addToFirebase() {
var chatid = document.getElementById("chatidField").value;
var name = document.getElementById("nameField").value;
console.log(chatid, name);
}


I keep getting the following error:




TypeError: Cannot read property 'value' of null



8 | var chatid = document.getElementById("chatidField").value;



9 | var name = document.getElementById("nameField").value;




full code: https://codesandbox.io/s/pqmrn4x










share|improve this question

























  • Pass function reference rather than call in onCick={addToFirebase}

    – Diljohn5741
    Nov 23 '18 at 5:18
















0















I've 2 textboxes and 1 button as material UI components.



 </p>
<TextField id="chatidField" />
<br />
<p>
<code>Enter Your Name:</code>
<hr />
</p>
<TextField id="nameField" />
<br />
<br />
<Button
variant="contained"
color="secondary"
onClick={addToFirebase()}
>
Get Your Token
</Button>


the function that onClick calls is as follows:



  function addToFirebase() {
var chatid = document.getElementById("chatidField").value;
var name = document.getElementById("nameField").value;
console.log(chatid, name);
}


I keep getting the following error:




TypeError: Cannot read property 'value' of null



8 | var chatid = document.getElementById("chatidField").value;



9 | var name = document.getElementById("nameField").value;




full code: https://codesandbox.io/s/pqmrn4x










share|improve this question

























  • Pass function reference rather than call in onCick={addToFirebase}

    – Diljohn5741
    Nov 23 '18 at 5:18














0












0








0








I've 2 textboxes and 1 button as material UI components.



 </p>
<TextField id="chatidField" />
<br />
<p>
<code>Enter Your Name:</code>
<hr />
</p>
<TextField id="nameField" />
<br />
<br />
<Button
variant="contained"
color="secondary"
onClick={addToFirebase()}
>
Get Your Token
</Button>


the function that onClick calls is as follows:



  function addToFirebase() {
var chatid = document.getElementById("chatidField").value;
var name = document.getElementById("nameField").value;
console.log(chatid, name);
}


I keep getting the following error:




TypeError: Cannot read property 'value' of null



8 | var chatid = document.getElementById("chatidField").value;



9 | var name = document.getElementById("nameField").value;




full code: https://codesandbox.io/s/pqmrn4x










share|improve this question
















I've 2 textboxes and 1 button as material UI components.



 </p>
<TextField id="chatidField" />
<br />
<p>
<code>Enter Your Name:</code>
<hr />
</p>
<TextField id="nameField" />
<br />
<br />
<Button
variant="contained"
color="secondary"
onClick={addToFirebase()}
>
Get Your Token
</Button>


the function that onClick calls is as follows:



  function addToFirebase() {
var chatid = document.getElementById("chatidField").value;
var name = document.getElementById("nameField").value;
console.log(chatid, name);
}


I keep getting the following error:




TypeError: Cannot read property 'value' of null



8 | var chatid = document.getElementById("chatidField").value;



9 | var name = document.getElementById("nameField").value;




full code: https://codesandbox.io/s/pqmrn4x







javascript html css reactjs material-ui






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 5:19







sooraj viswanath

















asked Nov 23 '18 at 5:14









sooraj viswanathsooraj viswanath

75




75













  • Pass function reference rather than call in onCick={addToFirebase}

    – Diljohn5741
    Nov 23 '18 at 5:18



















  • Pass function reference rather than call in onCick={addToFirebase}

    – Diljohn5741
    Nov 23 '18 at 5:18

















Pass function reference rather than call in onCick={addToFirebase}

– Diljohn5741
Nov 23 '18 at 5:18





Pass function reference rather than call in onCick={addToFirebase}

– Diljohn5741
Nov 23 '18 at 5:18












1 Answer
1






active

oldest

votes


















3














You have an error in your onClick handler, it should be addToFirebase not addToFirebase():



<Button
variant="contained"
color="secondary"
onClick={addToFirebase}
>


When you include (), it will run the addToFirebase function, and use the return value as the onClick handler. In addition, if the addToFirebase() function is a part of your class, you will likely need to use this.addToFirebase instead.






share|improve this answer



















  • 2





    @HemadriDasari They're null, because at the time that addToFirebase() is run, the actual DOM hasn't been rendered yet, so none of the elements with those ID's exist. After applying the onClick function correctly, when the button gets clicked, those elements should indeed exist in the DOM, and the error should go away.

    – FrankerZ
    Nov 23 '18 at 5:36













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53440954%2ftypeerror-cannot-read-property-value-of-null-reactjs%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














You have an error in your onClick handler, it should be addToFirebase not addToFirebase():



<Button
variant="contained"
color="secondary"
onClick={addToFirebase}
>


When you include (), it will run the addToFirebase function, and use the return value as the onClick handler. In addition, if the addToFirebase() function is a part of your class, you will likely need to use this.addToFirebase instead.






share|improve this answer



















  • 2





    @HemadriDasari They're null, because at the time that addToFirebase() is run, the actual DOM hasn't been rendered yet, so none of the elements with those ID's exist. After applying the onClick function correctly, when the button gets clicked, those elements should indeed exist in the DOM, and the error should go away.

    – FrankerZ
    Nov 23 '18 at 5:36


















3














You have an error in your onClick handler, it should be addToFirebase not addToFirebase():



<Button
variant="contained"
color="secondary"
onClick={addToFirebase}
>


When you include (), it will run the addToFirebase function, and use the return value as the onClick handler. In addition, if the addToFirebase() function is a part of your class, you will likely need to use this.addToFirebase instead.






share|improve this answer



















  • 2





    @HemadriDasari They're null, because at the time that addToFirebase() is run, the actual DOM hasn't been rendered yet, so none of the elements with those ID's exist. After applying the onClick function correctly, when the button gets clicked, those elements should indeed exist in the DOM, and the error should go away.

    – FrankerZ
    Nov 23 '18 at 5:36
















3












3








3







You have an error in your onClick handler, it should be addToFirebase not addToFirebase():



<Button
variant="contained"
color="secondary"
onClick={addToFirebase}
>


When you include (), it will run the addToFirebase function, and use the return value as the onClick handler. In addition, if the addToFirebase() function is a part of your class, you will likely need to use this.addToFirebase instead.






share|improve this answer













You have an error in your onClick handler, it should be addToFirebase not addToFirebase():



<Button
variant="contained"
color="secondary"
onClick={addToFirebase}
>


When you include (), it will run the addToFirebase function, and use the return value as the onClick handler. In addition, if the addToFirebase() function is a part of your class, you will likely need to use this.addToFirebase instead.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 23 '18 at 5:18









FrankerZFrankerZ

17k72862




17k72862








  • 2





    @HemadriDasari They're null, because at the time that addToFirebase() is run, the actual DOM hasn't been rendered yet, so none of the elements with those ID's exist. After applying the onClick function correctly, when the button gets clicked, those elements should indeed exist in the DOM, and the error should go away.

    – FrankerZ
    Nov 23 '18 at 5:36
















  • 2





    @HemadriDasari They're null, because at the time that addToFirebase() is run, the actual DOM hasn't been rendered yet, so none of the elements with those ID's exist. After applying the onClick function correctly, when the button gets clicked, those elements should indeed exist in the DOM, and the error should go away.

    – FrankerZ
    Nov 23 '18 at 5:36










2




2





@HemadriDasari They're null, because at the time that addToFirebase() is run, the actual DOM hasn't been rendered yet, so none of the elements with those ID's exist. After applying the onClick function correctly, when the button gets clicked, those elements should indeed exist in the DOM, and the error should go away.

– FrankerZ
Nov 23 '18 at 5:36







@HemadriDasari They're null, because at the time that addToFirebase() is run, the actual DOM hasn't been rendered yet, so none of the elements with those ID's exist. After applying the onClick function correctly, when the button gets clicked, those elements should indeed exist in the DOM, and the error should go away.

– FrankerZ
Nov 23 '18 at 5:36




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53440954%2ftypeerror-cannot-read-property-value-of-null-reactjs%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Wiesbaden

Marschland

Dieringhausen