Null pointer exception at a false line?
in
Core Library Questions
•
1 years ago
What I'm trying to do is some sort of baton with an accelerometer plugged to a Wiring S board. Processing would pretty much do the Graphic part, a sine wave to be precise, according to the accelerometer's X and Y axis values. However, when I try to run the Processing program, I get a nullPointerException, at line 41:
java.lang.NullPointerException
at processingCiclo2.<init>(processingCiclo2.java:41)
I tried moving the line by putting line breaks above it, but it keeps displaying the same line, and I don't know what to do.
Here's the code.
import processing.serial.*;
import ddf.minim.*;
import ddf.minim.signals.*;
import ddf.minim.analysis.*;
import ddf.minim.effects.*;
Serial myPort;
Minim minim;
AudioPlayer baseLoop;
AudioPlayer player2;
int linefeed = 10;
int numSensors = 2;
int sensors[];
int x1 = sensors[0];
int y1 = sensors[1];
float p; //Promedio entre valores x, -y-,
float theta = 0.0; // Start angle at 0
float amplitude = 75.0; // Height of wave
int xspacing = 8; // How far apart should each horizontal location be spaced
int w; // Width of entire wave
float period = 500.0; // How many pixels before the wave repeats
float dx; // Value for incrementing X, a function of period and xspacing
float[] yvalues; // Using an array to store height values for the wave
void setup()
{
size (800, 800);
println(Serial.list());
myPort = new Serial (this, Serial.list()[0], 9600);
minim = new Minim (this);
baseLoop=minim.loadFile ("Base (fast).mp3", 2048);
baseLoop.loop();
//player2=minim.loadFile (".mp4", 2048);
frameRate(45);
colorMode(RGB, 255, 255, 255, 100);
smooth();
w = width+16;
amplitude= y1;
dx = ((TWO_PI+x1) / (amplitude+y1)) * xspacing;
yvalues = new float[w/xspacing];
}
void draw()
{
if (sensors!=null)
{
background(255);
calcWave();
renderWave();
}
}
void calcWave()
{
// Incremento de teta, sumado al promedio entre los valores de x, y.
p= (x1+y1)/2;
theta += p;
// For every x value, calculate a y value with sine function
float x = theta;
for (int i = 0; i < yvalues.length; i++)
{
yvalues[i] = sin(x)*amplitude;
x+=dx;
}
}
void renderWave()
{
// A simple way to draw the wave with an ellipse at each location
for (int x = 0; x < yvalues.length; x++)
{
noStroke();
fill(0, 50);
ellipseMode(CENTER);
ellipse(x*xspacing, width/2+yvalues[x], 16, 16);
}
}
void stop()
{
baseLoop.close();
minim.stop();
super.stop();
}
void serialEvent (Serial myPort)
{
String mssg= myPort.readStringUntil(linefeed);
if (mssg != null)
{
mssg = trim(mssg);
sensors = int(split(mssg, ','));
}
}
1