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 & HelpSyntax Questions › :lukas' syntax question thread
Page Index Toggle Pages: 1
:lukas' syntax question thread (Read 3052 times)
:lukas' syntax question thread
Aug 28th, 2009, 10:35am
 
Hey!

I just started learning programming with Processing (just programming BlitzBasic before...) and I really like it so far! But now I have a rather strange problem even googling couldn't really solve for me... or rather: I couldn't even solve googling.  Cheesy

int [][] nodepos;
int nodecount;

void setup(){
size(300,300);
background(0);
noCursor();
frameRate(30);

int nodepos[][] = new int[2][999];
nodecount = 1;
}

void draw(){
background(0);
stroke(230);
ellipse(mouseX,mouseY, 8,8);  

for(int i=1; i < nodecount; i++){
int bruckeX = nodepos[0][i];
int bruckeY = nodepos[1][i];
stroke(230);
point(bruckeX,bruckeY);
stroke(0,0,150);
alpha(0);
ellipse(bruckeX,bruckeY,40,40);
}

mouseInput();  
}

void mouseInput(){
if (mousePressed==true){
  nodepos[0][nodecount] = mouseX;   //here's where it crashes
  nodepos[1][nodecount] = mouseY;

  nodecount++;
  delay(400);
}
}



So the program basically crashes in the mouseInput()-function (when clicking) with the compiler saying: "NullPointerException"


Has anyone got an idea what the problem might be?


Thanks a lot in advance,
:lukas

Re: (rather strange) NullPointerException
Reply #1 - Aug 28th, 2009, 11:04am
 
The code was almost OK... Smiley
First, a precision: the compiler generates errors (and warnings), not exceptions. That's the runtime which generates exceptions... Wink
Another remark: you should initialize nodeCount with 0, no need to loose an array position.

Now to the point: you declare nodepos, and unlike several newbies, you took care to allocate memory for it.
Except... that's not the nodepos you declared which receives the memory! You declare another variable, local to setup(), of same name, so it just "masks" the global variable, and dies at the end of setup(). So mouseInput() just see the uninitialized global variable. Cheesy

Simple solution: just write either
nodepos = new int[2][999]; in setup, or
int [][] nodepos = new int[2][999]; on the first line.

Oh, and you want to put a limit on nodecount! Otherwise you will get another exception: ArrayOutOfBounds.

And delay() won't work as you expect... Its call must be avoided in draw() (and in general, actually!).
Re: (rather strange) NullPointerException
Reply #2 - Aug 28th, 2009, 11:21am
 
Hey, a few things:

-  an array with n elements, like arrayType[n], will run from [0] to [n-1], so might want to start nodecount on 0.

-  alpha( 0 ) isn't actually doing anything, it's just returning the alpha value of color(0).  Which is probably 0.  If you want a transparent fill, write noFill(), which is the same as saying fill(0,0) -- first value there is the grey, second is the alpha.  Also the same as: fill(0,0,0,0); (RGBA or HSBA) where A is the alpha value.

- you can simplify your mousepressed check by removing it from the draw loop, and just using: void mousePressed(){ click stuff; }

- and your nullException means in this case that the int[][] "nodepos" is null -- if you put: println(nodepos); both in your click script, and at the end of setup, you'll see what I mean.  It's confusing; setup() is initializing an array called "nodepos," but it's a duplicate "nodepos" that expires once setup is complete.  The line:

int nodepos[][] = new int[2][999];

needs to just be:

nodepos = new int[2][999];

because nodepos has already been declared, outside of setup().
Edit: looks like you know that already, thanks to PhilLo :P

--Ben
Re: (rather strange) NullPointerException
Reply #3 - Aug 28th, 2009, 12:17pm
 
Thanks so much for the quick and great help!   Smiley

Works well now. Smiley


About the alpha()-thing:
Okay, well... but what would I have to do to make the blue circles just slightly transparent? So they create darker blues when overlapping.

Also: After about... 50-100 circles the program begins getting awfully slow. What could be the reason?


Thanks in advance again,
:lukas Smiley
Re: (rather strange) NullPointerException
Reply #4 - Aug 28th, 2009, 12:24pm
 
well, the slowdown could be helped by removing delay(400), if that's still in there.  Also the frameRate(30) -- I actually often use frameRate(999) just in order to break the default limit of (60).

For a translucent blue circle, try fill(0,0,200,125); - the alpha value ranges from 0-255 just like the color values (0 == transparent, 255 == opaque)

--Ben
Re: (rather strange) NullPointerException
Reply #5 - Aug 28th, 2009, 1:25pm
 
Thanks! The opacity thing works. Smiley

I have no delay in there anymore...

and I set the framerate to 999. Just slightly helped. The opacity thing kind of reveiled the problem and what I *tried* to *fix* with the delay:
If the mouse-button is not released until draw() is repeated again another circle will be registered.  Is there any way to easily fix this? (e.g. by using another command for registering mouse input)

:lukas
Re: (rather strange) NullPointerException
Reply #6 - Aug 28th, 2009, 1:28pm
 
are you using void mousePressed() and taking your "is the mouse pressed?" check out of draw()?  mousePressed() should only run once, when clicked...
Re: :lukas' syntax question thread
Reply #7 - Aug 29th, 2009, 2:17pm
 
Hey. Smiley

I figured it might be a good idea to turn this thread into my syntax-question thread since I'm probably going to have a lot of questions coming up in my upcoming Processing-learning-process.
I don't want to spam the whole board whenever I have a short question. Smiley
I hope it's okay this way?


Another question:
Is there any way to include code into a sketch without actually "adding  it to the project"? So... if I for example wrote some classes (I probably wont have to change that much) into a .pde and want to reuse them in future (larger) projects without having to deal with all those files in my program... would there be some way of putting the .pde-files into some extra folder in my sketches project folder and simply including the containing code into my sourcecode with a command? Could "import()" work for that or does it only work with special files?


Thanks a lot in advance,
:lukas
Re: :lukas' syntax question thread
Reply #8 - Aug 29th, 2009, 3:25pm
 
I think you want to make a library. There is a section in the site explaining how to do them. I haven't really tried to make one myself...
Re: :lukas' syntax question thread
Reply #9 - Aug 30th, 2009, 1:45pm
 
Well... I can't actually use Java or anything related...

I actually just want to include code without having to deal with it in the tab-bar... since large projects would end up being really confusing with large amounts of tabs up there.


:lukas
Re: :lukas' syntax question thread
Reply #10 - Aug 30th, 2009, 11:20pm
 
I have made a suggestion along these lines a while ago: to be able to write generic .pde files (or .java), and have them integrated semi-automatically in new sketches, avoiding a copy/paste of generic classes without copy/paste (which also means several copies to maintain/fix in case of bug) and without making a library (meaning the code is already tested, has to be packaged on each change, etc.).
Authors of Processing hadn't found time to do it (or don't agree with the idea, I don't know).
Page Index Toggle Pages: 1