Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript javascript error

I have this code all the ids in HTML exist.
JavaScript:
const materials = [
    {
        name: 'stone_box',
        amount: stoneAmt
    },
    {
        name: 'redMushSeeds',
        amount: redMushSd
    },
    {
        name: 'cop_box',
        amount: copperAmt
    },
    {
        name: 'iron_box',
        amount: ironAmt
    }
]

for (const material in materials) {
    if (material.amount <= 0) {
        material.amount = 0;
        document.getElementById(material.name).style.display = "none";
    } 
    else {
        document.getElementById(material.name).style.display = 'inline-block';
    }
}


and it gives me an error
script.js:1281 Uncaught TypeError: Cannot read properties of null (reading 'style')
 
Last edited by a moderator:
Solution
D
You guys are on the right track indeed, and I was wrong. I was assuming that the loop was ok, and thus the error must mean that some id's did not exist. But no, the loop was wrong.

This
for (const material in materials) {
should be
for (material of materials) {

The output of this code
JavaScript:
for (const x in materials)
{
    console.log(x);
}
for (y of materials)
{
    console.log(y);
}

shows the difference:
a.jpg
Using a loop counter is of course a possibility too if you want a bit more coding 🙂
I should have been more precise and have written "at least one of the id's stone_box, redMushSeeds, copperAmt, iron_box does not exist". This is what the error message is trying to tell you. Do some debugging to find out which, and correct that.
 
Last edited by a moderator:
let materials = [ { name: 'stone_box', amount: stoneAmt }, { name: 'redMushSeeds', amount: redMushSd }, { name: 'cop_box', amount: copperAmt }, { name: 'iron_box', amount: ironAmt }];

for (let i = 0; i < materials.length; i++) {
if (materials.amount <= 0) {
materials.amount = 0;
document.getElementById(materials.name).style.display = 'none';
} else {
document.getElementById(materials.name).style.display = 'inline-block';
}
}

try this
 
let materials = [ { name: 'stone_box', amount: stoneAmt }, { name: 'redMushSeeds', amount: redMushSd }, { name: 'cop_box', amount: copperAmt }, { name: 'iron_box', amount: ironAmt }];

for (let i = 0; i < materials.length; i++) {
if (materials.amount <= 0) {
materials.amount = 0;
document.getElementById(materials.name).style.display = 'none';
} else {
document.getElementById(materials.name).style.display = 'inline-block';
}
}

try this
Unfortunately, this will not work either. You are on the right path, but you are missing a little something: think more of the loop ;)
 
You guys are on the right track indeed, and I was wrong. I was assuming that the loop was ok, and thus the error must mean that some id's did not exist. But no, the loop was wrong.

This
for (const material in materials) {
should be
for (material of materials) {

The output of this code
JavaScript:
for (const x in materials)
{
    console.log(x);
}
for (y of materials)
{
    console.log(y);
}

shows the difference:
a.jpg
Using a loop counter is of course a possibility too if you want a bit more coding 🙂
 
Solution
You guys are on the right track indeed, and I was wrong. I was assuming that the loop was ok, and thus the error must mean that some id's did not exist. But no, the loop was wrong.

This
for (const material in materials) {
should be
for (material of materials) {

The output of this code
JavaScript:
for (const x in materials)
{
    console.log(x);
}
for (y of materials)
{
    console.log(y);
}

shows the difference:
View attachment 2005
Using a loop counter is of course a possibility too if you want a bit more coding 🙂
though yes this is right BUT you need to put a const or vscode will bug you.
JavaScript:
for (const material of materials)
 
though yes this is right BUT you need to put a const or vscode will bug you.
JavaScript:
for (const material of materials)
although that may work, you don't necessarily need to have const in there... in practice, I have seen and used var and let for the for loop parameters. It all depends on the scope of the variable
 
In the browser I needed neither var or const to have it work. Although using var would be good practice. I was wondering about the const, because inside the loop you are modifying materials.amount which I though shouldn't be allowed then. Perhaps the const does not extend to members ? Anyway it's solved now, right ?
 
Back
Top Bottom