Solved

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

Posted on 2014-12-19
5
721 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
ID: 40510405
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
ID: 40510406
The error is gone but now the console renders "Class 1: [object Object] "
0
 
LVL 6

Expert Comment

by:oferam
ID: 40510420
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 83

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 150 total points
ID: 40510474
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 83

Expert Comment

by:Dave Baldwin
ID: 40510477
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

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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…

777 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