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 › g.brightness() Null Pointer ?
Page Index Toggle Pages: 1
g.brightness() Null Pointer ?? (Read 763 times)
g.brightness() Null Pointer ??
Jun 20th, 2005, 11:49pm
 
Quote:
java.lang.NullPointerException

at processing.core.PApplet.brightness(PApplet.java:6668)
at mkv.MyGUI.MyGUIStyle.tintColor(MyGUIStyle.java:156)
...

Any ideas whats going wrong, I'll explain what I know:
I am trying to get my own MyGUI code into a library that I can use and test inside processing. Its compiled as a JAR sitting in \libraries\MyGUI\library, certain methods in the library I can access without error.

When I create a MyGUIStyle object, it by default sets up a bunch of colours, and tints one of the colours using a custom method:
Code:
  // sub-method, returns a single tinted value
public int tintColor(int c, int tint, float str) {
float br = (float)(brightness(c) * brightness(tint) / 255 / 255);
float r = (float)((red(tint)*str + red(c)*(1-str)) * br);
float g = (float)((green(tint)*str + green(c)*(1-str)) * br);
float b = (float)((blue(tint)*str + blue(c)*(1-str)) * br);
return color(round(r), round(g), round(b), alpha(c));
}

This explains the the trace to mkv.MyGUI.MyGUIStyle.tintColor(MyGUIStyle.java:156)

Problem, the stack trace breaks at line 6668 (the brightness method) in PApplet. I downloaded the latest PApplet.java file from SourceForge and scrolled to line 6668 (right at the bottom coincidently)... I found this code:

Code:

public final float brightness(int what) {
return g.brightness(what);
}


The variable g is a PGraphics object. So, what is causing the null pointer exception? A dodgey int value? the PGraphics g object not being created yet? (surely not).

Please, help if you can.
Re: g.brightness() Null Pointer ??
Reply #1 - Jun 21st, 2005, 4:20pm
 
looks like you're trying to call tintColor() before size() has been called. i.e. if you're creating your MyGUI thing like this:

Code:
MyGUI gui = new MyGUI(); // problem!

void setup() {
size(200, 200);
}


that if that's the case, you need to make sure that you're only calling those functions after size() inside setup.
Re: g.brightness() Null Pointer ??
Reply #2 - Jun 21st, 2005, 5:10pm
 
Nope, thats not the cause - I don't initiate anything except ints and floats outside setup() theses days:

Code:
import mkv.MyGUI.*;

MyGUIStyle style;

void setup() {
 // Set scene up
 size(300, 300);
 background(200, 180, 100);
 framerate(30);
 // Initiate objects
 style = new MyGUIStyle(color(255, 255, 0));
}

void draw() {

}

Quote:
java.lang.NullPointerException

at processing.core.PApplet.brightness(PApplet.java:6668)
at mkv.MyGUI.MyGUIStyle.tintColor(MyGUIStyle.java:156)
at mkv.MyGUI.MyGUIStyle.tintColor(MyGUIStyle.java:151)
at mkv.MyGUI.MyGUIStyle.tintColor(MyGUIStyle.java:129)
at mkv.MyGUI.MyGUIStyle.<init>(MyGUIStyle.java:49)
at Temporary_1858_6335.setup(Temporary_1858_6335.java:13)
at processing.core.PApplet.display(PApplet.java:1050)
at processing.core.PGraphics.requestDisplay(PGraphics.java:362)
at processing.core.PApplet.run(PApplet.java:951)
at java.lang.Thread.run(Unknown Source)


I can post the MyGUI.jar and source .java files need be.

Lines 129, 151 and 156 are highlighted below:
Quote:
 // this method alters all the colours by tinting them
 void tintColor(int c) {
   this.background = tintColor(background, c); // Line 129
   ...
 }

 // calls tintColor with maximum amount
 public int tintColor(int c, int tint) {
   return tintColor(c, tint, 1.0f); // Line 151
 }
                 
 // sub-method, returns a single tinted value
 public int tintColor(int c, int tint, float str) {
   float br = (float)(brightness(255) * brightness(255) / 255 / 255); // Line 156
   float r = (float)((red(tint)*str + red(c)*(1-str)) * br);
   float g = (float)((green(tint)*str + green(c)*(1-str)) * br);
   float b = (float)((blue(tint)*str + blue(c)*(1-str)) * br);
   return color(round(r), round(g), round(b), alpha(c));
 }
Re: g.brightness() Null Pointer ??
Reply #3 - Jun 22nd, 2005, 10:56pm
 
Ok, after a day of pondering and doing other things I've finally figured out the horrible truth why this doesn't work.

fry was right about why the error would normally be caused, but wrong about why.

The way I'd been working was in my library calling functions such as brightness(), red(), etc. For these methods to work they need to be attached to a PApplet object. I originally just extending PApplet, but this was a fruitless exercise because they'd be drawing to no where.

Instead, I've had to rewrite all my code so that every MyGUI object contains a reference to originating PApplet by way of the variable PApplet _root. From here, any call to a normal processing function is preceeded by _root. e.g. _root.brightness(color); _root.color(255, 255, 0); etc.

I've managed to get all of this working in the past hour, I think there may still be some passing bugs to resolve but mostly things are moving in the right direction - soon I'll have a skeleton library that has, at least, some uses. Buttons are working great.
Re: g.brightness() Null Pointer ??
Reply #4 - Jun 23rd, 2005, 12:32am
 
Smiley Smiley Smiley Smiley

(see working version below, have a look at the wonderfully short source code)

http://mkv25.net/applets/guiRandomSquares/
Page Index Toggle Pages: 1