Skip to content
This repository was archived by the owner on Aug 21, 2023. It is now read-only.
This repository was archived by the owner on Aug 21, 2023. It is now read-only.

Undefined method [] for Nil:Class during compilation #25

@elrok123

Description

@elrok123

Getting a problem when trying to create an instance of a collection, I can't get past the compilation as I think the compiler is trying to account for the client instance possibly being 'Nil' and there being no method handlers for '[]' on the object which is very odd, as I have validation expressions outside the object. If someone could give a hand on why this is happening, that'd be great.

Implementation:

 class MxMongo                                                                                       
 -»»»# Initialise everything we need                                                                 
 -»»»def initialize(host : String, username : String, password : String, port : String)              
 -»»»-»»»@client = Nil                                                                               
 -»»»-»»»@database = "aggregation"                                                                   
 -»»»-»»»self.connect(host, username, password, port)                                                
 -»»»end                                                                                             
 -»»»                                                                                                
 -»»»# Connect to the database                                                                       
 -»»»def connect(host : String, username : String, password : String, port : String)                 
 -»»»-»»»@client = Mongo::Client.new "mongodb://#{username}:#{password}@#{host}:#{port}/#{@database}"
 -»»»-»»»return @client                                                                              
 -»»»end                                                                                             
                                                                                                     
 -»»»# Get an instance of the database                                                               
 -»»»def get_instance                                                                                
 -»»»-»»»if @client.not_nil! && db.not_nil!                                                          
 -»»»-»»»-»»»return @client                                                                          
 -»»»-»»»else                                                                                        
 -»»»-»»»-»»»return Nil                                                                              
 -»»»-»»»end                                                                                         
 -»»»end                                                                                             
                                                                                                     
 -»»»def get_collection(collection : String)                                                         
 -»»»-»»»if [email protected]? && !collection.nil?                                                        
 -»»»-»»»-»»»client = @client                                                                        
 -»»»-»»»-»»»return client.not_nil![collection] if collection.not_nil!                               
 -»»»-»»»else                                                                                        
 -»»»-»»»-»»»return Nil                                                                              
 -»»»-»»»end                                                                                         
 -»»»end                                                                                             
 end                                                                                                 

Error I recieve:

                                                                                                                      
test = mongo_client.get_collection("aggregation")                                                                     
                    ^~~~~~~~~~~~~~                                                                                    
                                                                                                                      
in classes/init_mongo.cr:27: undefined method '[]' for Nil:Class (compile-time type is (Mongo::Client | Nil:Class))   
                                                                                                                      
   return client.not_nil![collection] if collection.not_nil!                                                          
                         ^                                                                                            
                                                                                                                      
================================================================================                                      
                                                                                                                      
Nil:Class trace:                                                                                                      
                                                                                                                      
  /usr/lib/crystal/object.cr:156                                                                                      
                                                                                                                      
      def not_nil!                                                                                                    
          ^~~~~~~~                                                                                                    
                                                                                                                      
  /usr/lib/crystal/object.cr:157                                                                                      
                                                                                                                      
        self                                                                                                          
        ^~~~                                                                                                          


Apologies for the formatting, copied from vim

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions