0

I am receiving a NullPointerException in thread "Animation Thread" while trying to draw a sphere using the processing libs. I can draw the sphere just fine if I declare the sphere in my Main.java class, but when I try to move the declarations to another class and instantiate that class I get a null pointer exception.

StackTrace

Exception in thread "Animation Thread" java.lang.RuntimeException: java.lang.NullPointerException
    at com.jogamp.common.util.awt.AWTEDTExecutor.invoke(AWTEDTExecutor.java:58)
    at jogamp.opengl.awt.AWTThreadingPlugin.invokeOnOpenGLThread(AWTThreadingPlugin.java:103)
    at jogamp.opengl.ThreadingImpl.invokeOnOpenGLThread(ThreadingImpl.java:206)
    at javax.media.opengl.Threading.invokeOnOpenGLThread(Threading.java:172)
    at javax.media.opengl.Threading.invoke(Threading.java:191)
    at javax.media.opengl.awt.GLCanvas.display(GLCanvas.java:541)
    at processing.opengl.PJOGL.requestDraw(PJOGL.java:688)
    at processing.opengl.PGraphicsOpenGL.requestDraw(PGraphicsOpenGL.java:1651)
    at processing.core.PApplet.run(PApplet.java:2256)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
    at unTextured3DShapes.Spheres.update(Spheres.java:43)
    at main.ShapeDefs.update(ShapeDefs.java:52)
    at main.Main.draw(Main.java:37)
    at processing.core.PApplet.handleDraw(PApplet.java:2386)
    at processing.opengl.PJOGL$PGLListener.display(PJOGL.java:862)
    at jogamp.opengl.GLDrawableHelper.displayImpl(GLDrawableHelper.java:665)
    at jogamp.opengl.GLDrawableHelper.display(GLDrawableHelper.java:649)
    at javax.media.opengl.awt.GLCanvas$10.run(GLCanvas.java:1289)
    at jogamp.opengl.GLDrawableHelper.invokeGLImpl(GLDrawableHelper.java:1119)
    at jogamp.opengl.GLDrawableHelper.invokeGL(GLDrawableHelper.java:994)
    at javax.media.opengl.awt.GLCanvas$11.run(GLCanvas.java:1300)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$500(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

Main.java

    package main;

import peasy.PeasyCam;
import processing.core.PApplet;
import unTextured3DShapes.Spheres;

@SuppressWarnings("serial")
public class Main extends PApplet {
    PeasyCam cam;

    public Spheres untexturedSphere = new Spheres(0,0,0,100,10,0,0,0,this);

    // ShapeDef definition to be updated later
//  ShapeDefs shapeDefinitions = new ShapeDefs();

    public void setup() {
        size(displayWidth, displayHeight, OPENGL);
        cam = new PeasyCam(this, 100);
        cam.setMinimumDistance(-width);
        cam.setMaximumDistance(width);

        // Drawing the images onto the spheres
//      untexturedSphere.sphereList.add(new Spheres(10,50,0,100,10,0,0,0,this));

        shapeDefinitions.shapeAdditions(this);
    }

    public void draw() {
        // Basic startup parameters
        translate(width / 2, height / 2, 0);
        background(map(mouseX, -width, width, 0, 128), map(mouseX, -width, width, 128, 255), map(mouseX, -width, width, 50, 200));
        smooth(4);

        //Calling the Update Methods
//      untexturedSphere.update();

        shapeDefinitions.update();
    }

}

Spheres.java

package unTextured3DShapes;

import java.util.ArrayList;
import main.Main;
import processing.core.PApplet;

public class Spheres {

    public float x;
    public float y;
    public float z;
    public int r;
    public int d;
    public float rotx;
    public float roty;
    public float rotz;
    public float dx = 0;
    public float dy = 1;
    public boolean drawSphere,rotateX,rotateY,rotateZ = false;

    PApplet applet = new PApplet();

    public ArrayList<Spheres> sphereList = new ArrayList<>();

    public Spheres(PApplet p) {
        this.applet = p;
    }

    public Spheres(float xpos, float ypos, float zpos, int rad, int det, float rxval, float ryval, float rzval, Main app) {
        x = xpos; y = ypos; z = zpos; r = rad; d = det; rotx = rxval; roty = ryval; rotz = rzval; applet = app;
    }

    public void crashCheck() {
        if (r <=0 || d <= 0 ) { drawSphere = false; } else { if (r > 0 && d > 0) { drawSphere = true; }}
        if (rotx >= 0) { rotateX = false; } else { rotateX = true; }
        if (roty >= 0) { rotateY = false; } else { rotateY = true; }
        if (rotz >= 0) { rotateZ = false; } else { rotateZ = true; }
    }

    public void update() {
        crashCheck();

        applet.pushMatrix();
        applet.translate(x, y, z);
        if (drawSphere = false) { ; } else {applet.noFill(); applet.sphereDetail(d); applet.sphere(r); }
        if (rotateX = false) { ; } else { applet.rotateX(rotx); }
        if (rotateY = false) { ; } else { applet.rotateY(roty); }
        if (rotateZ = false) { ; } else { applet.rotateZ(rotz); }
        applet.popMatrix();

        // Gravity and Physics
        // At some point in the future, make the gravity and other physics
        // dynamic based off of size, etc.
        x += dx;
        y += dy;
        dy += 1;
        if (x > applet.width/2 - r || x < applet.width/ -2 + r) {         //If "x" is greater than "width" or less than "0", then "dx" times negative 1
            dx *= -1;                                                     //Multiply dx (delta speed) by negative 1 
            }
            if (y > applet.height/2 - r || y < applet.height / -2 + r) {
               dy *= -0.9;
        }
    }
}

ShapeDefs.java

package main;

import unTextured3DShapes.Spheres;

public class ShapeDefs {
    Main applet;

    // Variables for untextured three dimensional shapes
    // Variables for Spheres
//  float sx = applet.random(-applet.width / +2, applet.width / 2);
//  float sy = applet.random(-applet.width / +2, applet.width / 2);
//  float sz = 0;
//  float srx = sx;
//  float sry = sy;
//  float srz = 0;
//  int srad = (int) applet.sin(applet.random(0, applet.width / 2));
//  int sdet = (int) 15;
//  float srxval = (applet.radians(applet.frameCount * 3));
//  float sryval = (applet.radians(applet.frameCount * 3));
//  float srzval = 0;
//
//  // Variables for Cubes
//  float cx = applet.random(-applet.width / +2, applet.width / 2);
//  float cy = applet.random(-applet.width / +2, applet.width / 2);
//  float cz = 0;
//  int crad = (int) Math.sin(applet.random(0, applet.width / 2));
//  int cdet = (int) 15;
//  float crxval = applet.radians(applet.frameCount * 2);
//  float cryval = applet.radians(applet.frameCount * 2);
//  float crzval = 0;

//  Ellipsoid e = new Ellipsoid(this, 2, 50);

    // Background definition
    // Rendering Back1 = new Rendering();

    // Untextured Three Dimensional shape definitions
    // Sphere Definitions
    Spheres utSpheres = new Spheres(this.applet);

    // Cube Definitions
    //Cubes C1 = new Cubes(cx, cy, cz, 100, cdet, crxval, cryval, crzval, this.applet);

    public void shapeAdditions(Main app) {
//      utSpheres.sphereList.add(new Spheres(sx, sy, sz, srad, sdet, srxval, sryval, srzval, this.applet));
        utSpheres.sphereList.add(new Spheres(0,0,0,100,10,0,0,0,this.applet));
    }


    public void update() {
        // Call to update method for three dimensional shapes       
        utSpheres.update();

//      C1.update();

//      e.draw();

    }
}
Matthewacon
  • 360
  • 3
  • 15
  • What line throws the exception? Can you post your stack trace? – Kevin Workman Aug 17 '15 at 15:19
  • I edited the post and added the stack trace. Also, I don't believe that this is a duplicate, because I know exactly what a NullPointerException is and how to fix it, but in this case, I cannot find where it is. The code just returns whatever line comes first in the Spheres update method. – Matthewacon Aug 17 '15 at 15:37
  • What is on line 43 of Spheres.java? I agree that this should not have been closed. I'll vote to reopen. – Kevin Workman Aug 17 '15 at 15:40
  • You're never setting the `Main applet` variable of your `ShapeDefs` class. So when you pass it into your `Spheres` constructor, you're passing in null. When you go to use that value, you get your NPE. – Kevin Workman Aug 17 '15 at 15:43
  • I have set the `Main applet` variable in `ShapeDefs`. Its set on the 6th line of `ShapeDefs.java` Or did I set it improperly? I'm quite new to java so I appreciate all of the help :) – Matthewacon Aug 17 '15 at 15:51
  • That is a **declaration**. Note that you aren't actually **setting** that variable to anything, so by default it's null. Try adding some print statements to test that theory (which the answers to the duplicate question should explain). I assume you want the value passed into the `shapeAdditions()` function. – Kevin Workman Aug 17 '15 at 15:53
  • I added `System.out.println("Testing, testing 123");` to the first line in the shapeAdditions method and it printed out to console. How would I go about setting the `Main applet` in `ShapeDefs`? I'm sorry, like I said I'm still learning. – Matthewacon Aug 17 '15 at 16:03
  • I meant you should add print statements that tell you the values of these variables. Something like this would be a start: public `Spheres(PApplet p) { this.applet = p; println(p == null); }` – Kevin Workman Aug 17 '15 at 16:04
  • I added `System.out.println(p == null);` to that constructor in `Spheres.java` and the console output was `true` `null` – Matthewacon Aug 17 '15 at 16:10
  • Yep, that shows you that the `p` variable is null. That's why you're getting an NPE. – Kevin Workman Aug 17 '15 at 16:19
  • So how would I correctly set `Main applet` in `ShapeDefs`? – Matthewacon Aug 17 '15 at 16:26
  • You have to pass it in from `Main`. – Kevin Workman Aug 17 '15 at 16:28
  • Well how would I do that in the ShapeDefs class? – Matthewacon Aug 17 '15 at 16:44
  • You wouldn't. You would have to set it from the `Main` class. You're already passing it in to the `shapeAdditions()` function, but then you aren't doing anything with it. – Kevin Workman Aug 17 '15 at 16:46
  • Let me rephrase my question; I don't know how I would code that in and fix the project... Can you give me a specific example of code? – Matthewacon Aug 17 '15 at 17:01

0 Answers0