Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Grid Alignment and Null Exception

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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++;
}
}
 
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Either e or myApp is null. You can find out by an if check which it is.
 
A "dutch baby" is not a baby. But this tiny ad is baby sized:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic