Object prototype may only be an Object or null: undefined

Hey guys,


I am getting this error, but cannot figure out what I'm doing wrong.


I am fairly new to Javascript, but the plugin I was using as a reference for the saving/loading seems to work fine and mine doesn't despite me being unable to see any fundamental differences.


TypeError: Object prototype may only be an Object or null: undefined {stack: (...), message: "Object prototype may only be an Object or null: undefined"}


//-----------------------------------------------------------------------------
// DATA MANAGER
//-----------------------------------------------------------------------------

var farming_DataManager_createGameObjects = DataManager.createGameObjects;
var farming_DataManager_makeSaveContents = DataManager.makeSaveContents;
var farming_DataManager_extractSaveContents = DataManager.extractSaveContents;
DataManager.createGameObjects = function () {
farming_DataManager_createGameObjects.call(this);
$farming = new Farming();
}
DataManager.makeSaveContents = function () {
var contents = farming_DataManager_makeSaveContents(this);
contents.farming = $farming;
return contents;
}
DataManager.extractSaveContents = function (contents) {
farming_DataManager_extractSaveContents.call(this, contents);
$farming = contents.farming;
var f = new Farming();
$farming.__proto__ = f.__proto__;
}




That code is basically copied from Vlue's GameTime plugin where it works fine. Mine does not however.


Here is my Farming Object:


//-----------------------------------------------------------------------------
// FARMING OBJECT
//-----------------------------------------------------------------------------
function Farming() {
this.initialize.apply(this, arguments);
}
Farming.prototype.initialize = function () {
console.log("Farming Initialized");
}


Just a console log in the initialization.


I feel like I am missing something obvious..
var contents = farming_DataManager_makeSaveContents(this);


???


_makeSaveContents.call(this)


This seems a bit weird to me too:


$farming.__proto__ = f.__proto__;


I don't know what it is or what it's trying to do.  I'm really not that familiar with JS, especially wrt prototyping functions, but I don't recall ever seeing anything like this before.


Check the console and see what line the error is occurring on.
var contents = farming_DataManager_makeSaveContents(this);


???


_makeSaveContents.call(this)


This seems a bit weird to me too:



$farming.__proto__ = f.__proto__;


I don't know what it is or what it's trying to do.  I'm really not that familiar with JS, especially wrt prototyping functions, but I don't recall ever seeing anything like this before.


Check the console and see what line the error is occurring on.



I tried changing to _makeSaveContents.call(this), but it still gives me the same error. 
Also I tried saving a single string instead and loading that and that works fine.


The error seems to be on the creation of the Farming object. It works fine when you start a new game, but when you load the game it errors on that line.


Specifically here


DataManager.createGameObjects = function () {
farming_DataManager_createGameObjects
.call(this);
$farming
= new Farming();
}






In regards to the __proto__, here is a link that explains it. That line was copied from the other plugin and removing it doesn't fix the problem.


https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/proto


Thanks for your reply!
Can you provide a screenshot of the console log?  It often gives more useful error messages (not always, and maybe not in this case).
Here's the log!


[IMG]http://i.imgur.com/8VtPIpq.png[/IMG]
I don't really know what's going on.  I'm thinking maybe you shouldn't assign the prototype at all, as it seems to be having trouble saving/loading it.


Do you need this line?


$farming.__proto__ = f.__proto__;


If you remove that line, start a new game, save, close, go back in and resume the saved game, does it work?


Also, which version of MV are you using (or which version did you use to originally create your project)?
No removing that line doesn't change anything unfortunately. I feel like it's something related to the Object creation itself.


I'm just baffled because it seems like I am doing exactly what that other plugin is doing, and that plugin works perfectly. 


I must be missing something. I'll have a look at it again with a fresh mind after work


Edit: I am using Version 1.2.0 and have not changed the version since starting the project
When you remove that line, are you starting a new game then saving, or are you just trying to load the previously saved game?
I have been always starting a new game, saving, exiting to menu and loading when testing changes
yeah, sorry - I can't help you anymore.  I'm not familiar enough with JS to go any further.  Hopefully someone with more experience will be along before too long and will be able to get you further.
Thanks so much for your help!


I really appreciate it 🙂
Omg I am an idiot.


I renamed the 'Farming' class to another name and everything works fine...


I must have 'Farming' defined in some other plugin somewhere. Sorry for wasting your time Shaz!
Follow up on this problem:


Everything is working as expected, and I discovered the reason __proto__ gets assigned.


The classes are just Objects when they are deseiralised from the save file, which means that they can hold their variables just fine, but do NOT hold onto the functions because they are simply Objects now.


Assigning the __proto__ sets the type/class and so they then have the expected functions.


I don't like the idea of setting the __proto__, and am running into a problem that makes me want to pursue a different approach.


I have a serialised array of 'MyObject's that gets saved in the save file.


This means that in order to assign them their correct class I would have to iterate through the array. I definitely want them to be 'MyObject' because 'MyObject' has some functions that I want to be able to use.

I could do this approach, but maybe there is a better way? The problem with this one is it requires a lot of upkeep.


It means that if I add another type somewhere in my base object I need to ensure I search for all instances of those and assign them their protos too.


Here's some example code:


var farming_DataManager_extractSaveContents = DataManager.extractSaveContents;
DataManager.extractSaveContents = function (contents) {
farming_DataManager_extractSaveContents.call(this, contents);

$farming = contents.farming;

var f = new Farming();
$farming.__proto__ = f.__proto__;

var fp = new FarmPlot();
for(int i = 0; i < $farming.maps.length; i ++){
for(int j = 0; j < $farming.maps; j++){
$farming.maps.Farmplots[j].__proto__ = fp.__proto__;
}
}
}






Edit:


Another solution I just thought of would be to have functions on the class not on the prototype. And the functions take in what is assumed to be a farmplot object.


e.g. usage:


FarmPlot.Spawn(farmplotObj);


It's not a terrible idea to be honest...

Original thread: https://forums.rpgmakerweb.com/threads/object-prototype-may-only-be-an-object-or-null-undefined.65476/