Link to home
Start Free TrialLog in
Avatar of Isaac
IsaacFlag for United States of America

asked on

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

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

ASKER CERTIFIED SOLUTION
Avatar of oferam
oferam
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Isaac

ASKER

The error is gone but now the console renders "Class 1: [object Object] "
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
SOLUTION
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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