We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › NullPointerException - don´t know why
Page Index Toggle Pages: 1
NullPointerException - don´t know why (Read 1480 times)
NullPointerException - don´t know why
Sep 5th, 2009, 5:59am
 
so here i am again shooting for the stars of the processing minds
my error:

NullPointerException

~Exception in thread "Animation Thread" java.lang.NullPointerException
     at Follow3$Snake.dragSegment(Follow3.java:147)
     at Follow3$Snake.draw(Follow3.java:138)
     at Follow3.draw(Follow3.java:84)
     at processing.core.PApplet.handleDraw(PApplet.java:1423)
     at processing.core.PApplet.run(PApplet.java:1328)
     at java.lang.Thread.run(Unknown Source)

the code:
Code:

/**
* Follow 3. *
* Based on code from Keith Peters
*
* A segmented line follows the mouse. The relative angle from
* each segment to the next is calculated with atan2() and the
* position of the next is calculated with sin() and cos().
*/

int CobrasCont = 3;//---------------------NUMERO DE COBRAS

int snLength;
float segLength = 15;
PFont fontA;

float xpos, ypos; // Starting position of shape

float xspeed = 2.8; // Speed of the shape
float yspeed = 2.2; // Speed of the shape

int xdirection = 1; // Left or Right
int ydirection = 1; // Top to Bottom
int size = 10;
String[] lines;
float[] xposition;
float[] yposition;




Snake SnakeSN;
Snake SnakeSN1;

void setup() {
lines = loadStrings("dialogos.txt");
fontA = loadFont("AmericanTypewriter-24.vlw");
textFont(fontA, 30);
size(1200, 400);
smooth();
strokeWeight(5);
stroke(0, 100);

xpos = width/2;
ypos = height/2;


}



void draw() {
background(50);
xpos = xpos + ( xspeed * xdirection );
ypos = ypos + ( yspeed * ydirection );
if (xpos > width-size || xpos < 0) {
xdirection *= -1;
}
if (ypos > height-size || ypos < 0) {
ydirection *= -1;
}

//dragSegment(0, xpos+size/2, ypos+size/2, " " );

// dragSegment(0, mouseX, mouseY, " " );
String texto = "d'AS Entranhas Apresentam GLORIA e muita pancada";
SnakeSN = new Snake(0,(xpos+size/2),(ypos+size/2),49,"d'AS Entranhas Apresentam GLORIA e muita pancada");
SnakeSN1 = new Snake();
SnakeSN.draw();
SnakeSN1.draw();


}





the class Snake:

Code:
class Snake {
int b;
float xFinal;
float yFinal;
int snLength;
String texto;


Snake(){
b = 0;
xFinal = mouseX;
yFinal = mouseY;
snLength = 20;
texto = "dopedope21";
}

// Contructor (required)
Snake(int id, float xMain, float yMain, int snLength2, String textMain) {
b = id;
xFinal=xMain;
yFinal=yMain;
texto=textMain;
snLength = snLength2;


}







void draw (){

float[]xposition = new float[snLength];
float[]yposition = new float[snLength];

String [] textoArray = new String [texto.length()];
for (int i = 1 ; i <= texto.length(); i++){
String ss3 = texto.substring(i-1,i); // Returns "CC"
textoArray[i-1]=ss3;
}
textoArray= reverse(textoArray);
for(int i=0; i<snLength-1; i++) {
dragSegment(i+1, xposition[i], yposition[i], textoArray[i]);
println (snLength);
}

println (xposition.length);
ellipse (xFinal,yFinal, snLength,snLength);
}

void dragSegment(int i, float xin, float yin, String textoLetra) {
float dx = xin - xposition[i];
float dy = yin - yposition[i];
float angle = atan2(dy, dx);
xposition[i] = xin - cos(angle) * segLength;
yposition[i] = yin - sin(angle) * segLength;
segment(xposition[i], yposition[i], angle,textoLetra);

}

void segment(float x, float y, float a, String textoLetra) {
pushMatrix();
translate(x, y);
rotate(a);
line(0, 0, segLength, 0);
text (textoLetra,0,0);
popMatrix();
}


}




the idea is basically make sentences that follow the movement.

thanks B.
Re: NullPointerException - don´t know why
Reply #1 - Sep 5th, 2009, 8:32am
 
you're defining xposition and yposition in Snake.draw() which makes it local to that method

 void draw (){
  float[]xposition = new float[snLength];
  float[]yposition = new float[snLength];

when you try and use them in snake.drawSegment():

 void dragSegment(int i, float xin, float yin, String textoLetra) {
   float dx = xin - xposition[i];
   float dy = yin - yposition[i];

it uses the version you've defined globally at the top there:

float[] xposition;
float[] yposition;

and which aren't actually allocated.

get rid of the float[] before xpositon and yposition in Snake.draw()

(edit. actually, this won't work - you want three snakes with their own xposition and yposition arrays so get rid of the global definition and add it to the snake.)

you've also got snLength defined globally and within Snake. which do you really need?
Re: NullPointerException - don´t know why
Reply #2 - Sep 5th, 2009, 8:36am
 
also this

 Snake(){
   b = 0;
   xFinal = mouseX;
   yFinal = mouseY;
   snLength = 20;
   texto = "dopedope21";
 }

doesn't work as snLength != length of 'dopedope21' so you end up with an array index out of bounds error. try using texto.size().
Re: NullPointerException - don´t know why
Reply #3 - Sep 5th, 2009, 11:12am
 
yes i removed the snLength and replaced it with the texto.length() but still no go.
where do i put the
float[]xposition = new float[texto.length()];
  float[]yposition = new float[texto.length()];
?
Re: NullPointerException - don´t know why
Reply #4 - Sep 5th, 2009, 12:59pm
 
damm still no go!

Code:
/**
* Follow 3.
* Based on code from Keith Peters
*
* A segmented line follows the mouse. The relative angle from
* each segment to the next is calculated with atan2() and the
* position of the next is calculated with sin() and cos().
*/

int CobrasCont = 3;//---------------------NUMERO DE COBRAS

float segLength = 15;
PFont fontA;

float xpos, ypos; // Starting position of shape

float xspeed = 2.8; // Speed of the shape
float yspeed = 2.2; // Speed of the shape

int xdirection = 1; // Left or Right
int ydirection = 1; // Top to Bottom
int size = 10;
String[] lines;




Snake SnakeSN;
Snake SnakeSN1;
Snake SnakeSN2;

void setup() {
lines = loadStrings("dialogos.txt");
fontA = loadFont("AmericanTypewriter-24.vlw");
textFont(fontA, 30);
size(1200, 400);
smooth();
strokeWeight(5);
stroke(0, 100);

xpos = width/2;
ypos = height/2;


}



void draw() {
background(50);
xpos = xpos + ( xspeed * xdirection );
ypos = ypos + ( yspeed * ydirection );
if (xpos > width-size || xpos < 0) {
xdirection *= -1;
}
if (ypos > height-size || ypos < 0) {
ydirection *= -1;
}

//dragSegment(0, xpos+size/2, ypos+size/2, " " );

// dragSegment(0, mouseX, mouseY, " " );
String texto = "d'AS Entranhas Apresentam GLORIA e muita pancada";
SnakeSN = new Snake(0,(xpos+size/2),(ypos+size/2),"d'AS Entranhas Apresentam GLORIA e muita pancada");
//SnakeSN1 = new Snake();
//SnakeSN2 = new Snake(0,mouseX,mouseY,49,"LALALALALALL");
SnakeSN.draw();
//SnakeSN1.draw();


}






snake:

Code:
class Snake {
int b;
float xFinal;
float yFinal;
String texto;


Snake(){
b = 0;
xFinal = mouseX;
yFinal = mouseY;
texto = "dopedope21";
}

// Contructor (required)
Snake(int id, float xMain, float yMain, String textMain) {
b = id;
xFinal=xMain;
yFinal=yMain;
texto=textMain;


}

void draw(){
float[]xposition;
float[]yposition;


float segLength = 15;
String [] textoArray = new String [texto.length()];
for (int i = 1 ; i <= texto.length(); i++){
String ss3 = texto.substring(i-1,i); // Returns "CC"
textoArray[i-1]=ss3;
}
textoArray= reverse(textoArray);
for(int i=0; i<(texto.length()-1); i++) {


dragSegment(i+1, xposition[i], yposition[i], textoArray[i]);
println (texto.length());
}
dragSegment(0, xFinal, yFinal,texto );
println (xposition.length);
ellipse (xFinal,yFinal, texto.length(),texto.length());
}

void dragSegment(int i, float xin, float yin, String textoLetra) {
float[]xposition = new float[texto.length()];
float[]yposition = new float[texto.length()];

float dx = xin - xposition[i];
float dy = yin - yposition[i];
float angle = atan2(dy, dx);
xposition[i] = xin - cos(angle) * segLength;
yposition[i] = yin - sin(angle) * segLength;
segment(xposition[i], yposition[i], angle,textoLetra);

}

void segment(float x, float y, float a, String textoLetra) {
pushMatrix();
translate(x, y);
rotate(a);
line(0, 0, segLength, 0);
text (textoLetra,0,0);
popMatrix();
}


}







ERROR:
The local variable xposition may not been initialized
please help :P
Re: NullPointerException - don´t know why
Reply #5 - Sep 6th, 2009, 2:31am
 
When reporting an error, it is better to indicate on which line you got it: you have several xpostion local variables! I suppose it is the one in draw().

But we can fix that by answering your previous question...
When you share a variable between several functions, you can declare them global (or pass them as parameter, but it is not possible here), ie. define them before setup(). Then remove all declarations inside the functions.

Mmm, you have to declare texto at the same level, so you can use its size in the initialization.
Re: NullPointerException - don´t know why
Reply #6 - Sep 6th, 2009, 4:27am
 
> yes i removed the snLength and replaced it with the texto.length() but still no go.

that wasn't quite what i meant.

rather than

snlength = 20;
texto = "whatever";

where you mean for snlength to be equal to the length of the text then it's better to write

texto = "something";
snlength = texto.size();

because that way they'll never get out of step like yours were. and having texto.size() everywhere might be a performance disaster - lots of recalculation.

i'll have a look at the rest of it later but i'm not entirely sure what it's meant to be doing so it's hard to fix. plus there's a lot of unused stuff in there making things muddy - lines[] isn't used, CobrasCont isn't used. segLength is defined twice. xposition is defined twice. lines are commented out...
Re: NullPointerException - don´t know why
Reply #7 - Sep 6th, 2009, 5:50am
 
thanks koogy and phil
i 'll work on it tonight.
Re: NullPointerException - don´t know why
Reply #8 - Sep 6th, 2009, 10:56am
 
ok, i went back to the original

http://processing.org/learning/topics/follow3.html

which cleared a few things up.

given that you wanted 3 snakes it didn't make sense to have them all following the mouse, so i had 1 follow the mouse and 1 follow the bouncing ball thing you had going on.

i can't decide what finalX and finalY is for - it's not in the original - so i've ignored it 8)

have used String.toCharArray() to split the string into characters and changed segment() to take a single char

have taken out a lot of stuff - you were new() this and new() that all over the place which was a lot of extra work. you need to read up on that and on variable scope.

anyway:
Code:

/**
* Follow 3.
* Based on code from Keith Peters
*
* A segmented line follows the mouse. The relative angle from
* each segment to the next is calculated with atan2() and the
* position of the next is calculated with sin() and cos().
*/

PFont fontA;

// bouncing ball
float xpos, ypos; // Starting position of shape
float xspeed = 2.8; // Speed of the shape
float yspeed = 2.2; // Speed of the shape
int xdirection = 1; // Left or Right
int ydirection = 1; // Top to Bottom

int radius = 10; // renamed from size which clashes with size() method name
//String[] lines; // unused

Snake SnakeSN1;
Snake SnakeSN2;

void setup() {
size(1200, 400);

// lines aren't used
// lines = loadStrings("dialogos.txt");

fontA = loadFont("AmericanTypewriter-24.vlw");
textFont(fontA, 30);
smooth();
strokeWeight(5);
stroke(0, 100);

xpos = width/2;
ypos = height/2;

SnakeSN1 = new Snake(); // use default constructor
// SnakeSN1 = new Snake(0, (xpos + radius / 2), (ypos + radius / 2), "String1");
SnakeSN2 = new Snake(0, (xpos + radius / 2), (ypos + radius / 2), "String2"); // use other constructor
}

void draw() {
background(50);

// update bouncing ball
xpos = xpos + ( xspeed * xdirection );
ypos = ypos + ( yspeed * ydirection );
if (xpos > width - radius || xpos < 0) {
xdirection *= -1;
}
if (ypos > height - radius || ypos < 0) {
ydirection *= -1;
}
// show current bouncer position
ellipse(xpos, ypos, radius, radius);

// draw snakes
SnakeSN1.draw(mouseX, mouseY); // first snake follows mouse
SnakeSN2.draw(xpos, ypos); // second snake follows bouncing ball
}

class Snake {
int b; // unused
float xFinal; // don't know
float yFinal; // don't know
// String texto; // text - not needed - just use textoArray instead
// segment positions
float[] xPosition;
float[] yPosition;
int snLength;
float segLength = 15; // letter spacing
char[] textoArray; // letters

Snake() {
// pass defaults to 'main' constructor
this(0, mouseX, mouseY, "dopedope21");
}

// Contructor - all this is done only once
Snake(int id, float xMain, float yMain, String textIn) {
b = id;
xFinal = xMain; // don't know what these are for
yFinal = yMain;
snLength = textIn.length();
println("snLength: " + snLength);

// create positions arrays
xPosition = new float[snLength];
yPosition = new float[snLength];

// toCharArray will split string into characters
textoArray = textIn.toCharArray();
textoArray = reverse(textoArray);
}

// draw this snake
void draw(float xPos, float yPos){
// first segment follows passed in values (mouse or ball)
dragSegment(0, xPos, yPos, textoArray[0]);
// other segments follow the previous segment
for(int i = 1; i < snLength; i++) {
dragSegment(i, xPosition[i - 1], yPosition[i - 1], textoArray[i]);
}
ellipse (xFinal, yFinal, snLength, snLength); // ??? what is this? why snlength?
}

void dragSegment(int i, float xin, float yin, char textoLetra) {
// dx and dy are differences from the current piece to the passed in values
// (which are either the mouse, ball or the previous segment)
float dx = xin - xPosition[i];
float dy = yin - yPosition[i];
float angle = atan2(dy, dx);
xPosition[i] = xin - cos(angle) * segLength;
yPosition[i] = yin - sin(angle) * segLength;
segment(xPosition[i], yPosition[i], angle, textoLetra);
}

// draw a single letter
void segment(float x, float y, float a, char textoLetra) {
pushMatrix();
translate(x, y);
rotate(a);
line(0, 0, segLength, 0);
text(textoLetra, 0, 0);
popMatrix();
}
}
Re: NullPointerException - don´t know why
Reply #9 - Sep 7th, 2009, 3:56am
 
thanks a lot koogy i am not an expert on the matter, but i am trying to learn - the hard way i guess
Page Index Toggle Pages: 1