1

Here is the code :

for (i = 0; i < data.RecruitingGroups.length; i++) {
        data.RecruitingGroups[i].id = i;

        if (data.RecruitingGroups[i].Rule.Rules != null) {
            for (j = 0; j < data.RecruitingGroups[i].Rule.Rules.length; i++) {
                data.RecruitingGroups[i].Rule.Rules[j].id = j;
            }    
        } 
    }

Problem is that sometimes RecruitingGroups[].Rule is null. So I tried to verify it was not null before continuing and running the next for loop, but it still throws the error:

Uncaught TypeError: Cannot read property 'Rules' of null

How can i bypass this error.

3 Answers 3

1

your second loop needs to increment j++ not i++. =)

Sign up to request clarification or add additional context in comments.

1 Comment

i like this allen dude!
1

You're testing if Rule.Rules is null. That's not the problem; Rule itself is null, as evidenced by the error message. You need to test both Rule and Rule.Rules if either can be null.

Try

if (data.RecruitingGroups[i].Rule && data.RecruitingGroups[i].Rule.Rules) {

3 Comments

same error Uncaught TypeError: Cannot read property 'Rules' of null
Then you're accessing Rule.Rules somewhere else without first checking if Rule is not null. The code I've posted cannot produce that error; it's impossible for my if statement to attempt to access Rule.Rules if Rule is null.
Found problem i++ was in second for loop. should be j++
-1
 if (data.RecruitingGroups[i].Rule && data.RecruitingGroups[i].Rule.Rules) {
        for (j = 0; j < data.RecruitingGroups[i].Rule.Rules.length; i++) {
            data.RecruitingGroups[i].Rule.Rules[j].id = j;
        }    
    } 

6 Comments

because Rule is the one that is null, not Rules
Your doing the same thing the original code is doing, and you're going to be producing the exact same error.
i read Uncaught TypeError: Cannot read property 'Rules' of null meaning that Rules was the issue, i will correct.
Now your answer contains an obvious syntax error, and it also happens to be identical to my answer, except without any explanation of what was wrong or what has been fixed.
problem was in the second for loop. j++ was needed not i++
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.