This week's book giveaway is in the Java in General forum.
We're giving away four copies of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 and have ishori Sharan & Adam L Davis on-line!
See this thread for details.
Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Grid Alignment and Null Exception

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Either e or myApp is null. You can find out by an if check which it is.
reply
    Bookmark Topic Watch Topic
  • New Topic