'how to fix attribute error : 'None type' object has no attribute next

When I run this I am getting the following error in while loop:

Attribute error: 'None type ' has no attributes 'next'

# Method lo delete a node at a particular position
def deleteatpos(self, pos):
    count = 0
    currentnode = self.head
    previousnode = self.head
    if pos > self.length or pos < 0:
        print("The position does not exist. Please enter a valid position")
    else:
        while currentnode.next != None or count < pos:
            count = count + 1
            if count == pos + 1:
                previousnode.next = currentnode.next # or 
                previousnode.setNext(currentnode.getNext)
                currentnode.next.previous = previousnode # or 
                currentnode.getNext.setprevious(previous)
                self.length -= 1
                return
            else:
                previousnode = currentnode
                currentnode = currentnode.next


Solution 1:[1]

The meaning of this error is probably you are using a non defined property of a object. for example if have a node object that has three property let say val, next, previous. Now if you are trying to access a property that is not there in the nodes property that will throw an error stating that there is no attribute.

Now coming to your question, according to the error, your node does not have a property called next, so cant access it. for example you can not use node.next. for that you to define it first.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ashok Bhobhiya