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 2 of 2

Thread: exception in thread "main" java.lang.nullpointerexception (Knights Tour program)

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default exception in thread "main" java.lang.nullpointerexception (Knights Tour program)

    Can anyone help me figure out where I'm wrong in this?
    I keep getting the error "Exception in thread "main" java.lang.NullPointerException" and don't know how to fix it.

    public class Brute
     {
    	 public static void main(String[] args)
    	 {   
    		 int[][]chessBoard = new int[8][8];
    		 init(chessBoard);  
     
    		 chessBoard = traverse(chessBoard,0,0);  
    		 print(chessBoard);      
    	}  
     
    	public static int [][] traverse(int[][] board, int ver, int hor)  
    	{  
    		if(boardTraversed(board))
    		{  
    			return board; 
    		}  
     
    		board[ver][hor] = 1;  
    		if (ver > 1 && hor > 0 && board[ver-2][hor-1] == 0)  
    		{  
    			return traverse(board, ver-2, hor-1);  
    		}  
    		if(ver > 1 && hor < 7 && board[ver-2][hor+1] == 0)  
    		{  
    			return traverse(board,ver-2,hor+1);  
    		}    
    		return null;
    	}  
     
    	public static void init(int[][] array)
    	{  
    		for(int i = 0; i < array[0].length; i++)
    		{  
    			for(int a = 0; a < array.length; a++)
    			{  
    				array[i][a] = 0;  
    			}  
    		}  
    	}  
     
    	public static void print(int[][] array)
    	{  
    		for(int i = 0; i < array[0].length; i++)
    		{  
    			System.out.println();  
    			for(int a = 0; a < array.length; a++)
    			{  
    				System.out.print(" " + array[i][a] + " ");  
    			}  
    		}  
    	}     
     
    	public static boolean boardTraversed(int board[][])
    	{    
    		for(int i = 0; i < board[0].length; i++)
    			for(int a = 0; a < board.length; a++) 
    				if(board[i][a] == 0)  
    					return false;  
     
    		return true;
    	}
     }


  2. #2
    Junior Member
    Join Date
    Jul 2011
    Posts
    17
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default Re: exception in thread "main" java.lang.nullpointerexception (Knights Tour program)

    because from traverse you take back null. First, you initialize your array and later you are change her to null.

Similar Threads

  1. Exception in thread "main" java.lang.NullPointerException
    By manzili in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 9th, 2011, 01:02 PM
  2. Replies: 7
    Last Post: May 30th, 2011, 10:11 AM
  3. Exception in thread "main" java.lang.NullPointerException
    By isun in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 28th, 2011, 10:22 AM
  4. Replies: 16
    Last Post: August 27th, 2010, 04:30 PM
  5. Please help! Exception in thread "main" java.lang.NullPointerException
    By Arutha2321 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 18th, 2009, 02:25 AM