8 Replies - 1232 Views - Last Post: 24 September 2011 - 06:34 AM Rate Topic: -----

#1 jvctan16   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 16-September 11

Null Pointer Exception Error

Posted 16 September 2011 - 11:10 PM

public void paint(Graphics g)
	{
setSize(590,530);
int iWidth = getWidth();
int iHeight = getHeight();
for (int i=0; i<8; i=i+2)//rows
{
 for (int j=0; j<9; j=j+2)//columns
  {
	g.setColor(Color.gray);
	g.fillRect(j*iWidth/9,(1+i)*iWidth/9,iWidth/9,iHeight/8); //(x,y,width,height) // EVEN
 	g.fillRect((1+j)*iWidth/9,i*iWidth/9,(iWidth/9)+1,iHeight/8); //(x,y,width,height) // ODD
  }
}
for (int i = 0; i < 42; i++)
{
Globals.pieces[i].xposition=rowToX(Globals.pieces[i].column);/error here
   Globals.pieces[i].yposition=colToY(Globals.pieces[i].row);

  if (Globals.pieces[i].visible)
  {
   g.drawImage(Globals.pieces[i].mypic,Globals.pieces[i].xposition,Globals.pieces[i].yposition,iWidth/9,iHeight/9,this);
  }
}
if (Globals.ok)
{
  g.setColor(Color.red.darker());
  g.drawRect(Globals.pieces[Globals.pieceChosen].xposition, Globals.pieces[Globals.pieceChosen].yposition,50,50);
  g.setColor(Color.red.brighter().brighter());
  g.drawRect(Globals.pieces[Globals.pieceChosen].xposition+1, Globals.pieces[Globals.pieceChosen].yposition+1,50,50);
  g.setColor(Color.red.darker());
  g.drawRect(Globals.pieces[Globals.pieceChosen].xposition+2, Globals.pieces[Globals.pieceChosen].yposition+2,48,48);
}
}

public int rowToX(int r)
	{
		int myx;
		int iHeight = getHeight();
		myx=r*iHeight/9 - 55;
		return myx;
	}
	




"Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at GG.AppletGUI.paint(AppletGUI.java:90)"

I'm making this run in an Swing Applet Form which is embedded in a Swing Internal Frame. I've managed to make this code work on a separate AWT Applet but I can't embedd in an Internal Frame since this is the code that I used:

javax.swing.JApplet theApplet = new AppletGUI();
        GamePanel.setContentPane(theApplet);

This post has been edited by jvctan16: 16 September 2011 - 11:12 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Null Pointer Exception Error

#2 farrell2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 874
  • View blog
  • Posts: 2,706
  • Joined: 29-July 11

Re: Null Pointer Exception Error

Posted 16 September 2011 - 11:44 PM

So where is line 90 in your paint method?
Was This Post Helpful? 0
  • +
  • -

#3 jvctan16   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 16-September 11

Re: Null Pointer Exception Error

Posted 16 September 2011 - 11:52 PM

this one;

Globals.pieces[i].xposition=rowToX(Globals.pieces[i].column);/error here


Was This Post Helpful? 0
  • +
  • -

#4 jvctan16   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 16-September 11

Re: Null Pointer Exception Error

Posted 17 September 2011 - 01:12 AM

tried enclosing it in a try catch but it just skips to the next code
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12355
  • View blog
  • Posts: 45,464
  • Joined: 27-December 08

Re: Null Pointer Exception Error

Posted 17 September 2011 - 08:49 AM

I'm guessing either Globals or Globals.pieces[i] is null.
Was This Post Helpful? 0
  • +
  • -

#6 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8379
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Null Pointer Exception Error

Posted 17 September 2011 - 11:38 AM

Show us the code that initialize Globals and its class
Was This Post Helpful? 0
  • +
  • -

#7 jvctan16   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 16-September 11

Re: Null Pointer Exception Error

Posted 18 September 2011 - 12:21 AM

Here it is;

Globals()
package GG;

import java.awt.Image;

/*Class containing the Global Variables used by the main GG Class*/
public class Globals 
{
    final static int Blank = 0;
    final static int Player = 1;
    final static int AI = 2;
    
    public static int x[]= new int[9]; // X Array for the X Axis of the Game Board
    public static int y[] = new int[8];  // Y Array for the Y Axis of the Game Board
    public static int[][] z = new int[8][9]; // Array that will use the values of x[] and y[] arrays to make the Game Board.
    
    public static piece [] pieces = new piece[42];
    public static int GGBoard[][] = new int [8][9];
    public static int squares[][] = new int [12][12];
    public static int pieceChosen = 0;	//this identifies which piece to move
    public static int numclicks = 0;
    public static int whos_turn;			// used to alternate turns
    public static int counter = 0;
    public static boolean ok = false;		//this is for seeing if its ok to move
    public static int oldx;
    public static int oldy;
    
    public static class piece //this allows us to track all the information
	{           //about the pieces
		int IDNum;
		int row;		//int row;
		int column;		//int column;
		protected int xposition;
                int yposition;
                boolean visible;//boolean visible	//counts as taken
		boolean firstmove;
                Image mypic;	//Image mypic
                String color;	//String color;
		String name;	//String name;
	};
    
    
}


AppletGUI()
package GG;

import GG.Globals.*;
import java.awt.*;
import java.awt.event.*;

public class AppletGUI extends javax.swing.JApplet implements MouseMotionListener {

    
    	
    public void init() {
    			//this declares the Globals.pieces, can't use them without this
		for (int i=0; i< 42; i++)
		{
			Globals.pieces[i]=new piece();
		}
		//This puts Globals.pieces in their original settings in the class
		//top left corner is [0][0]
		//bottom right is [7][8]
		int rowArray[] = {1,2,7,8};
		for (int k = 0; k < 4; k++)
		{
			for (int l = 0; l < 9; l++)
			{
				Globals.pieces[Globals.counter].IDNum = Globals.counter;
				Globals.pieces[Globals.counter].row = rowArray[k];
				Globals.pieces[Globals.counter].column = l + 1;
				Globals.pieces[Globals.counter].xposition = 0;
				Globals.pieces[Globals.counter].yposition = 0;
				Globals.pieces[Globals.counter].visible = true;
				Globals.pieces[Globals.counter].firstmove = true;
				if (rowArray[k] < 3)
					Globals.pieces[Globals.counter].color = "black";
				else
					Globals.pieces[Globals.counter].color = "white";
				GetFileAndName(rowArray[k],l+1);
				Globals.counter++;
			}
		}
		for (int i = 0; i < 8; i++)
			for (int j = 0; j < 9; j++)
				Globals.squares[i][j] = 0;
		addMouseListener(new MousePressListener());
		addMouseMotionListener((MouseMotionListener) this);
		Globals.whos_turn = 0;	// used to alternate turns



    try {
            java.awt.EventQueue.invokeAndWait(new Runnable() {
                public void run() {
                    initComponents();
                }
            });
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    	public int rowToX(int r)
	{
		int myx;
		int iHeight = getHeight();
		myx=r*iHeight/9 - 55;
		return myx;
	}
	public int colToY(int c)
	{
		int myy;
		int iWidth = getWidth();
		myy = c*iWidth/9 - 55;
		return myy;
	}
	public void paint(Graphics g)
	{
                setSize(590,530);
		int iWidth = getWidth();
		int iHeight = getHeight();
		for (int i=0; i<8; i=i+2)//rows
		{
			for (int j=0; j<9; j=j+2)//columns
			{
				g.setColor(Color.gray);
				g.fillRect(j*iWidth/9,(1+i)*iWidth/9,iWidth/9,iHeight/8); //(x,y,width,height) // EVEN
				g.fillRect((1+j)*iWidth/9,i*iWidth/9,(iWidth/9)+1,iHeight/8); //(x,y,width,height) // ODD
			}
		}
                
		if (Globals.ok)
		{
			g.setColor(Color.red.darker());
			g.drawRect(Globals.pieces[Globals.pieceChosen].xposition, Globals.pieces[Globals.pieceChosen].yposition,50,50);
                        g.setColor(Color.red.brighter().brighter());
			g.drawRect(Globals.pieces[Globals.pieceChosen].xposition+1, Globals.pieces[Globals.pieceChosen].yposition+1,50,50);
			g.setColor(Color.red.darker());
			g.drawRect(Globals.pieces[Globals.pieceChosen].xposition+2, Globals.pieces[Globals.pieceChosen].yposition+2,48,48);
		}
 	}


Was This Post Helpful? 0
  • +
  • -

#8 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8379
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Null Pointer Exception Error

Posted 18 September 2011 - 08:04 PM

Sorry but this is horrible code

All those static variables, really bad OO design, you will have to rethink your architecture/design at least make a singleton

Your code won;t even compile, so how can you have a NULL pointer exception ?
Your AppletGUI class does not implement, as it should do, all the methods of the MouseMotionListener

I could try to cut & paste and run your code but the piece class is missing... and while at it respect Java conventions and name your class with a starting capital letter so Piece not piece

The Interface MousePressListener does not exists... so how can you have a run time error ?

This post has been edited by pbl: 18 September 2011 - 08:05 PM

Was This Post Helpful? 1
  • +
  • -

#9 jvctan16   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 16-September 11

Re: Null Pointer Exception Error

Posted 24 September 2011 - 06:34 AM

i was able to fix it using this code:

        for(int i = 0; i < 42; i++)
        {
            Globals.pieces[i] = new GG.Globals.piece();
         }



Thanks for the help :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1