Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

nil:Nilclass: What is it and why does it exist?

Well, I've been using ruby and rgss for a while, and this error has always puzzled me. Why does Ruby have an undocumented catchall error that nobody can figure out? Anyway, I was wondering if anyone knew what could trigger this error, as well as possible ways to solve it in various situations. (I would have more to contribute in that area, if I knew what it was that I changed that finally caused the script to work)
 
For so far as I can tell, all nil:Nilclass tells you as that a class you are calling a method on does not exist, i.e. equals nil. Since it doesn't exist, it's obviously gonna give an error because you can't do something with an unexistant object.

I could be wrong, but I'm fairly sure that's what the error is for.
 
Beran":mvq0lrws said:
For so far as I can tell, all nil:Nilclass tells you as that a class you are calling a method on does not exist, i.e. equals nil. Since it doesn't exist, it's obviously gonna give an error because you can't do something with an unexistant object.

I could be wrong, but I'm fairly sure that's what the error is for.

The why does it tell me that things like the operators + - * and / are undefined and part of no class? I have even had this error where the coding pointing to the method was correct, and it just wouldn't read the method. (I get it a lot when trying to read something I'm inserting into Game_System)
 
The thing is that, to operate on class, there needs to be an instance of the class. Most of the cases I've come across where it throws a nil:Nilclass error, is where the instance of the class has not been created yet. i.e. trying to call on methods in certain classes during the title screen will give you that error simply due to the fact that the instance of the class only gets created later.
Another mistake I USED to make was just trying to go Game_System.method. That won't work, because it's not an instance of a class.
 
Beran":2u5hidy9 said:
The thing is that, to operate on class, there needs to be an instance of the class. Most of the cases I've come across where it throws a nil:Nilclass error, is where the instance of the class has not been created yet. i.e. trying to call on methods in certain classes during the title screen will give you that error simply due to the fact that the instance of the class only gets created later.
Another mistake I USED to make was just trying to go Game_System.method. That won't work, because it's not an instance of a class.

I refer to Game_System properly, as $game_system. Still why would it tell me the operators are undefined? That makes no sense!
 
Because nil:Nilclass doesn't have ANY methods defined. Including the very basic methods that are common to all classes.
I don't think nil:Nilclass is even a class, it's just...nil
and you can see what I mean by just creating a script call in an event like
Code:
$some_class.do_stuff
As you'll see, since the class doesn't exist, it'll throw a nil:Nilclass error
 
Beran":1snnvk31 said:
Because nil:Nilclass doesn't have ANY methods defined. Including the very basic methods that are common to all classes.
I don't think nil:Nilclass is even a class, it's just...nil
and you can see what I mean by just creating a script call in an event like
Code:
$some_class.do_stuff
As you'll see, since the class doesn't exist, it'll throw a nil:Nilclass error

What I'm saying is that I have gotten the error when referring to the Game_System class, the way it is defined in the RMXP .exe file, which is $game_system. Also, even when I am not referring to an external class, I can still get the error regarding the class that the method in question is from. (As in, I can try to use one method from a class in another method in the same class, and still get the error)
 
Referring to classes by their proper name doesn't mean they exist. $game_system is only called as an instance halfway through the main method of Scene_Title.
If something tries to operate on $game_system, before the line in Scene_Title saying "$game_system = Game_System.new" it'll throw a nil:Nilclass error due to the fact that $game_system simply doesn't exist.
As for getting in nil:Nilclass error when referring to a class within itself, can you give me an actual example of code that did that?
 
Beran":3ag85gnk said:
Referring to classes by their proper name doesn't mean they exist. $game_system is only called as an instance halfway through the main method of Scene_Title.
If something tries to operate on $game_system, before the line in Scene_Title saying "$game_system = Game_System.new" it'll throw a nil:Nilclass error due to the fact that $game_system simply doesn't exist.
As for getting in nil:Nilclass error when referring to a class within itself, can you give me an actual example of code that did that?

Actually, I scrapped that code a while ago, because I couldn't get it to work. What really puzzles me is when it will say that the operators are undefined, even though I'm not even referring to something outside the method. (And no, I don't have an example of that, either)
 
As far as I'm concerned, you cannot use operators as in '+=' and the likes on nil values.
By this means, if you want to use such an operator on a variable, it has to be defined. This code works:
Code:
var = 0
var += 5
print var # prints 5
This code does NOT work:
Code:
var += 5
print var
If you used such an operator before a definition, it would result in such an error.

About the NilClass. By default, all variables have to be defined somehow. This is done by using '=' and a value.
Once this has been done, all operators and methods available for that type or class will become available. A simple method to check if it's a certain type:
Code:
if var.is_a?(Integer)
  var += 1
else
  var = 0 #defines as an integer
end
I haven't tried, but I reckon that using Integer.new is also a possible way to define integers, such as Array.new works for arrays.

I know you said you did not have an example, but how did you encounter the error without some code?
Can't you provide that code if your question still remains?

Hope this is of any use.
 
Well, what I meant is that I've encountered this a lot, and never quite figured out what caused it. The most recent example I provided was the $game_system one, although I have seen others as well, over time. I have no examples because, in every case, I somehow fixed it or scrapped the code.
 
If you read through my previous post, it pretty much explains NilClass simplistically.
Very simple definition: "A variable has to be defined before operations may be used. This excludes anything behind a '='."

Now to sum up what Beran said. If you have the following code:
Code:
$game_system.bgm_stop
$game_system = Game_System.new
The first line will return the NilClass error. Why? Because the '$game_system' is not defined until the line after. Which in general means, you cannot call any functions and operations until the class is instanced.

If you run into this again any time soon, post an example and I will explain the problem you're having.
 
It's not unlikely that the error is so common they didn't feel a need to document it. All the error tells you is that an instanc of a class or variable that you are referring to simply does not exist. Since anything that doesn't exist automatically equals nil, and nil has absolutely nothing defined for it, not even the standard, very low level, methods, anything you try to do with nil is just....wrong, it won't work. After all...how do you do something with nothing?
Another great example of a common place to find nil:Nilclass errors is when using the Game_Actors class.
You call an instance of the class as $game_actors[ID]
But if you call a non-existant ID, such as 0, you'll get the error, since that instance of the class doesn't actually exist. In this case, $game_actors is actually an array of class instances. whee!
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top