#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    7
    Rep Power
    0

    Exception in thread "main" java.lang.NullPointerException


    I am trying to write a program for a college project which is based on a game similar to battleship. I keep hitting the following error and i don't know where i am going wrong as i cannot see any errors in the code in Eclipse.

    Exception in thread "main" java.lang.NullPointerException
    at Board.playerTurn(Board.java:117)
    at Board.main(Board.java:132)

    Here's the lines that the error is showing:
    Code:
    public void playerTurn(){
        		int turn= 0;
        		while(turn<3){
        			if(turn==0){
        				System.out.println("Offending player's turn");
        				offend = new Ship();
        				System.out.println("Defending person's turn");
        				defend = new Ship();
        			}
        			else{
        				if(turn%2==0){
        					off.place(offensive);
        					while(offend.place(offensive)!=1){}
        					createBoard();
        				}
        				else{
        					//THIS LINE
                                           def.place(defensive);
        					while(defend.place(defensive)!=1){}
        					createBoard();
        				}
        			}
        			turn++;
        		}
        		
        	}
    
        	public static void main(String[] args) {
    
        		Board b= new Board();
        		b.initBoard();
        		b.createBoard();
                       //AND THIS LINE    		
                       b.playerTurn();
        	}

    Here's the complete code so far:
    Code:
    public class Board {
    	
    	private int row;
    	private int col;
    	public Square[][] offensive = null;
    	public Square[][] defensive = null;
    	Ship offend;
    	Ship defend;
    	Mine off;
    	Mine def;
    	    	
        	private Scanner scan = new Scanner(System.in);
        	
        	public void initBoard(){
        		System.out.println("Enter the rows for the board:");
        		row = scan.nextInt();
        		System.out.println("Enter the columns for the board:");
        		col = scan.nextInt();
        		
        		defensive = new Square[row][col];
        		offensive = new Square[row][col];
        		
        		for(int i=0; i<row; i++){
        			for(int j=0; j<col; j++){
        				defensive[i][j] = new Square();
        				offensive[i][j] = new Square();
        			}
        		}
        	}
        	
        	public void createBoard(){
        		System.out.println("Initializing boards...");	
        		 
        		System.out.println("Defensive Board");
        		for(int i=0; i<=row; i++){
        			System.out.print("    "+ i);
        		}	
        		System.out.println();
        		
        		int column = 0;
        		for(int i=0; i<row; i++){
        			System.out.print(column++);
        			for(int j=0; j<col; j++){
        				if(defensive[i][j].getState()== -1){
        					System.out.print("    " +'-');
        				}
        				else if(defensive[i][j].getState()==0){
        					System.out.print("    " +'*');
        				}
        				else if(defensive[i][j].getState()==1){
        					System.out.print("    " +'X');
        				}
        				else if(defensive[i][j].getState()==2){
        					System.out.print("    " + 'S');
        				}
        				else{
        					System.out.println("    " +'M');
        				}
        			}
        			System.out.println();
        		}
        		
        		System.out.println();
        		
        		System.out.println("Offensive Board");
        		for(int i=0; i<=row; i++){
        			System.out.print("    "+i);
        		}	
        		System.out.println();
        		
        		column = 1;
        		for(int i=0; i<row; i++){
        			System.out.print(column++);
        			for(int j=0; j<col; j++){
        					
        				if(offensive[i][j].getState()== -1){
        					System.out.print("    " +'-');
        				}
        				else if(offensive[i][j].getState()==0){
        					System.out.print("    " +'*');
        				}
        				else if(offensive[i][j].getState()==1){
        					System.out.print("    " +'X');
        				}
        				else if(offensive[i][j].getState()==2){
        					System.out.print("    " + 'S');
        				}
        				else{
        					System.out.println("    " + 'M');
        				}
        			}
        			System.out.println();
        		}
        		System.out.println("Boards Initialized");
        	}
        	
        	public void playerTurn(){
        		int turn= 0;
        		while(turn<3){
        			if(turn==0){
        				System.out.println("Offending player's turn");
        				offend = new Ship();
        				System.out.println("Defending person's turn");
        				defend = new Ship();
        			}
        			else{
        				if(turn%2==0){
        					off.place(offensive);
        					while(offend.place(offensive)!=1){}
        					createBoard();
        				}
        				else{
        					def.place(defensive);
        					while(defend.place(defensive)!=1){}
        					createBoard();
        				}
        			}
        			turn++;
        		}
        		
        	}
    
        	public static void main(String[] args) {
    
        		Board b= new Board();
        		b.initBoard();
        		b.createBoard();
        		b.playerTurn();
        	}
    
        
        }
    
    public class Mine extends Square {
    	private Scanner scan = new Scanner(System.in);
    	
    	public void place(Square[][] board){
    		System.out.println("Enter the row of the board where you want to place the ship");
    		int place_row = scan.nextInt();
    		System.out.println("Enter the column of the board where you want to place the ships");
    		int place_column = scan.nextInt();
    		board[place_row][place_column].setState(3);
    	}
    	
    	public static void main(String[] args) {
    		new Mine();
    	}
    
    }
    
    public class Square {
    	private int state;
    
    	public Square(){
    		this.state = -1;
    	}
    	
    	public int getState() {
    		return state;
    	}
    
    	public void setState(int state) {
    		this.state = state;
    	}
    }
    
    public class Ship extends Square {
    	private enum Ships {Battleship, Minesweeper, Destroyer, SubMarine};
    	private Ships[] _battleship = null;
    	private Ships[] _minesweeper= null;
    	private Ships[] _destroyer = null;
    	private Ships[] _submarine = null;
    	private Scanner  scan= new Scanner(System.in);
    	
    
    	
    	public int no_of_battleship = 0;
    	public int no_of_minesweeper = 0;
    	public int no_of_destroyer = 0;
    	public int no_of_submarine = 0;
    	
    	public int index_of_battleship = 0;
    	public int index_of_minesweeper = 0;
    	public int index_of_destroyer = 0;
    	public int index_of_submarine = 0;
    	public int total_number_ships =0;
    	
    	public Ship(){
    		
    		System.out.println("How many battleships?");
    		int battleship = scan.nextInt();
    		no_of_battleship = battleship; 
    		if(battleship==0){ }
    		else{
    			_battleship = new Ships[battleship];
    			for(int i=0; i<battleship; i++){
    				_battleship[i] = Ships.Battleship;
    			}
    		}
    		
    		System.out.println("How many Minesweeper?");
    		int minesweeper = scan.nextInt();
    		no_of_minesweeper = minesweeper;
    		if(minesweeper==0){ }
    		else{
    			_minesweeper = new Ships[minesweeper];
    			for(int i=0; i<minesweeper; i++){
    				_minesweeper[i] = Ships.Minesweeper;
    			}
    		}
    		
    		
    		System.out.println("How many Destroyer?");
    		int destroyer = scan.nextInt();
    		no_of_destroyer = destroyer;
    		if(destroyer==0){ }
    		else{
    			_destroyer = new Ships[destroyer];
    			for(int i=0; i<destroyer; i++){
    				_destroyer[i] = Ships.Destroyer;
    			}
    		}
    		
    		System.out.println("How many submarine?");
    		int submarine = scan.nextInt();
    		no_of_submarine = submarine;
    		if(submarine==0){ }
    		else{
    			_submarine = new Ships[submarine];
    			for(int i=0; i<submarine; i++){
    				_submarine[i] = Ships.SubMarine;
    			}
    		}
    		total_number_ships = no_of_battleship + no_of_minesweeper + no_of_destroyer + no_of_submarine; 
    		System.out.println("Ships are initialized");
    	}
    	
    	public int placeBattleship(Square[][] board,int row, int col, int row_d, int col_d){
    		if(board[row][col].getState()==1){
    			board[row_d][col_d].setState(1);
    			board[row][col].setState(-1);
    			return 1;
    		}
    		else{
    			return 0;
    		}
    		
    	}
    	
    	public int place(Square[][] board){
    		System.out.println("Enter the row of the board where you want to place the ship");
    		int place_row = scan.nextInt();
    		int count = 0;
    		System.out.println("Enter the column of the board where you want to place the ships");
    		int place_column = scan.nextInt();
    		if(total_number_ships!=0){
    			System.out.println("What kind of ship you want to place on board?");
    			int counter = 1;
    			for(Ships s: Ships.values()){
    				System.out.println(""+counter+ s);
    			}
    			
    			switch(scan.nextInt()){
    			case 1:
    					if(no_of_battleship!=0){
    						no_of_battleship--;
    						count = 1;
    					}
    					else{
    						System.out.println("No more battleships");
    						return 0;
    					}
    				break;
    			case 2:
    				if(no_of_minesweeper!=0){
    					no_of_minesweeper--;
    					count = 1;
    				}
    				else{
    					System.out.println("No more Minesweeper");
    					return 0;
    				}
    				break;
    			case 3:
    				if(no_of_destroyer!=0){
    					no_of_destroyer--;
    					count = 1;
    				}
    				else{
    					System.out.println("No more Destroyer");
    				}
    				break;
    			case 4:
    				if(no_of_submarine!=0){
    					no_of_submarine--;
    					count = 1;
    				}
    				else{
    					System.out.println("No more submarine");
    					return 0;
    				}
    				break;
    			default: 
    			System.out.println("Ship not defined");
    				return 0;
    			}
    			board[place_row][place_column].setState(2);
    			total_number_ships--;
    		
    		}
    		return count;
    		
    	}
    	public static void main(String[] args) {
    		new Ship();
    	}
    
    }
    The game allows me to enter numbers to set up the board and the problem seems to be on the functionallity of the turn system. Any help would be really appreciated cheers!
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    you never define def (or off). I don't know what you expect to get when using these unset attributes, but they're both null.

    Why do you have a main method for every class?
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".

IMN logo majestic logo threadwatch logo seochat tools logo