2017-11-12 2 views
0

私は "User"オブジェクトが作成され、操作されるPython用のプログラムを作成しています。オブジェクトがNoneTypeであるというエラー?

私はUserオブジェクトを表すノードを持つオブジェクトのリンクリストを持っています。

Iユーザーオブジェクトとして変数xを設定、リストを検索して、返されたノードデータ、(例えばx = List.search("a username")ためsearchを検索名前のユーザーオブジェクトを返す場合)に等しい変数を設定することにより、私はUserを得ますこのx変数のUserクラスのメソッドを使用しようとすると、NoneTypeエラーが発生します。

この原因は何ですか?
xがオブジェクトUserに直接割り当てられている場合(つまりx = User())、前者の場合は機能しません。


コードの注意:.txtファイルからコマンドを受け取ります。これらのコマンドは、「追加」と「フレンド」です。

class Node (object): 

    def __init__(self,initdata): 
     self.data = initdata 
     self.next = None   # always do this – saves a lot 
            # of headaches later! 
    def getData (self): 
     return self.data   # returns a POINTER 

    def getNext (self): 
     return self.next   # returns a POINTER 

    def setData (self, newData): 
     self.data = newData   # changes a POINTER 

    def setNext (self,newNext): 
     self.next = newNext   # changes a POINTER 

class UnorderedList(): 

    def __init__(self): 
     self.head = None 

    def isEmpty (self): 
     return self.head == None 

    def add (self,item): 
     # add a new Node to the beginning of an existing list 
     temp = Node(item) 
     temp.setNext(self.head) 
     self.head = temp 

    def length (self): 
     current = self.head 
     count = 0 

     while current != None: 
     count += 1 
     current = current.getNext() 

     return count 

    def search (self,item): #NOW RETURNS OBJECT IN LIST 
     current = self.head 

     while current != None: 
     if current.getData().name == item: 
      return current.getData() 
     else: 
      current = current.getNext() 

     return None 

    def remove (self,item): 
     current = self.head 
     previous = None 
     found = False 

     while not found: 
     if current.getData() == item: 
      found = True 
     else: 
      previous = current 
      current = current.getNext() 

     if previous == None: 
     self.head = current.getNext() 
     else: 
     previous.setNext(current.getNext()) 

class User(): 

    def __init__(self): 
     self.name = "" 
     self.friendsList = UnorderedList() 

    def setName(self,info): 
     self.name = info 

    def getName(self): 
     return self.name 

    def removeFriend(self, item): 
     self.friendsList.remove(item) 

    def addFriend(self, item): 
     self.friendsList.add(item) 

    def searchList(self, item): 
     self.friendsList.search(item) 

    def __str__(self): 
     return self.name 

def main(): 

    inFile = open("FriendData.txt") 
    peopleList = UnorderedList() 

    for line in inFile: 
     textList = line.split() 

     if "Person" in textList: 

      newUser = User() 
      newUser.setName(textList[1]) 

      if peopleList.search(newUser.getName()) != None: 
       print("This user is already in the program") 

      else: 
       peopleList.add(newUser) 
       print(newUser.getName(),"now has an account") 

     elif "Friend" in textList: 
      #PROBLEM OBJECTS a AND b BELOW 
      a = peopleList.search(textList[1]) #returns user1 object 
      b = peopleList.search(textList[2]) # return user2 object 
      b.getName() 


      if peopleList.search(textList[1]) == None: 
       print("A person with the name", textList[1], "does not currently exist") 
      elif peopleList.search(textList[2]) == None: 
       print("A person with the name", textList[2], "does not currently exist") 
      elif textList[1]==textList[2]: 
       print("A person cannot friend him/herself") 
      elif peopleList.search(textList[1]).searchList(textList[2])!= None: 
       print(textList[1],"and",textList[2],"are already friends") 
      elif peopleList.search(textList[2]).searchList(textList[1]) != None: 
       print(textList[2],"and",textList[1],"are already friends") 
      #else: 
       #a.friendsList.add(b) #adds user 2 to user1 friendlist 
       #b.addFriend(a) 
       #print(a.getName(),"and",b.getName(),"are now friends") 

main() 

エラー:友達が別のユーザーのオブジェクトにLinkedListは、各ユーザーオブジェクトを追加し、その逆

がコード想定している

Traceback (most recent call last): 
    File "C:/Users/rsaen/Desktop/Python Prgms/Friends.py", line 137, in <module> 
    main() 
    File "C:/Users/rsaen/Desktop/Python Prgms/Friends.py", line 119, in main 
    b.getName() 
AttributeError: 'NoneType' object has no attribute 'getName' 

解析されたが、.txtファイル

['Person', 'Rachel'] 
['Person', 'Monica'] 
['Friend', 'Rachel', 'Monica'] 
['Friend', 'Monica', 'Rachel'] 
['Friend', 'Rachel', 'Ross'] 
['Person', 'Ross'] 
['Friend', 'Rachel', 'Ross'] 
['Person', 'Joey'] 
['Person', 'Joey'] 
['Friend', 'Joey', 'Joey'] 
['Friend', 'Joey', 'Rachel'] 
['Person', 'Chandler'] 
['Friend', 'Chandler', 'Monica'] 
['Friend', 'Chandler', 'Rachel'] 
['Friend', 'Ross', 'Chandler'] 
['Friend', 'Phoebe', 'Rachel'] 
['Person', 'Phoebe'] 
['Friend', 'Phoebe', 'Rachel'] 
['Exit'] 
+1

コードを表示.... –

+0

少し長くて少しスパゲッティですが、私はそれを示すことができます – user3491700

+0

長いコードを投稿せず、問題を示す特定の関連コードを投稿してください。そのコードが何であるかわからない場合は、実行するまで試してください。何百行ものコードを私たちにダンプして、それを掘り下げることを期待しないでください。 –

答えて

0

コードとエラーメッセージから、textList[2]がにないかのように見えます属性。実際、そうでないように見えます。 friendsList属性内の唯一のUsertextList[1]であると思われます。これは、friendsListが引数なしでインスタンス化され、後でtextList[1]が追加されるためです。

重要な情報:Pythonにはポインタはありません。詳細はこちらをご覧ください:https://ubuntuforums.org/showthread.php?t=895804また

重要なのは知っている:次のオブジェクトは、PythonでNoneであるかどうかをテストする方法です:

if x is not None: 
    print(x) 
+0

謝罪しますが、どのユーザーの「friendsList」を参照していますか? 'friendsList'構造体は、追加されるごとに各ユーザの友人を保持することになっていますが、aやb変数の' friendsList'構造体にアクセスしようとする試みもそうではないなぜなら、何らかの理由でNoneTypeと見なされているからです。 – user3491700

0

問題があり、そのご入力の5行目に、あなた人物ロスが追加される前に、レイチェルとロスの友情を加えてみてください。プログラムはRossを見つけることができません。変数bNoneで、b.getName()はクラッシュします。

あなたのUserクラスの検索メソッドでは、検索の結果を返す必要があります。私はあなたが割り当て(?)の一部であるかもしれないgetterとsetterを使用しているのも見ていますが、Pythonでは必要ありません。

+0

コメント内の提案のためにコードをスキャンし、 'UnorderedList'の' search'メソッドと何か関係があることを実感しました。 'current.getData()'の部分を返さず、aとbの両方が返されているので、 'return None'部分にスキップしているようです。NoneType – user3491700

+0

あなたは入力の問題ではないと確信していますか?私はあなたのコードをダウンロードし、デバッガでそれを踏んで、それは私のカスタム入力(彼らの間の友情の前にPersonを追加する限り)でうまくいきました。 – TAsk

+0

不思議なことに、私は単純に 'User'検索メソッドを変更して結果を返しました。ファイル内のすべての行について作業しています。非常に奇妙な – user3491700

関連する問題