Sorry about this messy formatting. As a beginner in java i got no idea which part of the code is creating this errors so i have posted the whole program here. Here i tried to make the monopoly game of two players with some basic things like moving the player on the game board according to the dice rolled. it even runs and works accurate but after some play the error hits on to the console view. Can someone tell me what in the code is causing that run-time errors? Any kind of help will be helpful to me. and let me know if you want to know more about this because i've not posted the other classes here like Property,Player, main class etc.. I hope i will get some solution here..
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Board extends JPanel implements ActionListener
{
//timer object would call the method actionPerfored at every 25 mili-seconds
private Timer timer;
//Create spaces on the game board
Property prop[] = new Property[40];
//Create Players
Player p[] = new Player[2];
//some local helpful variables
int et, dice1, dice2, pflag;
static int k;
Board ()
{
//Initialization of all Spaces on the Game Board
prop[0] = new Property(632, 637, 0, 0, "Go", 1);
prop[1] = new Property(557, 580, "Mediterranean Avenue", 60, 2);
prop[2] = new Property(506, 580, 0, 0, "Community Chest", 1);
prop[3] = new Property(449, 580, "Baltic Avenue", 60, 4);
prop[4] = new Property(392, 580, 0, 0, "Income Tax", 1);
prop[5] = new Property(337, 580, "Reading Railroad", 300, 25);
prop[6] = new Property(283, 580, "Oriental Avenue", 100, 6);
prop[7] = new Property(227, 580, 0, 0, "Chance", 1);
prop[8] = new Property(172, 580, "Vermont Avenue", 100, 6);
prop[9] = new Property(116, 580, "Connecticut Avenue", 120, 8);
prop[10] = new Property(13, 668, 58, 624, "Just Visiting", 1);
prop[11] = new Property(100, 565, "St. Charles Place", 140, 10);
prop[12] = new Property(100, 509, "Electric Company", 150, 0);
prop[13] = new Property(100, 455, "States Avenue", 140, 10);
prop[14] = new Property(100, 398, "Virginia Avenue", 160, 12);
prop[15] = new Property(100, 348, "Pennsylvania Railroad", 300, 25);
prop[16] = new Property(100, 286, "St. James Place", 180, 14);
prop[17] = new Property(100, 231, 0, 0, "Community Chest", 1);
prop[18] = new Property(100, 176, "Tennessee Avenue", 180, 14);
prop[19] = new Property(100, 119, "New York Avenue", 200, 16);
prop[20] = new Property(18, 72, 0, 0, "Free Parking", 1);
prop[21] = new Property(114, 103, "Kentucky Avenue", 220, 18);
prop[22] = new Property(167, 103, 0, 0, "Chance", 1);
prop[23] = new Property(223, 103, "Indiana Avenue", 220, 18);
prop[24] = new Property(279, 103, "Illinois Avenue", 240, 20);
prop[25] = new Property(332, 103, "B. & O. Railroad", 300, 25);
prop[26] = new Property(390, 103, "Atlantic Avenue", 260, 22);
prop[27] = new Property(447, 103, "Ventnor Avenue", 260, 22);
prop[28] = new Property( 505, 103, "Water Works", 150, 0);
prop[29] = new Property(559, 103, "Marvin Gardens", 280, 24);
prop[30] = new Property(637, 47, 0, 0, "Go to Jail", 1);
prop[31] = new Property(573, 123, "Pacific Avenue", 300, 26);
prop[32] = new Property(573, 174, "North Carolina Avenue", 300, 26);
prop[34] = new Property(573, 285, "Pennsylvania Avenue", 320, 28);
prop[35] = new Property(573, 340, "Short Line", 300, 25);
prop[36] = new Property(573, 398, 0, 0, "Chance", 1);
prop[37] = new Property(573, 452, "Park Place", 350, 35);
prop[38] = new Property(573, 509, 0, 0, "Luxary Tax", 1);
prop[39] = new Property(573, 563, "Boardwalk", 400, 50);
//Initialization of the Players
p[0] = new Player();
p[1] = new Player();
addKeyListener (new Al());
setFocusable(true);
timer = new Timer(25,this);
timer.start();
}
public void actionPerformed(ActionEvent e)
{
repaint();
}
public void paint(Graphics g)
{
super.paint(g);
BufferedImage bgImage = readImage("C:/javagame/m.jpg");
BufferedImage fgImage1 = readImage("C:/javagame/p1.png");
BufferedImage fgImage2 = readImage("c:/javagame/p2.png");
BufferedImage overlayedImage;
overlayedImage = overlayImages (bgImage, fgImage1, fgImage2, prop[p[0].cur].x, prop[p[0].cur].y, prop[p[1].cur].x, prop[p[1].cur].y);
g.drawImage(overlayedImage,0,0,null);
g.drawString(String.valueOf(dice1),700,500);
g.drawString(String.valueOf(dice2),700,510);
}
//Here in this method the player's images will be overlayed over the Game Board's Image
public static BufferedImage overlayImages(BufferedImage bgImage,BufferedImage fgImage1, BufferedImage fgImage2, int x1, int y1, int x2, int y2)
{
Graphics2D g2 = bgImage.createGraphics();
g2.drawImage(bgImage, 0, 0, null);
g2.drawImage(fgImage1, x1, y1, null);
g2.drawImage(fgImage2, x2, y2, null);
g2.dispose();
return bgImage;
}
public static BufferedImage readImage(String fileLocation)
{
BufferedImage img = null;
try
{
img = ImageIO.read(new File(fileLocation));
}
catch (IOException e)
{
e.printStackTrace();
}
return img;
}
//Here the Dice Throw Simulation will take place
public static int roll()
{
if (k == 6)
{
k = 0;
}
k++;
return k;
}
public class Al extends KeyAdapter
{
public void keyPressed (KeyEvent e)
{
int kc = e.getKeyCode();
//if Player has pressed for the R (Dice throw/roll) then roll the dice and move accordingly on the Game Board
if (kc == KeyEvent.VK_R && et == 0)
{
et = 1;
dice1 = roll();
dice2 = roll();
p[pflag].cur = p[pflag].cur + dice1 + dice2;
//If player's position exceeds the 39th space
if (p[pflag].cur > 39)
{
p[pflag].cur = p[pflag].cur - 40;
}
}
//If player has pressed for the E then end the current player's turn and pass the turn to the next player
else if (kc == KeyEvent.VK_E && et == 1)
{
if (pflag == 0)
{
et = 0;
pflag = 1;
}
else
{
et = 0;
pflag = 0;
}
}
}
}
}
Here are those Non stop errors i get ar run-time
at javax.swing.JComponent.paintToOffscreen(Unknown Source) at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source) at javax.swing.RepaintManager$PaintManager.paint(Unknown Source) at javax.swing.RepaintManager.paint(Unknown Source) at javax.swing.JComponent._paintImmediately(Unknown Source) at javax.swing.JComponent.paintImmediately(Unknown Source) at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source) at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source) at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source) at javax.swing.RepaintManager.access$700(Unknown Source) at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source) at java.awt.event.InvocationEvent.dispatch(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$000(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source) Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at Board.paint(Board.java:101) at javax.swing.JComponent.paintToOffscreen(Unknown Source) at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source) at javax.swing.RepaintManager$PaintManager.paint(Unknown Source) at javax.swing.RepaintManager.paint(Unknown Source) at javax.swing.JComponent._paintImmediately(Unknown Source) at javax.swing.JComponent.paintImmediately(Unknown Source) at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source) at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source) at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source) at javax.swing.RepaintManager.access$700(Unknown Source) at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source) at java.awt.event.InvocationEvent.dispatch(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$000(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source)