0

I am having a very simple code of python, wherein I am creating an object array in the constructor, But whenever I try calling a display function to see all the objects in the Object Array, it returns this NoneType exception.

Class called Toy is the actual object class, which is shown below:

class Toy:
__name=None
__cost=None

def __init__(self,name,cost):
 self.__name=name
 self.__cost=cost

def get_Name(self):
 return self.__name

def get_Cost(self):
 return self.__cost 

def print_Toy(self):
 print(" Name of the toy: ",self.__name)
 print(" Cost of the toy: ",self.__cost)

The below-shown class Toy_Bag contains the object array, which I am initializing in the constructor.

from Toy import Toy

class Toy_Bag:
__toys=[None]

def __init__(self, no_of_toys):
 self.__create_Toy_bag(no_of_toys)

def __create_Toy_bag(self, no_of_toys):
 name,cost= None,0
 for toy in range(0, no_of_toys):
  print("\n Enter the name of the toy: ",end="")
  name=input()
  print("\n Enter the cost of the toy: ",end="")
  cost=int(input())
  toy=Toy(name,cost)
  self.__toys.append(toy)
 self.print_Toy_Bag() 

def print_Toy_Bag(self):
 for toy in self.__toys:
  toy.print_Toy()

enter image description here

Traceback (most recent call last):
File "Main.py", line 9, in <module>
toy_bag=Toy_Bag(3)
File "C:\Users\SONY\Desktop\Python Scripts\Tools\Toy_Bag.py", line 8, in __init__
self.__create_Toy_bag(no_of_toys)
File "C:\Users\SONY\Desktop\Python Scripts\Tools\Toy_Bag.py", line 19, in __create_Toy_bag
self.print_Toy_Bag()
File "C:\Users\SONY\Desktop\Python Scripts\Tools\Toy_Bag.py", line 23, in print_Toy_Bag
toy.print_Toy()
AttributeError: 'NoneType' object has no attribute 'print_Toy'

C:\Users\SONY\Desktop\Python Scripts\Tools>

Any help is highly appreciated.

Abdul Tayyeb
  • 157
  • 1
  • 2
  • 13
  • 1
    Possible duplicate of [Python: Attribute Error - 'NoneType' object has no attribute 'something'](https://stackoverflow.com/questions/8949252/python-attribute-error-nonetype-object-has-no-attribute-something) – Patrick Artner Jul 28 '18 at 16:42
  • Please copy-paste the error: images are hard to search, and hard to understand for searches. – 9769953 Jul 28 '18 at 16:42
  • You should use a single underscore for methods that are "hidden" – roganjosh Jul 28 '18 at 16:47

1 Answers1

2

You are initializing your list with one element of None right here:

class Toy_Bag:
    __toys=[None]

then you add more toys inside def __create_Toy_bag(self, no_of_toys) - will be at position 1 to n inside your list. The first stays None.

If you print your __toys you call a method of your Toy-class with this first None.

Change it to

__toys=[]

so

def print_Toy_Bag(self):
    for toy in self.__toys:  # first element is no longer None
        toy.print_Toy()

does no longer get the first element as None.


You might want to give How to debug small programs (#1) a read -it helps you debug your own programs. If you are interested in how to name stuff "correctly": PEP-008: Style Guide is a great read as well (spacing, private members and a lot of other stuff covered in it).

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69