It's not a secret anymore!*
The moose likes Beginning Java and the fly likes Grid Alignment and Null Exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Grid Alignment and Null Exception" Watch "Grid Alignment and Null Exception" New topic
Author

Grid Alignment and Null Exception

capp luckett
Ranch Hand

Joined: Dec 07, 2000
Posts: 33
Hi Experts:
I am working on a Minesweeper program, in a 3x3 grid.
I have two major problems. First, I get a "java.lang.NullPointerException" at the statement "myApp.getPoint(e.getX(), e.getY());". I don't know why.
Second, I have trouble getting my grid to divide up evenly.
I have set up a Demension of 300x300 (html:<applet code="Game.class" width = 400 height = 400>".
Thanks
cappertan
My code is as follows:
import javax.swing.*;
import java.awt.*;
import java.awt.Graphics;
import java.awt.event.*;
public class Game extends JApplet
{
int ON = 0;
int x;
int y;
int i, j;
int OFF = 0;
int WIDTH = 300;
int mineField[][];
int counterOn = 0;
int counterOff = 0;
int end = 0;
int hits;
int misses;
int total;
int b;
int dX, dY;
public void init()
{

x = -5;
y = -5;
total = 0;
mineField = new int [3][3];
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
mineField[i][j] = OFF;
}

hits = 0;
misses = 0;
total = 0;
b = 0;

}


MouseClickedListener myListener = new MouseClickedListener(this);
this.addMouseListener(myListener);
}
public void paint(Graphics g)
{

// set the color for the rectangle
//g.setColor(Color.gray);

// fill the rectangle with the color light grey
//g.fillRect((getSize().width - 200) /2, WIDTH, WIDTH, WIDTH);
// set the color for the grid
g.setColor(Color.black);
Dimension d = new Dimension ( 300, 300);
dX = d.width / 3;
dY = d.height / 3;
g.drawRect(dX - 15, 100, 230, 250);
//g.drawRect(dX - 15, 2*dY + 70, 200, 200);
g.drawLine(dX + 50, 100, dX + 50, d.height + 50 );
g.drawLine(2*dX + 20, 100, 2*dX + 20, d.height + 50);

//g.drawLine(dX + 5, 2*dY, d.width, 2*dY);

g.drawLine(dX - 15, 2*dY + 70, d.width + 15, 2 * dY + 70);
g.drawLine(dX - 15, (2*dY)-25, d.width + 15, (2 * dY) - 25);

if (total == 1)
{
g.setColor(Color.black);
g.fillOval(x, y, 20, 20);
g.setColor(Color.red);
g.drawLine( 15, 15, 15, 15);
hits++;
total++;
}

}
public void update(Graphics g)
{

// fill the rectangle with the color light grey
g.clearRect(0, 0, getWidth(), getHeight());
paint(g);

}

public void getPoint(int x, int y)
{
this.x = x;
this.y = y;
//this.b = b;
}

}
class MouseClickedListener extends MouseAdapter
{
// Instance Variable of the Applet class
Game myApp;

// Constructor of the MouseClickedListener

//MouseClickedListener(Minesweeper myApp)
MouseClickedListener(Game myApp)


{
myApp = myApp;

}
public void mouseClicked(MouseEvent e)

{


myApp.getPoint(e.getX(), e.getY());


myApp.repaint();
//myApp.total++;
}
}

flodin
Paul Stevens
Ranch Hand

Joined: May 17, 2001
Posts: 2823
Either e or myApp is null. You can find out by an if check which it is.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Grid Alignment and Null Exception