3

having problems with this error in python:

File "F:\dykrstra", line 36, in route
while node.label != node.prevNode.label:
AttributeError: 'NoneType' object has no attribute 'label'

Inside this while loop:

 while node.label != node.prevNode.label:
    node = node.prevNode
    labels.append(node.label)

I think it relates to this:

   def __init__(self, label):
        self.label = label
        self.neighbours = []
        self.distances = []
        self.prevNode = None
        self.totalDistance = 0

I'm not sure why prevNode doesn't like the nothing being assigned to it, please help.

1
  • 1
    It is totally fine that you assign None to prevNode. But the value None has to attributes, so prevNode.label will give you an error. You can try it in the console, type None.label. Commented Mar 13, 2011 at 16:01

3 Answers 3

6

Your constructor sets self.prevNode to None, and later you try to access node.prevNode.label, which is like trying to access None.label. None doesn't have any attributes, so trying to access any will give you an AttributeError.

Sign up to request clarification or add additional context in comments.

Comments

0

As per the other answers (and the error message) you are accessing None.label. If it is expected that node might be None, then you will need to check for it before appending it.

while node.label != node.prevNode.label:
    node = node.prevNode
    if node is not None:
        labels.append(node.label)

Comments

0

Exception AttributeError happens when attribute of the object is not available. An attribute reference is a primary followed by a period and a name.

So basically you need to double check your object and the attribute name.

For example to return a list of valid attributes for that object, use dir():

dir(node)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.