2

I have the following Processing program:

//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;
    }

}

When I run it, I get this NullPointerException. Notice that the exception is coming from within Processing's libraries - it's not directly caused by my own code!

enter image description here

The 3 lines that are suspect are:

  1. currentGame = new C4Game(player1Is,player2Is,player1Color,player2Color);
  2. theGrid = new C4Grid(); theGrid.setup();
  3. s= createShape(); near the top of setup()

currentGame, theGrid, and s are all non-null (I've checked countless times).

Even when I test each line in isolation, I get an error in anything that related to the PShape class. I got rid of every PShape object and it worked, but is there a way to fix it so I can use PShape as part of my code?

Kevin
  • 11,714
  • 18
  • 66
  • 107
  • Where is your stacktrace? The Exception is not thrown in that line. – f1sh Apr 13 '16 at 14:57
  • no its not a duplicate because im getting and animation thread error cause by a nullpointerException. i know what is it i just wanna know where it is because the line where im getting the errors have n varibles what so ever its the createPshape – user6199445 Apr 13 '16 at 15:36
  • We need to see how you're calling this class. Please create a [mcve] that contains a `main()` method and as few lines as possible to reproduce the problem. Your example should not need any other classes, and in fact most of the code you've already posted is just extra. Get rid of anything not directly related to the problem. Also include what version of Processing you're using. Try to avoid posting screenshots of code or error messages. If at all possible, copy and paste the text directly. – Kevin Workman Apr 13 '16 at 16:51
  • @user6199445 I edited your question to make it more readable and to make it more obvious that you're using Processing, not just Java (which has tripped up some senior SO users who aren't familiar with Processing). Are you OK with the changes I made? – Kevin Apr 15 '16 at 20:49
  • 2
    Voted for reopen as http://meta.stackoverflow.com/q/321127/540552 - **IT IS NOT A DUPE OF THAT OTHER QUESTION** – Victor Stafusa Apr 15 '16 at 22:36
  • Can you post your `main()` method? We need to see how you're calling this class, since you're not running it from the Processing editor. – Kevin Workman Apr 18 '16 at 13:42
  • Did you ever get this figured out? – Kevin Workman May 09 '18 at 16:07

1 Answers1

6

When I run your code, I don't get a NullPointerException. I get an error that says this:

When not using the PDE, size() can only be used inside settings(). Remove the size() method from setup(), and add the following:

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

And the error says it all. When you're using Processing as a library, you can't call the size() function from the setup() function. Call it from the settings() function instead.

If I make that change, your code runs fine:

Kevin Workman
  • 39,413
  • 8
  • 61
  • 94
  • no i'm able to make the code work by itself but its when im implementing it in a project i get the errors from this class. would it help if i uploaded all my classes?? – user6199445 Apr 13 '16 at 15:32
  • 1
    @user6199445 I understand. I too am deploying this in eclipse (which is what I assume you mean by "implementing in a project"). No, don't post all of your classes. Instead, try to narrow your problem down to a [mcve]. This should be as few lines as possible but with enough information that we can copy and paste your code to see the exact same thing as you. – Kevin Workman Apr 13 '16 at 15:34
  • ive edited the question with the relevant information and a picture of the error – user6199445 Apr 13 '16 at 15:45
  • @user6199445 I'm not trying to be difficult, but that is not the information we need. We need to see how you're calling this class. Please create a [mcve] that contains a `main()` method and **as few lines as possible** to reproduce the problem. Your example should not need any other classes, and in fact most of the code you've already posted is just extra. Get rid of anything not directly related to the problem. Also include what version of Processing you're using. Try to avoid posting screenshots of code or error messages. If at all possible, copy and paste the text directly. – Kevin Workman Apr 13 '16 at 15:48
  • i managed o solve the issue tho the error said null pointer, the issue was that you cant make and processing related drawings outside of the main i was making the shapes in the secondary classes and it worked after i transfered it to the main – user6199445 Apr 20 '16 at 10:56
  • @user6199445 That doesn't sound right. You should absolutely be able to create drawings outside of the main class. My guess is that you fixed something else in the process, but we can't be sure without seeing a [mcve]. – Kevin Workman Apr 20 '16 at 11:46