Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
its giving me this error and i dont know how to fix it:
Exception in thread "main" java.lang.NullPointerException
	at Testing.main(Testing.java:61)


What I have tried:

Java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Testing 
{
	public static void main(String args[]) 
	   {
	      
		 List<String> teams = new ArrayList<String>();
		 // List<String> ghosts = new ArrayList<String>();
		 // HashMap<String, List<Integer>> map = new HashMap<String, List<Integer>>();
		  String ghoststr;
		      
		  teams.add("A");
		  teams.add("B");
		  teams.add("C");
		  teams.add("D");
		  teams.add("E");
		  
		  
		  int ghost = teams.size() % 2;
		  
		  ghoststr = "GHOST";
		  if (ghost == 1 ) 
		  {
			  
			  teams.add(ghoststr);
			  //ghosts.add("GHOST");
		  }
		  
		 //teams.add("G");
		  //teams.add("F");
		  
		  
		int totalRounds = teams.size()-1;
		int matchesPerRound = teams.size()/2;
		Game[][] rounds = new Game[totalRounds][matchesPerRound];
		//String ght = teams.get(teams.size()-1);
		
		
		for (int round = 0; round< totalRounds; round++)
		{
				
			
			for (int match = 0; match < matchesPerRound; match++) 
			{
				int home = (round + match) % (teams.size() - 1);
	            int away = (teams.size() - 1 - match + round) % (teams.size() - 1);
	            String team1 = teams.get(home);
		        String team2 = teams.get(away);
		        
	            if (match == 0)
	            {
	            	away = teams.size()-1;
	            	
	            }
	            
	            
	            	//if ( team1 != team2)
	             rounds[round][match].team1= team1; 
	             rounds[round][match].team2 = team2;
		       //rounds[round][match] = Game.team1 + " v " + team2 ;		
	            	
	            	
	            
	            
			}
		}
		
		
		
		 for (int i = 0; i < rounds.length; i++) {
			 for(int j = 0; j < rounds.length ; j++)
			 {
				 System.out.println("Round " + (i + 1));
				 if ( !(rounds[i][j].team1.equals(ghoststr) || rounds[i][j].team2.equals(ghoststr)) )
		         {
					String printstr = rounds[i][j].team1 + "V" + rounds[i][j].team2;
					System.out.println(printstr);
					System.out.println();
		         }
				
				 
			 }
	         
	     }

				
				}

}
class Game 
{
	String team1;
	String team2;
}
Posted
Updated 6-Apr-17 0:31am
Comments
CHill60 6-Apr-17 5:54am    
Is the line giving the error rounds[round][match].team1= team1;?
Member 13110105 6-Apr-17 6:04am    
yes
CHill60 6-Apr-17 6:17am    
Then I suspect you have not set rounds[round][match] to an instance of the Games object.
I suspect you need a line similar to rounds[round][match] = new Game;
Member 13110105 6-Apr-17 6:24am    
Thank you but now the if statement in the code below is giving me a problem:

for (int i = 0; i < rounds.length; i++) {
for(int j = 0; j < rounds.length ; j++)
{
System.out.println("Round " + (i + 1));
if ( !(rounds[i][j].team1.equals(ghoststr) || rounds[i][j].team2.equals(ghoststr)) )
{
String printstr = rounds[i][j].team1 + "V" + rounds[i][j].team2;
System.out.println(printstr);
System.out.println();
}


}

}

the error is :

java.lang.ArrayIndexOutOfBoundsException: 3
at Testing.main(Testing.java:78)
CHill60 6-Apr-17 6:28am    
You have run off the end of the array by the sound of it. Debug and check the values of i, j and rounds.length

1 solution

From comments.

OP had declared the array but had not created new instances of the object when populating it.

Something similar to this was needed:
rounds[round][match] = new Game;
rounds[round][match].team1= team1; 
rounds[round][match].team2 = team2;
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900