NullPointerException error/methods of a class which return objects
in
Programming Questions
•
8 months ago
Hello. I'm getting a NullPointerException error, and I can't seem to figure out what is going wrong. Having looked at the Processing wiki page on NPE errors, I see nothing exactly like what's happening in my own code. I'm wondering if part of the problem is that I've written several classes with methods that return new objects, and if it is indeed possible to do this, that I'm just not doing it correctly. (I am pretty new to object oriented programming and to Processing in particular, so I'm sure the code below is probably pretty brutish, and that there are easier ways to do all of this that I don't yet know about.)
The opening section of the error message is as follows:
Exception in thread "Animation Thread" java.lang.NullPointerException
at bounded_test_130128$Plane.mRotate(bounded_test_130128.java:190)
at bounded_test_130128$Region.rRotate(bounded_test_130128.java:262)
at bounded_test_130128.setup(bounded_test_130128.java:50)
at processing.core.PApplet.handleDraw(PApplet.java:2117)
at processing.opengl.PGL$PGLListener.display(PGL.java:2472)
at jogamp.opengl.GLDrawableHelper.displayImpl(GLDrawableHelper.java:548)
at jogamp.opengl.GLDrawableHelper.display(GLDrawableHelper.java:533)
at jogamp.opengl.GLAutoDrawableBase$2.run(GLAutoDrawableBase.java:280)
at jogamp.opengl.GLDrawableHelper.invokeGLImpl(GLDrawableHelper.java:904)
at jogamp.opengl.GLDrawableHelper.invokeGL(GLDrawableHelper.java:822)
at com.jogamp.newt.opengl.GLWindow.display(GLWindow.java:543)
at processing.opengl.PGL.requestDraw(PGL.java:814)
at processing.opengl.PGraphicsOpenGL.requestDraw(PGraphicsOpenGL.java:1566)
at processing.core.PApplet.run(PApplet.java:2020)
at java.lang.Thread.run(Thread.java:662)
The section of code which Processing highlights for me as the location of the error is in the mRotate method of my Plane class, shown below.
- class Plane {
- CVector intersect;
- CVector normal;
- Plane(CVector tempIntersect, CVector tempNormal) {
- intersect = tempIntersect;
- normal = tempNormal;
- }
- Plane mRotate(RMatrix myrmat) {
- CVector intersectnew = intersect.leftmatmult(myrmat);
- CVector normalnew = normal.leftmatmult(myrmat);
- Plane rotplanereturn = new Plane(intersectnew,normalnew);
- return rotplanereturn;
- }
intersect and normal are two instances of my CVector class, and as I've written that class, with the 'leftmatmult' method it takes in an RMatrix and returns a new CVector which is the original vector multiplied by that matrix. Is there something else I should be doing to initialize 'normalnew' up there before plugging that new rotated vector into it? Am I using a variable name which is already defined in Processing as something else? Is there something else going on here that I'm missing? The relevant parts of CVector are below:
- class CVector {
- float[] vectorarray = new float[3];
- CVector(float x, float y, float z) {
- vectorarray[0] = x;
- vectorarray[1] = y;
- vectorarray[2] = z;
- }
- CVector leftmatmult(RMatrix multiplier) {
- CVector multiplied;
- float newx = 0;
- float newy = 0;
- float newz = 0;
- for (int i = 0; i<3; i++) {
- newx = newx + multiplier.showarray()[0][i]*vectorarray[i];
- newy = newy + multiplier.showarray()[1][i]*vectorarray[i];
- newz = newz + multiplier.showarray()[2][i]*vectorarray[i];
- }
- multiplied = new CVector(newx, newy, newz);
- return multiplied;
- }
I thank you all in advance for your patience and your help.
UPDATE: Found out what was causing the error by writing in a lot of println()s, which showed the above sections of code were working and producing results about half the time; turns out the fault was actually a silly typo in the Region class which called these two other classes. Augh.
1