Click here to Skip to main content
15,899,679 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi all,
i got this error "Uncaught TypeError: Cannot read property '0' of null" in console while click the link.
C#
var c = parseInt(e.attr("class").match(/rating\d+/)[0].replace('rating', ''));
this is the line which shows the error.

Thanks,
Sampath
Posted
Updated 14-May-14 3:17am
v3
Comments
joshrduncan2012 14-May-14 9:41am    
Ok, so what is your question?
Sergey Alexandrovich Kryukov 14-May-14 11:04am    
Is it Javascript? If it is, why all the other tags in the question? When you add unrelated tags, it only reduces you chance to have anyone who would look at your question and answer.

As to your question: rewrite it in several different lines, with intermediate variables. Then, under the debugger, examine each step. Find out where you expected non-null but get null. What is e? it could be null, or e.attr("class") can be null, and so on...

—SA

1 solution

Never chain anything unless you know for sure the intermediate objects are defined.
I.e. x.y[z].f() is far too offensive programming.

In your case:
- e.attr("class") may not point to any object
- ...match(...) may not match, thus the number of matches is 0
- ...[0] may be out of range (see above)

Split it into separate chunks, store in a variable each and check if the returned object is defined before going on to the next part.

Cheers
Andi
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900