尝试从一个类引用一个方法到另一个类= NullPointerException

时间:2014-03-28 19:42:52

标签: java nullpointerexception jmonkeyengine

我在jMonkey Engine 3中制作游戏,我的世界代码有点笨重,所以我打算将它移到一个名为Generator的新类,但是当我终于解决了所有问题并运行了我的程序,我在我试图使用其他类的方法的地方得到NullPointerException。我过去曾经遇到过NullPointerException这么糟糕的运气,但这是最糟糕的一次。代码在主类文件中有效。我将为您提供以下两个类的代码以及错误。

请注意,我不包括包装声明或导入以节省空间。

第一课:主要:

public class Main extends SimpleApplication implements ActionListener {

    private static final Logger logger = Logger.getLogger(Main.class.getName());
    private BulletAppState bulletAppState;
    private CharacterControl playerControl;
    private Vector3f walkDirection = new Vector3f();
    private boolean[] arrowKeys = new boolean[4];
    private CubesSettings cubesSettings;
    private BlockTerrainControl blockTerrain;
    private Node terrainNode = new Node();
    private Generator gen;

    public static void main(String[] args){
        Logger.getLogger("").setLevel(Level.FINE);
        AppSettings s = new AppSettings(true);
        Main app = new Main();
        try {
            s.load("com.bminus");
        } catch(BackingStoreException e) {
            logger.log(Level.SEVERE, "Could not load configuration settings.",e);
        }
        try {
            s.setIcons(new BufferedImage[]{ImageIO.read(new File("assets/Textures/icon.gif"))});
        } catch (IOException e) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, "Icon file missing",e);
        }
        s.setRenderer(AppSettings.LWJGL_OPENGL2);
        s.setBitsPerPixel(24);
        s.setFrameRate(-1);
        s.setFullscreen(false);
        s.setResolution(640,480);
        s.setSamples(0);
        s.setVSync(true);
        s.setFrequency(60);
        s.setTitle("The Third Power Pre-Alpha 1.1.0");
        try {
            s.save("com.bminus");
        } catch(BackingStoreException e) {
            logger.log(Level.SEVERE, "Could not save configuration settings.",e);
        }
        app.setShowSettings(false);
        app.setSettings(s);
        app.start();
    }

    public Main(){}

    @Override
    public void simpleInitApp(){

        setDisplayFps(false);
        setDisplayStatView(false);
        bulletAppState = new BulletAppState();
        stateManager.attach(bulletAppState);
        initControls();
        gen.initBlockTerrain(); //THIS LINE IS THE ONE THAT IS NULL!!!
        initGUI();
        initPlayer();
        cam.lookAtDirection(new Vector3f(1, 0, 1), Vector3f.UNIT_Y);
    }

    private void initControls(){
        inputManager.addMapping("fps_show", new KeyTrigger(KeyInput.KEY_F3));
        inputManager.addMapping("fps_hide", new KeyTrigger(KeyInput.KEY_F4));
        inputManager.addMapping("left", new KeyTrigger(KeyInput.KEY_A));
        inputManager.addMapping("right", new KeyTrigger(KeyInput.KEY_D));
        inputManager.addMapping("forward", new KeyTrigger(KeyInput.KEY_W));
        inputManager.addMapping("backward", new KeyTrigger(KeyInput.KEY_S));
        inputManager.addMapping("jump", new KeyTrigger(KeyInput.KEY_SPACE));
        inputManager.addMapping("place", new MouseButtonTrigger(MouseInput.BUTTON_RIGHT));
        inputManager.addMapping("break", new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
        inputManager.addListener(this, "fps_show");
        inputManager.addListener(this, "fps_hide");
        inputManager.addListener(this, "left");
        inputManager.addListener(this, "right");
        inputManager.addListener(this, "forward");
        inputManager.addListener(this, "backward");
        inputManager.addListener(this, "jump");
        inputManager.addListener(this, "place");
        inputManager.addListener(this, "break");
    }

    private void initGUI(){
        BitmapText crosshair = new BitmapText(guiFont);
        crosshair.setText("+");
        crosshair.setSize(guiFont.getCharSet().getRenderedSize() * 2);
        crosshair.setLocalTranslation(
                (settings.getWidth() / 2) - (guiFont.getCharSet().getRenderedSize() / 3 * 2),
                (settings.getHeight() / 2) + (crosshair.getLineHeight() / 2), 0);
        guiNode.attachChild(crosshair);

        BitmapText instructionsText1 = new BitmapText(guiFont);
        instructionsText1.setText("Left Click: Remove");
        instructionsText1.setLocalTranslation(0, settings.getHeight(), 0);
        guiNode.attachChild(instructionsText1);
        BitmapText instructionsText2 = new BitmapText(guiFont);
        instructionsText2.setText("Right Click: Place");
        instructionsText2.setLocalTranslation(0, settings.getHeight() - instructionsText2.getLineHeight(), 0);
        guiNode.attachChild(instructionsText2);
        BitmapText instructionsText3 = new BitmapText(guiFont);
        instructionsText3.setText("Bottom Layer Cannot Be Broken");
        instructionsText3.setLocalTranslation(0, settings.getHeight() - (2 * instructionsText3.getLineHeight()), 0);
        guiNode.attachChild(instructionsText3);
    }

    private void initPlayer(){
        playerControl = new CharacterControl(new CapsuleCollisionShape((cubesSettings.getBlockSize() / 2), cubesSettings.getBlockSize() * 2), 0.05f);
        playerControl.setJumpSpeed(25);
        playerControl.setFallSpeed(140);
        playerControl.setGravity(100);
        playerControl.setPhysicsLocation(new Vector3f(5, 257, 5).mult(cubesSettings.getBlockSize()));
        bulletAppState.getPhysicsSpace().add(playerControl);
    }

    @Override
    public void simpleUpdate(float lastTimePerFrame) {
        float playerMoveSpeed = ((cubesSettings.getBlockSize() * 2.5f) * lastTimePerFrame);
        Vector3f camDir = cam.getDirection().mult(playerMoveSpeed);
        Vector3f camLeft = cam.getLeft().mult(playerMoveSpeed);
        walkDirection.set(0, 0, 0);
        if(arrowKeys[0]){ walkDirection.addLocal(camDir); }
        if(arrowKeys[1]){ walkDirection.addLocal(camLeft.negate()); }
        if(arrowKeys[2]){ walkDirection.addLocal(camDir.negate()); }
        if(arrowKeys[3]){ walkDirection.addLocal(camLeft); }
        walkDirection.setY(0);
        playerControl.setWalkDirection(walkDirection);
        cam.setLocation(playerControl.getPhysicsLocation());
    }
        @Override
        public void onAction(String actionName, boolean value, float lastTimePerFrame){
        if(actionName.equals("forward")) {
            arrowKeys[0] = value;
        }
        else if(actionName.equals("right")) {
            arrowKeys[1] = value;
        }
        else if(actionName.equals("left")) {
            arrowKeys[3] = value;
        }
        else if(actionName.equals("backward")) {
            arrowKeys[2] = value;
        }
        else if(actionName.equals("jump")) {
            playerControl.jump();
        }
    else if(actionName.equals("fps_show")) {
        setDisplayFps(true);
    }
        else if(actionName.equals("fps_hide")) {
            setDisplayFps(false);
        }
        else if(actionName.equals("place") && value){
            Vector3Int blockLocation = getCurrentPointedBlockLocation(true);
            if(blockLocation != null){
                blockTerrain.setBlock(blockLocation, Block_Wood.class);
            }
        }
        else if(actionName.equals("break") && value){
            Vector3Int blockLocation = getCurrentPointedBlockLocation(false);
            if((blockLocation != null) && (blockLocation.getY() > 0)){
                blockTerrain.removeBlock(blockLocation);
            }
        }
    }

        private Vector3Int getCurrentPointedBlockLocation(boolean getNeighborLocation){
        CollisionResults results = getRayCastingResults(terrainNode);
        if(results.size() > 0){
            Vector3f collisionContactPoint = results.getClosestCollision().getContactPoint();
            return BlockNavigator.getPointedBlockLocation(blockTerrain, collisionContactPoint, getNeighborLocation);
        }
        return null;
    }

    private CollisionResults getRayCastingResults(Node node){
        Vector3f origin = cam.getWorldCoordinates(new Vector2f((settings.getWidth() / 2), (settings.getHeight() / 2)), 0.0f);
        Vector3f direction = cam.getWorldCoordinates(new Vector2f((settings.getWidth() / 2), (settings.getHeight() / 2)), 0.3f);
        direction.subtractLocal(origin).normalizeLocal();
        Ray ray = new Ray(origin, direction);
        CollisionResults results = new CollisionResults();
        node.collideWith(ray, results);
        return results;
    }

    public void attachRootChildTerrain(){ //THIS IS CALLED IN THE GENERATOR CLASS!!
        rootNode.attachChild(gen.terrainNode);
    }
}

第二类:发电机:

public class Generator {

    private CubesSettings cubesSettings;
    private BlockTerrainControl blockTerrain;
    private final Vector3Int terrainSize = new Vector3Int(1000, 256, 1000);
    private final Vector3Int bottomLayer = new Vector3Int(1000,1,1000);
    private BulletAppState bulletAppState;
    public Node terrainNode = new Node();
    private SimpleApplication sa;
    private Main main;

    public void initBlockTerrain(){
        CubesTestAssets.registerBlocks();
        CubesTestAssets.initializeEnvironment(sa);

        cubesSettings = CubesTestAssets.getSettings(sa);
        blockTerrain = new BlockTerrainControl(cubesSettings, new Vector3Int(7, 1, 7));
        blockTerrain.setBlocksFromNoise(new Vector3Int(), terrainSize, 20f, Block_Grass.class);
        blockTerrain.setBlocksFromNoise(new Vector3Int(), bottomLayer, 0.0f, Block_Stone.class);
        blockTerrain.addChunkListener(new BlockChunkListener(){

            @Override
            public void onSpatialUpdated(BlockChunkControl blockChunk){
                Geometry optimizedGeometry = blockChunk.getOptimizedGeometry_Opaque();
                RigidBodyControl rigidBodyControl = optimizedGeometry.getControl(RigidBodyControl.class);
                if(rigidBodyControl == null){
                    rigidBodyControl = new RigidBodyControl(0);
                    optimizedGeometry.addControl(rigidBodyControl);
                    bulletAppState.getPhysicsSpace().add(rigidBodyControl);
                }
                rigidBodyControl.setCollisionShape(new MeshCollisionShape(optimizedGeometry.getMesh()));
            }
        });
        terrainNode.addControl(blockTerrain);
        terrainNode.setShadowMode(RenderQueue.ShadowMode.CastAndReceive);
        main.attachRootChildTerrain();
    }
}

错误:

java.lang.NullPointerException
    at com.bminus.Main.simpleInitApp(Main.java:110)
    at com.jme3.app.SimpleApplication.initialize(SimpleApplication.java:226)
    at com.jme3.system.lwjgl.LwjglAbstractDisplay.initInThread(LwjglAbstractDisplay.java:130)
    at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:207)
    at java.lang.Thread.run(Thread.java:744)

这是我第一次尝试使用多个类。我猜我忘了从Generator类中调用一些东西或者创建一些public而不是private,但我不确定。如果有人能够解决这个问题,我将不胜感激。谢谢!

编辑:我做了你们的建议(我认为),我收到了这个错误:

Exception in thread "main" java.lang.StackOverflowError
    at sun.misc.Unsafe.putObject(Native Method)
    at java.util.concurrent.ConcurrentLinkedQueue$Node.<init>(ConcurrentLinkedQueue.java:187)
    at java.util.concurrent.ConcurrentLinkedQueue.<init>(ConcurrentLinkedQueue.java:255)
    at com.jme3.app.Application.<init>(Application.java:94)
    at com.jme3.app.SimpleApplication.<init>(SimpleApplication.java:102)
    at com.jme3.app.SimpleApplication.<init>(SimpleApplication.java:98)
    at com.bminus.Main.<init>(Main.java:88)
    at com.bminus.Generator.<init>(Generator.java:31)
    at com.bminus.Main.<init>(Main.java:47)

因为它是溢出错误,所以最后两个错误行继续进行!我的代码中被称为错误的行是:

    public Main(){}

    private Main main = new Main();

    private Generator gen = new Generator(this);

我不确切知道为什么会发生这种情况,但如果你们有人这样做,那么知道如何修复它会很棒。谢谢!

1 个答案:

答案 0 :(得分:2)

调试NullPointerException时最重要的是找到抛出异常的行。您尝试在该行上使用的变量为null。


可能的罪魁祸首:

在Main类中,您的Generator变量gen永远不会分配实例,因此为null。通过调用Generator构造函数给它一个实例。

gen = new Generator();

在您的Generator类中,Main字段main为null,因为您从未为其分配Main实例。我建议你给构造函数一个Main参数,这样你就可以将你在main方法中创建的当前使用的Main实例(这些名称令人困惑!)传入你的Generator类,并使用这个参数初始化你的主要字段。

即,

public Generator(Main main) {
  //.....

  this.main = main; // give the main field an object
}

因此,请更改以上代码以将Generator创建为:

gen = new Generator(this);