Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

intellij idea - java.lang.NullPointerException while using PeasyCam and Processing. PeasyCam says it cannot read "width" because <parameter2> is null

I am using IntelliJ idea, processing, peasy and obviously Java to run everything. I'm trying to use PeasyCam in my setting() method and it keeps giving me this error:

java.lang.NullPointerException: Cannot read field "width" because "<parameter2>" is null
    at peasy.PeasyCam.<init>(Unknown Source)
    at peasy.PeasyCam.<init>(Unknown Source)
    at com.nod.planets.Main.settings(Main.java:14)
    at processing.core.PApplet.handleSettings(PApplet.java:978)
    at processing.core.PApplet.runSketch(PApplet.java:10897)
    at processing.core.PApplet.main(PApplet.java:10657)
    at processing.core.PApplet.main(PApplet.java:10639)
    at com.nod.planets.Main.main(Main.java:45)

Honestly I'm completely new to peasy and processing and I'm stuck when it comes to debugging this.

Here's the code for reference:

package com.nod.planets;

import processing.core.PApplet;
import peasy.*;

public class Main extends PApplet
{
    PeasyCam cam;

    @Override
    public void settings()
    {
        size(600, 600, P3D);
        cam = new PeasyCam(this, 200);
    }

    @Override
    public void draw()
    {
        background(0);
        fill(255);
        lights();
        //translate(width / 2, height / 2);

       float sphereRadius = 200;
       int sphereVertices = 100;
       for (int i = 0; i < sphereVertices; i++)
       {
           float longitude = map(i, 0, sphereVertices, -PI, PI);
           for (int j = 0; j < sphereVertices; j++)
           {
               float latitude = map(j, 0, sphereVertices, -HALF_PI, HALF_PI);
               float x = sphereRadius * sin(longitude) * cos(latitude);
               float y = sphereRadius * sin(longitude) * sin(latitude);
               float z = sphereRadius * cos(longitude);
               stroke(255);
               strokeWeight(2);
               point(x, y, z);
           }
        }
    }

    public static void main(String[] args)
    {
        PApplet.main("com.nod.planets.Main");
    }
}

If I get rid of initializing the PeasyCam and uncomment the translate(width / 2, height / 2); code then it works like a charm, but the minute I try this it gives that error... It's just this damn "easy" "PeasyCam" that's giving me the trouble. Also yes I am trying to make planets with perlin noise terrain in case you're wondering. Don't @ me on whether it'll work or not (idk tbh), just please help me in figuring out why it's giving me this error.

question from:https://stackoverflow.com/questions/65854399/java-lang-nullpointerexception-while-using-peasycam-and-processing-peasycam-say

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The problem could be caused by the PApplet object not being fully initialized when the settings function is called.

Background information: the "main" constructor of the PeasyCam class uses a PGraphics object which is derived from the PApplet object. This PGraphics object might be null when settings is called, which would cause the following line in the PeasyCam constructor to fail:

viewport[2] = pg.width;

You can postpone the initialization of the camera to the setup function like this (thanks to George Profenza for improving this answer):

@Override
public void settings()
{
    size(600, 600, P3D);
}

@Override
public void setup()
{
    cam = new PeasyCam(this, 200);
}

@Override
public void draw()
{
    background(0);
    // [...]
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...