Null Pointer Exception problem (sorry if this is a repeat of something)
in
Programming Questions
•
5 months ago
Hey I'm new here. I'm a bit new with Processing, and I'm trying to figure out this one issue. The program is suppose to create a dragon fractal image from an array, but when I enter into a certain function it calls "NullPointerException" on me. Here's my code:
// this is in a separate tab if that changes anything
class Point {
float x, y;
Point (float x_, float y_) {
x = x_;
y = y_;
}
}
Point rotate_point (Point r, Point p, float angle) {
Point old = new Point (p.x - r.x, p.y - r.y);
Point newp = new Point (old.x * cos (angle) - old.y * sin (angle) + r.x, old.x * sin (angle) + old.y * cos (angle) + r.y);
return newp;
}
Point [] dragon (Point [] p, float angle, int current, int depth) {
if (!(current > depth)) {
Point [] newp = new Point [p.length * 2 - 1];
for (int i = 0; i < p.length; i++) {
newp [i] = p [i];
}
for (int i = p.length + 1; i < newp.length; i++) {
newp [i] = rotate_point (p [p.length - 1], p [i - p.length - 1], angle);
}
return dragon (newp, angle, current + 1, depth);
}
else {
return p;
}
}
void draw_dragon (Point [] d) {
for (int i = 0; i < d.length - 1; i++) {
line (d [i].x, d [i].y, d [i + 1].x, d [i + 1].y);
}
}
Point [] p;
void setup () {
size (200, 200);
p = new Point [2];
p [0] = new Point (100, 100);
p [1] = new Point (105, 100);
p = dragon (p, HALF_PI, 0, 3);
draw_dragon (p);
}
void draw () {
}
I get an error in my function rotate (), and it has to do with the pointer r. If someone could help that would be great.
1