# NoneType error in Python Processing

**URL:** <https://discourse.processing.org/t/nonetype-error-in-python-processing/8579>\
**Category:** Processing.py\
**Created:** [February 21, 2019, 12:11am UTC](https://discourse.processing.org/t/nonetype-error-in-python-processing/8579 "2019-02-21T00:11:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Livaarleen](https://avatars.discourse-cdn.com/v4/letter/l/7c8e57/32.png) [@Livaarleen](https://discourse.processing.org/u/Livaarleen)\
**Post date:** [February 21, 2019, 12:11am UTC](https://discourse.processing.org/t/nonetype-error-in-python-processing/8579/1 "2019-02-21T00:11:58Z")

</div>

Hi. I am completely new to Processing, and know nothing of Java, but I am fairly experienced with Python, so I have been writing some code with the Python version of Processing. I am writing a code that moves particles around according to the values of pixels in an image, and ran into errors whenever I tried to run it. I would get NoneType errors on the variable I created to hold an image. Here is my code.

```auto
image_name = "image.jpg"
particles_number = 100
picture = loadImage(image_name)
magnetic_constant = 255

import math
def setup():
    background(255)
    stroke(0)
    picture = loadImage(image_name)
    size(512 * 2 + 2, 512)
    image(picture, 0, 0)
    create_particles(particles_number)
    draw_particles(picture.width)
    

particles_list = []

def closest_particle(particle_index):
    current_distance = 100
    for i in range(0, particles_number + 1):
        if i != particle_index:
            x_off = (particles_list[particle_index].location[0] - particles_list[i].location[0])
            y_off = (particles_list[particle_index].location[1] - particles_list[i].location[1])
            if x_off **2 + y_off** 2 < current_distance:
                current_closest = i
    return current_closest, x_off, y_off

def move_particle(particle_index):
    closest, x_off, y_off = closest_particle(particle_index)
    direction = math.floor(y_off /( x_off +0.1) * 8)
    closest = particles_list[closest]
    particle = particles_list[particle_index]
    if x_off + y_off == 0:
        particle.location = [particle.location[0] + int(random(-2, 2)), particle.location[1] + int(random(-2, 2))]
    else:
        magnitude = int(closest.charge / (x_off + y_off))
        if direction == 0:
            particle.location[0] += magnitude
        elif direction == 1:
            offset = int(magnitude / 1.41)
            particle.location[0] += offset
            particle.location[1] += offset
        elif direction == 2:
            particle.location[1] += magnitude
        elif direction == 3:
            offset = int(magnitude / 1.41)
            particle.location[0] -= offset
            particle.location[1] += offset
        elif direction == 4:
            particle.location[0] -= magnitude
        elif direction == 5:
            offset = int(magnitude / 1.41)
            particle.location[0] -= offset
            particle.location[1] -= offset
        elif direction == 6:
            particle.location[1] -= magnitude
        elif direction == 7:
            offset = int(magnitude / 1.41)
            particle.location[0] += offset
            particle.location[1] -= offset        
    change_charge(particle)            
            
def change_charge(particle):
    x = particle.location[0]
    y = particle.location[1]
    picture.loadPixels()
    pix = picture.pixels(index(x, y))
    gray = 0.21 * red(pix) + 0.72 * green(pix) + 0.07 * blue(pix)
    charge = gray / magnetic_constant
    picture.updatePixels()
    
    
def index(x, y):
    return x * picture.width + y    
                
            
            
def move_particles():
    for i in range(0, particles_number + 1):
        move_particle(i)
        
def draw_particles(image_width):
    for i in particles_list:
        point(i.location[0] + image_width + 2., i.location[1])
    
    
                           

    
class particle():
    def __init__ (self, location, charge, ID):
        self.location = location
        self.charge = charge
        self.ID = ID

def create_particles(n):
    for i in range(0, n + 1):
        new_particle = particle([math.floor(i / 512), i % 512], 1, i)
        particles_list.append(new_particle)
        
def draw():
    move_particles()
    draw_particles(picture.width)        

```

What can I do fix this? Am I doing something stupid?

---

<div class="post-metadata">

**Author:** ![tabreturn](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tabreturn/32/3697_2.png) [@tabreturn](https://discourse.processing.org/u/tabreturn)\
**Post date:** [February 21, 2019, 5:33am UTC](https://discourse.processing.org/t/nonetype-error-in-python-processing/8579/2 "2019-02-21T05:33:59Z")

</div>

By “variable I created to hold an image”, do you mean the `picture` variable? If so, I think I’ve solved your NoneType error … but then ran into other types of errors. Perhaps this will get you moving forward?

```auto
image_name = "image.png"
particles_number = 100
magnetic_constant = 255

import math
def setup():
    global picture
    background(255)
    stroke(0)
    picture = loadImage(image_name)
    image(picture, 0, 0)
    size(512 * 2 + 2, 512)
    #create_particles(particles_number)
    #draw_particles(picture.width)
    
'''
...

'''

def draw():
    #move_particles()
    #draw_particles(picture.width)
    print(picture.width)

```

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [February 21, 2019, 5:55am UTC](https://discourse.processing.org/t/nonetype-error-in-python-processing/8579/3 "2019-02-21T05:55:10Z")

</div>

> [@\[SOLVED\] Image() nullPtrException](https://discourse.processing.org/t/solved-image-nullptrexception/7424/4):
>
> def setup(): size(500, 500) noLoop() global img img = loadImage('defect\_walk0001.png') def draw(): background(0) image(img, 100, 100)

---

<div class="post-metadata">

**Author:** ![Livaarleen](https://avatars.discourse-cdn.com/v4/letter/l/7c8e57/32.png) [@Livaarleen](https://discourse.processing.org/u/Livaarleen)\
**Post date:** [February 22, 2019, 2:40am UTC](https://discourse.processing.org/t/nonetype-error-in-python-processing/8579/4 "2019-02-22T02:40:18Z")

</div>

Thank you for your help!
