Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2014-12-19
5
Medium Priority
?
1,084 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 6

Accepted Solution

by:
oferam earned 1400 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 84

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 600 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 84

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

What’s Wrong with Your Cloud Strategy ?

Even as many CIOs are embracing a cloud-first strategy, the reality is that moving to the cloud is a lengthy process and the end-state is likely to be a blend of multiple clouds—public and private. Learn why multicloud solutions matter in this webinar by Nimble Storage.

Question has a verified solution.

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

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
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…

636 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