Null Pointer Exception, problem exporting to PDF
in
Programming Questions
•
3 years ago
This forum was so helpful yesterday, hope I'm not abusing it! I've been banging my head agains the wall for several hours now, though. This program basically creates new letterforms based on adding variables that react to Arial.
So two problems: 1) I'm getting a Null Point Exception at run time 2) I am trying to record the outcome to a pdf (so that when someone hits 'enter' the animation stops and creates a pdf of it - see code in yellow). So far it will stop and it creates the pdf, but the pdf has an error message saying the file type isn't supported or it's damaged (the size is 0K). Any ideas? Thanks so much in advance!!
import processing.opengl.*;
import geomerative.*;
import processing.pdf.*;
float toldist;
RFont f;
RGroup grupo;
boolean restart = false;
Particle[] psys;
int numPoints, numParticles;
float maxvel;
//------------------------ Runtime properties ----------------------------------
// Save each frame
boolean SAVEVIDEO = false;
boolean SAVEFRAME = false;
boolean APPLICATION = true;
String DEFAULTAPPLETRENDERER = P3D;
int DEFAULTAPPLETWIDTH = 800;
int DEFAULTAPPLETHEIGHT = 800;
String DEFAULTAPPLICRENDERER = OPENGL;
int DEFAULTAPPLICWIDTH = 800;
int DEFAULTAPPLICHEIGHT = 800;
//------------------------------------------------------------------------------
// Text to be written
String STRNG = "B";
// Font to be used
String FONT = "arial.ttf";
// Velocity of change
int VELOCITY = 1;
// Velocity of deformation
float TOLCHANGE = 0.0001;
// Coefficient that handles the variation of amount of ink for the drawing
float INKERRCOEFF = 0.001;
// Coefficient that handles the amount of ink for the drawing
float INKCOEFF = 0.1;
// Coefficient of precision: 0 for lowest precision
float PRECCOEFF = 4;
String newString = "";
int Start_Second;
int Gray1 = 150;
int Gray2 = 150;
int Gray3 = 150;
color Color_Index[]={#e02892, #9a3594, #0094d8, #3f9a5a, #f5851f, #ed1c24};
int Mood_Trigger[] = {0,0,0,0,0,0}; int Active_Color =0;
int Alpha = 200;
int widthdistancia;
void setup(){
int w = DEFAULTAPPLICWIDTH, h = DEFAULTAPPLICHEIGHT;
String r = DEFAULTAPPLICRENDERER;
if(!APPLICATION){
// Specify the width and height at runtime
w = int(param("width"));
h = int(param("height"));
r = (String)param("renderer");
// (String) will return null if param("renderer") doesn’t exist
if (r != OPENGL && r != P3D && r != JAVA2D && r != P2D) {
r = DEFAULTAPPLETRENDERER;
}
// int() will return 0 if param("width") doesn’t exist
if (w <= 0) {
w = DEFAULTAPPLETWIDTH;
}
// int() will return 0 if param(“height”) doesn’t exist
if (h <= 0) {
h = DEFAULTAPPLETHEIGHT;
}
}
size(w,h,r);
beginRecord(PDF, "image.pdf");
frameRate(35);
try{
smooth();
}
catch(Exception e){
}
background(255);
RG.init(this);
widthdistancia = 10;
f = new RFont(this.FONT,72,RFont.CENTER);
initialize();
Start_Second = second();
}
void draw(){
pushMatrix();
translate(width/2, height/2);
noStroke();
for(int i=0;i<numParticles;i++){
for(int j=0;j<VELOCITY;j++){
psys[i].update(grupo);
psys[i].draw(g);
}
}
popMatrix();
toldist =1;
}
void initialize(){
toldist = ceil(width/200F) * (6F/(STRNG.length()+1));
maxvel = width/80F * INKERRCOEFF * (6F/(STRNG.length()+1));
grupo = f.toGroup(STRNG);
RCommand.setSegmentStep(1-constrain(PRECCOEFF,0,0.99));
RCommand.setSegmentator(RCommand.UNIFORMSTEP);
grupo = grupo.toPolygonGroup();
grupo.centerIn(g, 5, 1, 1);
background(255);
RPoint[] ps = grupo.getPoints();
numPoints = ps.length;
numParticles = numPoints;
psys = new Particle[numParticles];
for(int i=0;i<numParticles;i++){
psys[i] = new Particle(g,i,int(float(i)/float(numParticles)*125));
psys[i].pos = new RPoint(ps[i]);
psys[i].vel.add(new RPoint(random(-10,10),random(-10,10)));
}
toldist = 1;
}
void keyReleased(){
if(keyCode==ENTER){
STRNG = newString;
newString = "";
initialize();
endRecord();
} else if(keyCode==BACKSPACE){
if(newString.length() !=0 ){
newString = newString.substring(0,newString.length()-1);
}
}
else if(keyCode!=SHIFT){
newString += key;
}
}
public class Particle{
// Velocity
RPoint vel;
// Position
RPoint pos;
RPoint lastpos;
// Characteristics
int col;
int hueval;
float sz;
// ID
int id;
// Constructor
public Particle(PGraphics gfx, int ident, int huevalue){
pos = new RPoint(random(-gfx.width/2,gfx.width/2),random(-gfx.height/2,gfx.height/2));
lastpos = new RPoint(pos);
vel = new RPoint(0, 0);
//colorMode(HSB);
colorMode(RGB);
sz = random(2,3);
id = ident;
hueval = huevalue;
}
// Updater of position, velocity and color depending on a RGroup
public void update(RGroup grp){
lastpos = new RPoint(pos);
pos.add(vel);
RPoint[] ps = grp.getPoints();
if(ps != null){
float distancia = dist(pos.x,pos.y,ps[id].x,ps[id].y);
if(distancia <= toldist){
id = (id + 1) % ps.length;
}
RPoint distPoint = new RPoint(ps[id]);
distPoint.sub(pos);
distPoint.scale(random(0.028,0.029));
vel.scale(random(0.5,1.3));
vel.add(distPoint);
float sat = constrain((widthdistancia)*0.25,0.001,255);
float velnorm = constrain(vel.norm(),0,maxvel);
sat = abs(maxvel-velnorm)/maxvel*INKCOEFF*255;
sat = constrain(sat,0,255);
col = color(hueval,150,255,sat*(toldist/80));
}
}
public void setPos(RPoint newpos){
lastpos = new RPoint(pos);
pos = newpos;
}
// Drawing the particle
public void draw(PGraphics gfx){
smooth();
//ReadListFile();
int Current_Second = second();
if(abs(Current_Second-Start_Second)>0.1){
ReadListFile();
Start_Second = Current_Second;
}
if(Active_Color>0){
int index = (int) random(0, Active_Color);
int cindex = Mood_Trigger[index];
int r = (int)red(Color_Index[cindex]);
int g = (int)green(Color_Index[cindex]);
int b = (int)blue(Color_Index[cindex]);
stroke(120,100);
noFill();
}
else{
stroke(255,255,255, Alpha);
}
// Change the position and circumference of the ellipse, followed by the numeric color value for a black stroke
ellipse(lastpos.x/2,lastpos.y/2,5, 5);
}
}
void ReadListFile(){
String lines[] = loadStrings("list.txt");
String off="Off";
String on="On";
Active_Color = 0;
for (int i=0; i < lines.length; i++){
if(lines[i].equals(on)==true){
Mood_Trigger[Active_Color]=i;
Active_Color++;
}else{
Mood_Trigger[i]=0;
}
}
}
1