Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Exception in thread "main" java.lang.NullPointerException (when running)

807591Sep 5 2007 — edited Apr 2 2008
I am having a problem with this main class. The code compiles without error but this exception is thrown while running. The exception is at line 17 where the comment highlight.
// This progam uses the elements from the tile class to create a game of minesweeper
import java.util.Scanner;
public class minesweeper 
{
        

    public static void main(String [] args)
    {
    	Scanner Scan = new Scanner(System.in);
		String end="ii";// for end of game message
		tile[][] tileArray = new tile[10][10];// creates a matrix of tile objects
		// prints out the grid or minesweeper
		for(int i=0; i<10; i++)
		{	
			for(int j=0; j<10; j++)
			{
				//************BELOW IS LINE THAT THROWS EXCEPTION********************
				System.out.print(tileArray[i][j].toString());
				//************ABOVE IS LINE THAT THROWS EXCEPTION**************
			}
			System.out.println("");
		}
		
		boolean b = false;// the while loop
		// this loop enables the player to make moves until the game is over
		while (!b)
		{
			int x=0; int y=0; int a=0; int z=0;// ints for coordinates
			System.out.println("To uncover enter 0. To flag/unflag enter 1: ");
			int n = Scan.nextInt();
			// loop which gives a choice to the player and then does what the player chooses
			switch (n)
			{
				case '0':
				System.out.println("please enter a position to uncover. Enter one digit press enter, and then the other");
				x = Scan.nextInt(); y = Scan.nextInt();
				tileArray[x][y].uncover();
				case '1':
				System.out.println("please enter a position to flag/unflag. Enter one digit press enter, and then the other");
				a = Scan.nextInt(); z = Scan.nextInt();
				tileArray[a][z].flag();
			}
			//prints the grid
			for(int i=0; i<10; i++)
			{	
				for(int j=0; j<10; j++)
				{
					System.out.print(tileArray[i][j].toString());
				}
				System.out.println("");
				
			}
			// check...non factr for now
			if(tileArray[x][y].mineCheck()==true)
			{
				b=true;
				end = "lost";
			}
			
	
		}
		
		//won or loss message
		System.out.println("the game is over and you "+end);
    }
}
Message was edited by:
aleandro30228
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 30 2008
Added on Sep 5 2007
9 comments
279 views