# Static reference to the non-static method

**URL:** <https://discourse.processing.org/t/static-reference-to-the-non-static-method/37711>\
**Category:** Beginners\
**Created:** [June 25, 2022, 12:55pm UTC](https://discourse.processing.org/t/static-reference-to-the-non-static-method/37711 "2022-06-25T12:55:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![PixelMx](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/pixelmx/32/4478_2.png) [@PixelMx](https://discourse.processing.org/u/PixelMx)\
**Post date:** [June 25, 2022, 12:55pm UTC](https://discourse.processing.org/t/static-reference-to-the-non-static-method/37711/1 "2022-06-25T12:55:15Z")

</div>

Hi, I’ve been reading about The Salesman Problem and I’ve seen TSP art, which I’m interested in creating.

I found an existing sketch in [gihub](https://github.com/kundan989/TSP-using-Processing/blob/master/Travelling_Salesman_Problem/Travelling_Salesman_Problem.pde) and I’m trying to adapt it to randomly read pixels from a black and white image and if the pixel is black it would add it to the “cities” array.

I’m getting the following error:  
**“Cannot make a static reference to the non-static method add(PVector) from the type PVector”**  
Is there any other way to add an x/y value to a PVector?

Thanks in advance.

```auto

PVector[] cities;
int no_of_cities=100;
PImage img;
int xc, yc;
color c;
float recorddistance;
PVector[] bestever;

void setup()
{
  size(1024,767);
  
  img = loadImage("test.png");
  //image(img, 0, 0);
  cities = new PVector[no_of_cities];
  bestever = new PVector[no_of_cities];
  for(int i=0; i<no_of_cities; )
  {
    xc=int(random(width));
    yc=int(random(height));
    
    c = img.get(xc,yc);
    println(xc,yc,brightness(c),i);
    PVector v = new PVector(xc, yc); //create points at random position
      if (brightness(c) < 10){
        cities = PVector.add(v); //and store it in an array
        i++;
        }  
 
}
//print(cities); 
 }
 
 void draw()
{
  
  background(255);
  for(int i=0; i<no_of_cities-1; i++)
  {
    ellipse(cities[i].x,cities[i].y,4,4); //plot the points
  }
}

```

---

<div class="post-metadata">

**Author:** ![PhySan](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/physan/32/16087_2.png) [@PhySan](https://discourse.processing.org/u/PhySan)\
**Post date:** [June 25, 2022, 1:22pm UTC](https://discourse.processing.org/t/static-reference-to-the-non-static-method/37711/2 "2022-06-25T13:22:52Z")

</div>

Hi @PixelMx,

I think the add() method adds the vector component, not the vector to the array:

> **[Reference](https://processing.org/reference/PVector_add_.html)**
>
> Adds x, y, and z components to a vector, adds one vector to another, or adds two independent vectors together. The version of the method that adds two vectors together is a static method and returns a…

Instead of an array, why not use an arrayList?

> **[Reference](https://processing.org/reference/ArrayList.html)**
>
> An ArrayList stores a variable number of objects. This is similar to making an array of objects, but with an ArrayList, items can be easily added and removed from the ArrayList and it is…

That way you can append/add elements to the arrayList. A small example is found below:

> **[Reference](https://processing.org/reference/ArrayList.html)**
>
> An ArrayList stores a variable number of objects. This is similar to making an array of objects, but with an ArrayList, items can be easily added and removed from the ArrayList and it is…

So your sketch would become something like this:

```auto
ArrayList<PVector> cities;
int no_of_cities=100;
PImage img;
int xc, yc;
color c;
float recorddistance;
PVector[] bestever;

void setup()
{
  size(1024, 767);

  img = loadImage("test.png");
  //image(img, 0, 0);
  cities = new ArrayList<PVector>(no_of_cities);
  bestever = new PVector[no_of_cities];
  for (int i=0; i<no_of_cities; )
  {
    xc=int(random(width));
    yc=int(random(height));

    c = img.get(xc, yc);
    println(xc, yc, brightness(c), i);
    PVector v = new PVector(xc, yc); //create points at random position
    if (brightness(c) < 10) {
      cities.add(v); //and store it in an array
      i++;
    }
  }
  //print(cities);
}

void draw()
{

  background(255);
  for (int i=0; i<no_of_cities-1; i++)
  {
    ellipse(cities.get(i).x, cities.get(i).y, 4, 4); //plot the points
  }
}

```

I think that you can still improve your sketch using pixels to retrieve all pixels of the image and then randomly select which ones you want 🙂

> **[PImage::pixels\[\] / Reference](https://processing.org/reference/PImage_pixels.html)**
>
> The pixels\[\] array contains the values for all the pixels in the image. These values are of the color datatype. This array is the size of the image, meaning if the image is 100 x 100 pixels, th…

Hope it helps! 🙂

---

<div class="post-metadata">

**Author:** ![PixelMx](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/pixelmx/32/4478_2.png) [@PixelMx](https://discourse.processing.org/u/PixelMx)\
**Post date:** [June 25, 2022, 2:32pm UTC](https://discourse.processing.org/t/static-reference-to-the-non-static-method/37711/3 "2022-06-25T14:32:49Z")

</div>

> [@PhySan](#):
>
> `cities.get(i).x, cities.get(i).y, 4, 4`

Thanks a lot @PhySan. This worked perfectly!
