0

I am trying to make a slightly modified version of checkers for a final project, however I am getting a strange error whenever I run anything related to graphics: Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException at Board.drawBoard(TacticalCheckers.java:45) at TacticalCheckers.paint(TacticalCheckers.java:14) at sun.awt.RepaintArea.paintComponent(RepaintArea.java:264) at sun.awt.RepaintArea.paint(RepaintArea.java:240) at sun.awt.windows.WComponentPeer.handleEvent(WComponentPeer.java:358) at java.awt.Component.dispatchEventImpl(Component.java:4967) at java.awt.Container.dispatchEventImpl(Container.java:2294) at java.awt.Component.dispatchEvent(Component.java:4713) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758) at java.awt.EventQueue.access$500(EventQueue.java:97) at java.awt.EventQueue$3.run(EventQueue.java:709) at java.awt.EventQueue$3.run(EventQueue.java:703) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86) at java.awt.EventQueue$4.run(EventQueue.java:731) at java.awt.EventQueue$4.run(EventQueue.java:729) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76) at java.awt.EventQueue.dispatchEvent(EventQueue.java:728) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93) at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

My code:

// TacticalCheckers.java
import java.awt.*;
import java.applet.*;
import java.util.*; 


public class TacticalCheckers extends Applet
{

    public void paint(Graphics g){
        Board.drawBoard(g);
    }

}


class Square
{

    private Color clr;

    public void drawSquare(Color clr,Graphics g,int r,int c){
        clr = this.clr;
        g.setColor(clr);
        g.drawRect(r*90,c*90,(r+1)*90,(c+1)*90);
    }

}


class Board
{
    private static Color green = new Color(167,254,138);
    private static Color tan = new Color(222,184,135);

    private static Square board[][] = new Square[8][8];

    public static void drawBoard(Graphics g){
        for(int r = 0; r < 8; r++){
            for(int c = 0; c< 8; c++){
                if(((r%2 == 0) && (c%2 == 0)) || ((r%2 == 1) && (c%2 == 1))){
                    board[r][c].drawSquare(green,g,r,c);
                }else{
                    board[r][c].drawSquare(tan,g,r,c);
                }
             }
          }   
       }
    }

I have tried re-installing the IDE (j-grasp) as well and the jre and jdk, but I still get the same error. I have also gotten very similar errors (the only difference being the file/method names) on other graphics programs, which I know used to work. Does anyone know what this error means or more importantly know how to solve it? Thank you for your time! - Fr3d

fr3d
  • 3
  • 5
  • Looks to me you are using a null reference at TacticalCheckers.java:45. What's your code look like there? – Reci May 04 '16 at 23:09
  • I suggest you read this QA for how to correctly initialize multidimensional array: http://stackoverflow.com/questions/1067073/initialising-a-multidimensional-array-in-java – Reci May 05 '16 at 02:16
  • Let's try a small quiz (not look up in Google): given `MyClass classes[][] = new MyClass[3][3];` what will `classes[1][1]` be? – Reci May 05 '16 at 02:26
  • SOLUTION: I forgot to construct the objects of the array... Wow, I feel stupid... although my APCS teacher didn't catch it either so that's nice to know. As always, thanks to everyone who helped, especially @CrendKing! – fr3d May 05 '16 at 02:40

0 Answers0