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
Solved

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

Posted on 2014-12-19
5
776 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

860 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