We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi everyone : ) ,
I'm having issues with a program I currently making and can't figure out how the solve my nullpointer exception error... I want to make a little program that load a .svg, and allow you to draw with it. Thing is, i don't want the drawing to be pixels, i want it to be vectors. So the thing i'm trying to do is to add in a buffer shape the shapes you're making by moving the mouse with the RG.union function in geomerative.
Here's the code :
import geomerative.*;
RShape shp;
RShape cS; //the cursor shape
RShape cR=null; //the polygone saved
float anglerot;
void setup(){
size(800, 600);
background(255);
smooth();
//Il faut faire jouer ici le frameRate et anglerot pour trouver la ligne parfaite
//frameRate(1);
anglerot = 0.001;
// VERY IMPORTANT: Allways initialize the library before using it
RG.init(this);
//svg file wich'll be shape of the pen
shp = RG.loadShape("plume.svg");
shp = RG.centerIn(shp, g);
//pen size
shp.scale(0.1);
shp.setFill(5);
}
Boolean initRes() {
if (cR==null) {
cR=new RShape(cS); //If cR empty, the first time, we fill it with cS shape
println("creating cR " + cR);
return true;
} else return false;
}
void draw(){
line(0, 400, width, 400);
line(0, 100, width, 100);
line(0, 200, width, 200);
translate(width/2,height/2);
cS = new RShape(shp);
if (mousePressed){
cS.translate(mouseX - width/2, mouseY - height/2);
cS.rotate(anglerot, cS.getCenter());
initRes();
RG.shape(cS);
}
}
void mouseDragged() {
println(" cR " + cR + ", cS " + cS);
cR=RG.union(cS, cR);
}
void keyPressed() {
if (key == 'e' || key == 'E') {
background(255);
}
if (key=='d') RG.shape(cR);
}
The line wich is not working is
cR=RG.union(cS, cR);
and i get a NullpointerException
Weird thing is that just before the
println(" cR " + cR + ", cS " + cS);
returns me
cR geomerative.RShape@715c057c, cS geomerative.RShape@1a28651c
wich should means that shapes exist right ?
When looking around on the internet, I saw that most common source of nullPointer is local and global declaration, but I can't see any problem of that kind in my code. But a might missing it...
Is someone have an idea why it doesn't work ? : )
Answers
why is it in
mouseDragged()
?my gut feeling:
try use
mousePressed()
as a function instead of having it indraw()
as a boolean;-)
Maybe the NPE is inside the Geomerative library. You should show the full stack trace (copy & paste from the console).
Hi guys !
First of all, thanks for your replies ! I had to stop working on that problem because of some other hard work i had to do, but now i'm ready to get back on it !
Chrisir, are you suggesting something like that ?
It doesn't work either : /
and PhiLho, the full stack trace is only what's inside the console when running the error ? Here it is :
Could possibly a NullPointer error be caused by the fact that too many shapes are unioned ?
Ok, after running some tests, clearly the problem come from the loaded svg. Now I have to find out why.
Why does a svg into a union makes a nullPointer, while regular shape works.
I was helped by this example : openprocessing.org/sketch/2159
Ok, finally find out why it wasn't working. I could have searched for years in the code, it was actually in the .svg.
My shape was a triangle but had weird useless points left. Once removed, it worked !