Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Assigning Colors in a 2D array in a random order

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Assigning Colors in a 2D array in a random order

    Alright so basically what I'm trying to do is create a 16x16 square board of tiles (256 total) and when I run the program I need it to set up the tiles randomly into the following colors
    39 yellow tiles
    46 red tiles
    34 blue tiles
    52 green tiles
    41 magenta tiles
    44 cyan tiles

    When I run the code, I'm getting a null pointer at
    tiles[i].setBackground(Color.YELLOW);

    Heres the GUI class
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.Random;
    import java.util.Scanner;
     
    import javax.swing.JButton;
    import javax.swing.JFileChooser;
    import javax.swing.JFrame;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.JPanel;
     
    public class GameBoard extends JFrame{
    	Tile[][] myTiles;
    	private int numRows = 16, numCols = 16;
    	//private MyEventHandler eh = new MyEventHandler();
     
    	public static void main(String[] args) {
    		new GameBoard();
    	}
     
    	public GameBoard() {
    		myTiles = new Tile[numRows][numCols];
    		setSize(800,800);
     
    		Container c = getContentPane();
    		c.setLayout(new BorderLayout());
     
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    		setTitle("Random Colors");
     
    		JPanel tilePanel = buildTilePanel();
    		c.add(tilePanel, BorderLayout.CENTER);
     
    		setVisible(true);
    	}
     
    	private JPanel buildTilePanel() {
    		JPanel tilePanel = new JPanel();
    		tilePanel.setBackground(Color.BLACK);
    		tilePanel.setPreferredSize(new Dimension(800, 800));
    		tilePanel.setLayout(new GridLayout(numRows, numCols));
     
    		for (int i = 0; i < myTiles.length; i++)
    			for (int j = 0; j < myTiles[0].length; j++) {
    				myTiles[i][j] = new Tile();
    				myTiles[i][j].setColors();
    				myTiles[i][j].shuffle();
    				//myTiles[i][j].addActionListener(eh);
    				tilePanel.add(myTiles[i][j]);
    			}
    		return tilePanel;
    	}
    }

    and heres the tiles class
    import java.awt.Color;
    import java.awt.Dimension;
    import javax.swing.JButton;
     
    public class Tile extends JButton{
     
    	private Tile[] tiles = new Tile[256];
     
     
    	public Tile() {
    		setPreferredSize(new Dimension(10, 10));
    	}
     
    	public void setColors() {
    		for(int i = 0; i < 39; i++){
    			tiles[i].setBackground(Color.YELLOW);
    			paintImmediately(0, 0, getWidth(), getHeight());
     
    		}
     
    		for(int i = 39; i < 85; i++){
    			tiles[i].setBackground(Color.RED);
    			paintImmediately(0, 0, getWidth(), getHeight());
    		}
     
    		for(int i = 85; i < 119; i++){
    			tiles[i].setBackground(Color.BLUE);
    			paintImmediately(0, 0, getWidth(), getHeight());
    		}
     
    		for(int i = 119; i < 171; i++){
    			tiles[i.]setBackground(Color.GREEN);
    			paintImmediately(0, 0, getWidth(), getHeight());
    		}
     
    		for(int i = 171; i < 212; i++){
    			tiles[i].setBackground(Color.MAGENTA);
    			paintImmediately(0, 0, getWidth(), getHeight());
    		}
     
    		for(int i = 212; i < 256; i++){
    			tiles[i].setBackground(Color.CYAN);
    			paintImmediately(0, 0, getWidth(), getHeight());
    		}
    	}
     
    	public void shuffle() {
    		int r = tiles.length;
    		for(int i = 0; i < tiles.length; i++){
    			Tile t = tiles[i];
    			tiles[i] = tiles[r-1];
    			tiles[r-1] = t;
    		}
    	}
    }
    Last edited by captain; February 20th, 2012 at 10:18 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Assigning Colors in a 2D array in a random order

    I'm getting a null pointer.
    Please copy and paste here the full text of the error message.
    Look at the line number referenced in your program and find the variable that has a null value. Then backtrack in the code to see why that variable does not have a non-null value.

  3. #3
    Junior Member
    Join Date
    Feb 2012
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Assigning Colors in a 2D array in a random order

    Exception in thread "main" java.lang.NullPointerException
    at Tile.setColor(Tile.java:16)
    at GameBoard.buildTilePanel(GameBoard.java:55)
    at GameBoard.<init>(GameBoard.java:39)
    at GameBoard.main(GameBoard.java:26)

    which would be
    tiles[i].setBackground(Color.YELLOW);

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Assigning Colors in a 2D array in a random order

    Is the value of tiles[i] null?
    If so, where do you assign it a value?
    You have defined an array, but you have not put any objects into the array. All its slots are empty.
    You must assign a value to all the slots in the array before you can call a method using the value of a slot.

  5. #5
    Junior Member
    Join Date
    Feb 2012
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Assigning Colors in a 2D array in a random order

    Well my thought process for that piece of code was that it would set the i-th tile in the array to yellow. I'm not sure where I would assign a value to it initially though.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Assigning Colors in a 2D array in a random order

    The problem is there is no tile in the array at the i-th slot. You need to assign tiles to the array before you can set their colors.
    where I would assign a value to it
    Any time before you want to access it.

Similar Threads

  1. [SOLVED] Random Colors Problem
    By captain in forum What's Wrong With My Code?
    Replies: 11
    Last Post: February 23rd, 2012, 10:40 PM
  2. Random variable order.
    By Alex555 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 31st, 2012, 10:27 AM
  3. [SOLVED] Assigning Values to Multi Array
    By dredjohn in forum Collections and Generics
    Replies: 3
    Last Post: April 18th, 2011, 07:43 PM
  4. prininting out a 4x4 array according to an order by a 1d array
    By fortune2k in forum Collections and Generics
    Replies: 7
    Last Post: November 25th, 2010, 12:54 PM
  5. Assigning variable to an Array
    By sridevi in forum Collections and Generics
    Replies: 3
    Last Post: August 10th, 2009, 10:58 PM