# Getting null pointer exception error in class using PVector

**URL:** <https://discourse.processing.org/t/getting-null-pointer-exception-error-in-class-using-pvector/35932>\
**Category:** Beginners\
**Created:** [March 25, 2022, 3:36am UTC](https://discourse.processing.org/t/getting-null-pointer-exception-error-in-class-using-pvector/35932 "2022-03-25T03:36:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![animanoir](https://avatars.discourse-cdn.com/v4/letter/a/e495f1/32.png) [@animanoir](https://discourse.processing.org/u/animanoir)\
**Post date:** [March 25, 2022, 3:36am UTC](https://discourse.processing.org/t/getting-null-pointer-exception-error-in-class-using-pvector/35932/1 "2022-03-25T03:36:38Z")

</div>

I don’t understand why I’m getting NPE error. Processing points me to:

```auto
  Walker(PVector _location) {
    location.set(_location);
  }

```

Here is all my code:

```auto
PVector location;
PVector velocity;

Walker walker1;

void setup() {
  location = new PVector(random(width), random(height));
  velocity = new PVector(5, 5);
  walker1 = new Walker(location);
  walker1.display();
  size(800, 800);
}

void draw() {
  background(0);
  
  walker1.move(velocity);
}

class Walker {
  PVector location, velocity;

  Walker(PVector _location) {
    location.set(_location);
  }

  void display() {
    stroke(0);
    point(location.x, location.y);
  }
  
  void move(PVector v) {
    location.x = location.x + v.x;
    location.x = location.x + v.x;
  }
}

```

---

<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:** [March 25, 2022, 3:42am UTC](https://discourse.processing.org/t/getting-null-pointer-exception-error-in-class-using-pvector/35932/2 "2022-03-25T03:42:07Z")

</div>

> [@animanoir](#):
>
> `PVector location, velocity;`

`final PVector location = new PVector(), velocity = new PVector();`

---

<div class="post-metadata">

**Author:** ![animanoir](https://avatars.discourse-cdn.com/v4/letter/a/e495f1/32.png) [@animanoir](https://discourse.processing.org/u/animanoir)\
**Post date:** [March 25, 2022, 4:01am UTC](https://discourse.processing.org/t/getting-null-pointer-exception-error-in-class-using-pvector/35932/3 "2022-03-25T04:01:21Z")

</div>

That worked, thanks. So what’s the deal here? why use `final`?

---

<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:** [March 25, 2022, 5:49am UTC](https://discourse.processing.org/t/getting-null-pointer-exception-error-in-class-using-pvector/35932/4 "2022-03-25T05:49:29Z")

</div>

> [@animanoir](#):
>
> So what’s the deal here?

Java’s non-primitive datatypes, like String and PVector, defaults to `null` when we declare a variable:

> **[null / Reference](https://processing.org/reference/null.html)**
>
> Special value used to signify the target is not a valid data element. In Processing, you may run across the keyword null when trying to access data which is not there.

So before using a non-primitive variable, for example invoking any of its methods as you did at `location.set(_location);`, we have to make sure we’ve already initialized it.

That’s why I’ve assigned a PVector to _location_: `final PVector location = new PVector();`

> [@animanoir](#):
>
> Why use `final`?

Just to make sure those variables can’t be unintentionally re-assigned later on:

> **[final / Reference](https://processing.org/reference/final.html)**
>
> Keyword used to state that a value, class, or method can't be changed. If the final keyword is used to define a variable, the variable can't be changed within the program. When used to define a…

Its usage is completely optional, of course.
