Results 1 to 4 of 4
  1. #1
    Mapisto is offline Senior Member
    Join Date
    Dec 2011
    Posts
    102
    Rep Power
    0

    Default Why won't it work? NullPointerException

    Hi,

    I've written this program which supposed to create a checkmate board.

    Though when I've tried to run it, it'd stop on line 45 and write "Exception in thread "main" java.lang.NullPointerException"

    What does it mean?


    Java Code:
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.border.LineBorder;
    import java.awt.Color;
    import java.awt.GridLayout;
    import java.awt.Dimension;
    
    
    public class Checkmate {
    
    	
    	public static void main(String[] args) {
    		new CheckMBoard();
    
    	}
    
    }
    
    class CheckMBoard extends JFrame{
    	
    	private GridLayout grid= new GridLayout(8,8);
    	private JPanel[][] checkers= new JPanel[8][8]; 
    	private JLabel[][] nums= new JLabel[8][8]; 
    	private LineBorder border= new LineBorder(Color.BLACK,2);
    	Dimension size= new Dimension(5,5);
    	
    	
    	public CheckMBoard (){
    		setFrame();
    		setCheckers();
    		addComponents();
    		
    		
    		
    		
    	}
    	
    	
    	private void setCheckers(){
    		for (int j=0; j<8; j++){
    			if (j%2==0){
    				for(int k=0; k<8; k++){
    					if (k%2==0){
    						checkers[j][k].setBackground(Color.BLUE);
    						
    					}
    					else{
    						checkers[j][k].setBackground(Color.YELLOW);
    						
    					}
    					nums[j][k]= new JLabel(((j+1)*8+(k+1))+"",JLabel.CENTER);
    					checkers[j][k].add(nums[j][k]);
    					checkers[j][k].setBorder(border);
    					checkers[j][k].setOpaque(true);
    					checkers[j][k].setPreferredSize(size);
    				}
    				
    			}
    			else{
    				for (int k=0; k<8; k++){
    					if (k%2==0){
    						checkers[j][k].setBackground(Color.YELLOW);
    						
    					}
    					else{
    						checkers[j][k].setBackground(Color.BLUE);
    						
    					}
    					nums[j][k]= new JLabel(((j+1)*8+(k+1))+"",JLabel.CENTER);
    					checkers[j][k].add(nums[j][k]);
    					checkers[j][k].setBorder(border);
    					checkers[j][k].setOpaque(true);
    					checkers[j][k].setPreferredSize(size);
    				}
    			}
    		}
    		
    		
    		
    		
    		
    		
    		
    		
    	}
    	
    	private void setFrame(){
    		//pack();
    		setVisible(true);
    		setAlwaysOnTop(isAlwaysOnTop());
    		setResizable(false);
    		setLocationRelativeTo(null);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setLayout(grid);
    		
    		
    	}
    	
    	private void addComponents(){
    		for (int j=0; j<8; j++){
    			for(int k=0; k<8; k++){
    				add(checkers[j][k]);
    				
    			}
    			
    			
    		}
    		
    		
    	
    	}
    	
    	
    	
    	
    	
    	
    }




    Thanks!

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is online now Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,234
    Rep Power
    19

    Default Re: Why won't it work? NullPointerException

    1. When posting code to a forum, please remove unnecessary blank lines. Go through the Code Conventions for the Java Programming Language: Contents

    2. Declaring an array does not automagically fill it. The elements of a newly declared array assume default values; zero for numeric primitives, false for boolean and null for object types.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Mapisto is offline Senior Member
    Join Date
    Dec 2011
    Posts
    102
    Rep Power
    0

    Default Re: Why won't it work? NullPointerException

    Thanks!
    I'll check it right away

  4. #4
    Mapisto is offline Senior Member
    Join Date
    Dec 2011
    Posts
    102
    Rep Power
    0

    Default Re: Why won't it work? NullPointerException

    Omg, I feel so stupid :|
    This isn't the first time it happens to me.. I should have figured it on my own :(

    Thanks again!

Similar Threads

  1. NullPointerException
    By mangesh.gho in forum New To Java
    Replies: 11
    Last Post: 01-21-2012, 06:19 AM
  2. applet call dll work in Win2000 but not work in WinXP
    By manhcuongtin4 in forum Java Applets
    Replies: 1
    Last Post: 07-14-2011, 01:45 PM
  3. NullPointerException
    By Diz in forum New To Java
    Replies: 10
    Last Post: 05-13-2011, 02:58 AM
  4. NullPointerException
    By jayragz in forum NetBeans
    Replies: 5
    Last Post: 05-12-2011, 05:19 PM
  5. NullPointerException
    By mensa in forum Java 2D
    Replies: 5
    Last Post: 05-03-2008, 11:19 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •