Solved

SCRIPT5007: Unable to get property '0' of undefined or null reference

Posted on 2014-12-19
5
543 Views
Last Modified: 2014-12-20
Why do I get an error on this line?  "SCRIPT5007: Unable to get property '0' of undefined or null reference "
            console.log("Class 1: "+newStudent.Classes[0]);

		function student(firstName,lastName,ssn,dormName)
		{
			this.fname = firstName;
			this.lname = lastName;
			this._ssn = ssn || 0;
			this.classes = [];
			this.dName = dormName || "A Hall";
		};
		
		// Individually add methods to the prototype
		student.prototype.addClasses = function(className, roomNumber) {
			this.classes.push({className: className, roomNumber: roomNumber});
		};

		Object.defineProperty(student.prototype, "fullname", {
			get: function() {
				return this.fname + " " + this.lname;
			}
		});
		
		var newStudent = new student("John","Doe","231-31-1313","jjjjj1111");
		console.log("HELLO WORLD");
		console.log(newStudent.fullname);
		newStudent.addClasses("Algebra", "33B");
		newStudent.addClasses("Geometry", "44B");
		newStudent.addClasses("History", "12B");
		newStudent.addClasses("Sociology", "144C");
		newStudent.addClasses("Biology", "312D");
		newStudent.addClasses("Calculus", "98S");
		newStudent.addClasses("Statistics", "55B");
		
		console.log("Number of classes: " + newStudent.classes.length);
		console.log("Class 1: "+newStudent.Classes[0]);

Open in new window

0
Comment
Question by:Isaac
  • 2
  • 2
5 Comments
 
LVL 6

Accepted Solution

by:
oferam earned 350 total points
Comment Utility
It's case sensitive - "console.log("Class 1: "+newStudent.Classes[0])" should be console.log("Class 1: "+newStudent.classes[0])
0
 
LVL 5

Author Comment

by:Isaac
Comment Utility
The error is gone but now the console renders "Class 1: [object Object] "
0
 
LVL 6

Expert Comment

by:oferam
Comment Utility
That's another question :)

I guess it's because it does not recognize the object you've inserted.

try printing newStudent.classes[0].className - to get the class name and roomNumber to get the room number
0
 
LVL 82

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 150 total points
Comment Utility
Out of curiosity, I made a web page out of your code and @oferam is right, you needed to 'name' the objects to get them to appear.  How are you seeing the response in the 'console'?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>JS Classes</title>
</head>
<body>
<h1>JS Classes</h1>
<script type="text/javascript">
<!--
		function student(firstName,lastName,ssn,dormName)
		{
			this.fname = firstName;
			this.lname = lastName;
			this._ssn = ssn || 0;
			this.classes = [];
			this.dName = dormName || "A Hall";
		};
		
		// Individually add methods to the prototype
		student.prototype.addClasses = function(className, roomNumber) {
			this.classes.push({className: className, roomNumber: roomNumber});
		};

		Object.defineProperty(student.prototype, "fullname", {
			get: function() {
				return this.fname + " " + this.lname;
			}
		});
		
		var newStudent = new student("John","Doe","231-31-1313","jjjjj1111");
		console.log("HELLO WORLD");
		console.log(newStudent.fullname);
		newStudent.addClasses("Algebra", "33B");
		newStudent.addClasses("Geometry", "44B");
		newStudent.addClasses("History", "12B");
		newStudent.addClasses("Sociology", "144C");
		newStudent.addClasses("Biology", "312D");
		newStudent.addClasses("Calculus", "98S");
		newStudent.addClasses("Statistics", "55B");
		
		console.log("Number of classes: " + newStudent.classes.length);
		document.write("Number of classes: " + newStudent.classes.length + "<br>");
		console.log("Class 1: "+newStudent.classes[0]);
		document.write("Class 1: "+newStudent.classes[0].className+" "+newStudent.classes[0].roomNumber);

// -->
</script>
</body>
</html>

Open in new window

0
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
And to list all of the classes...
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>JS Classes</title>
</head>
<body>
<h1>JS Classes</h1>
<script type="text/javascript">
<!--
		function student(firstName,lastName,ssn,dormName)
		{
			this.fname = firstName;
			this.lname = lastName;
			this._ssn = ssn || 0;
			this.classes = [];
			this.dName = dormName || "A Hall";
		};
		
		// Individually add methods to the prototype
		student.prototype.addClasses = function(className, roomNumber) {
			this.classes.push({className: className, roomNumber: roomNumber});
		};

		Object.defineProperty(student.prototype, "fullname", {
			get: function() {
				return this.fname + " " + this.lname;
			}
		});
		
		var newStudent = new student("John","Doe","231-31-1313","jjjjj1111");
		console.log("HELLO WORLD");
		console.log(newStudent.fullname);
		newStudent.addClasses("Algebra", "33B");
		newStudent.addClasses("Geometry", "44B");
		newStudent.addClasses("History", "12B");
		newStudent.addClasses("Sociology", "144C");
		newStudent.addClasses("Biology", "312D");
		newStudent.addClasses("Calculus", "98S");
		newStudent.addClasses("Statistics", "55B");
		
		console.log("Number of classes: " + newStudent.classes.length);
		document.write("Number of classes: " + newStudent.classes.length + "<br>");
		for(var ii=0;ii<newStudent.classes.length;ii++) {
		console.log("Class "+(ii+1)+": "+newStudent.classes[ii]);
		document.write("Class "+(ii+1)+": "+newStudent.classes[ii].className+" "+newStudent.classes[ii].roomNumber + "<br>");
		}

// -->
</script>
</body>
</html>

Open in new window

0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now