我有以下处理程序:

//using Papplet instead of STDraw to visually represent my grid, created by Mahmed Ibrahim
import java.awt.Color;

import processing.core.*;

import processing.core.PApplet;

public class C4Grid extends PApplet {
    PShape s;
    PShape[][] circleSpaces;
    boolean[][] circleSpacesFilled;
    boolean[][] circleHasYelowPiece;
    boolean[][] circleHasRedPiece;
    final float SPACES_BETWEEN_ROWS = 110;
    final float SPACES_BETWEEN_COLUMNS = 130;


    public C4Grid(){}

    public void setup() {
        System.out.println("it got to here where it breaks");

        size(1000, 1000, P2D);

        // Making the shape of the grid using vertices
        // so I'm manually drawing my polygon.
        s = createShape();
        s.beginShape();
        s.fill(34, 56, 100);
        s.tint(34, 56, 100);
        s.stroke(0);
        s.strokeWeight(5);
        s.vertex(400, 400);
        s.vertex(400, -440);
        s.vertex(360, -440);
        s.vertex(360, -400);
        s.vertex(-360, -400);
        s.vertex(-360, -440);
        s.vertex(-400, -440);
        s.vertex(-400, 420);
        s.vertex(-420, 420);
        s.vertex(-420, 440);
        s.vertex(-360, 440);
        s.vertex(-360, 420);
        s.vertex(-380, 420);
        s.vertex(-380, 400);
        s.vertex(380, 400);
        s.vertex(380, 420);
        s.vertex(360, 420);
        s.vertex(360, 440);
        s.vertex(420, 440);
        s.vertex(420, 420);
        s.vertex(400, 420);
        s.vertex(400, 420);
        s.vertex(400, -440);
        s.vertex(400, 400);
        s.endShape();
        System.out.println("it got to here where it breaks");

        // using a 2D array to create a grid of circles
        // which will represent the spaces on the grid
        circleHasYelowPiece = new boolean[7][6];
        circleHasRedPiece = new boolean[7][6];
        circleSpacesFilled = new boolean[7][6];
        circleSpaces = new PShape[7][6];
        for (int row = 0; row < 7; row++) {
            for (int column = 0; column < 6; column++) {
                circleSpaces[row][column] = createShape(ELLIPSE, -380 + (row) * SPACES_BETWEEN_ROWS,
                        -370 + (column) * SPACES_BETWEEN_COLUMNS, 100, 100);
                circleSpaces[row][column].disableStyle();
                stroke(0);
                strokeWeight(5);
                circleSpacesFilled[row][column] = false;
                circleHasRedPiece[row][column] = false;
                circleHasYelowPiece[row][column] = false;
            }
        }

    }

    public void draw() {
        translate(width / 2, height / 2);
        shape(s);
        for (int row = 0; row < 7; row++) {
            for (int column = 0; column < 6; column++) {
                shape(circleSpaces[row][column]);
            }
        }
    }

    public boolean piecePlaced(int column, Color pieceColor) {
        column = column - 1; // the choice are form 1-7 but in an array its 0-6;
        boolean moveDone = false;
        int i = 5;
        Color red = new Color(255, 0, 0);
        while (i >= 0) {
            if (circleSpacesFilled[column][i] == false) {
                circleSpacesFilled[column][i] = true;
                if (pieceColor.equals(red)) {
                    circleHasRedPiece[column][i] = true;
                    circleSpaces[column][i].fill(255, 0, 0);
                    circleSpaces[column][i].tint(255, 0, 0);
                } else {
                    circleHasYelowPiece[column][i] = true;
                    circleSpaces[column][i].fill(255, 255, 0);
                    circleSpaces[column][i].tint(255, 255, 0);
                }
                return true;
            }
        }

        return false;
    }

}


当我运行它时,我得到这个NullPointerException。请注意,异常来自Processing的库中-它不是直接由我自己的代码引起的!

java - 创建PShape时来自Processing的库中的NullPointerException-LMLPHP

可疑的3行是:


currentGame = new C4Game(player1Is,player2Is,player1Color,player2Color);
theGrid = new C4Grid(); theGrid.setup();
s= createShape();顶部附近的setup()


currentGametheGrids都不为空(我已经检查了无数次)。

即使当我单独测试每一行时,与PShape类相关的任何事情也会出现错误。我摆脱了每个PShape对象,它都起作用了,但是有什么办法可以解决它,以便可以在代码中使用PShape

最佳答案

当我运行您的代码时,没有得到NullPointerException。我收到这样的错误消息:


当不使用PDE时,size()只能在settings()中使用。
从setup()中删除size()方法,并添加以下内容:

public void settings() {
  size(1000, 1000, "processing.opengl.PGraphics2D");
}



错误说明了一切。将处理用作库时,不能从size()函数调用setup()函数。而是从settings()函数调用它。

如果我进行更改,您的代码可以正常运行:

关于java - 创建PShape时来自Processing的库中的NullPointerException,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36602275/

10-11 12:19