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

Thread: Exception in thread "main" java.lang.NullPointerException - And everything else...

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Exception in thread "main" java.lang.NullPointerException - And everything else...

    Hello, I have a project that requires that I make a Pokemon program to simulate what would occur in an ordinary Pokemon game, without the graphics involved. I am continually coming across the same error Exception in thread "main" java.lang.NullPointerException in multiple different areas, as well as for numerous different variables. If anyone can help me out, I'd greatly appreciate it. Thanks in advance.

    //Pokemon Arena
     
     
    import java.util.*;
    import java.io.*;
    import java.awt.geom.*;
     
     
    //WHAT GOES WHERE NOW???
     
     
     
    public class PokemonArena{
    	private static ArrayList<Pokemon>pokes = new ArrayList<Pokemon>(); 
    	private static ArrayList<Pokemon>fakepokes = new ArrayList<Pokemon>(); 
    	private static ArrayList <Pokemon> goodPokemon = new ArrayList<Pokemon>();
    	private static ArrayList <Pokemon> enemyPokemon = new ArrayList<Pokemon>();
     
    	private static ArrayList <Attack> attacks = new ArrayList<Attack>();
    	private static ArrayList <Attack> goodPokemonAttacks = new ArrayList<Attack>();
     
    	public static Pokemon currentPokemon; //private or public
    	public static Pokemon currentEnemy;	//private or public
     
    	public static int numPokes;
    	public static int numPokes2;
    	public static int numPokes3;
    	public static int turn;
    	public static boolean isAlive;
     
    	public static int userStunPass;
    	public static int enemyStunPass;
     
    	public static double damageEffect;
     
    	public static boolean gpDisabled;
    	public static boolean epDisabled;
     
    //LOADS ALL POKEMON GROUPS & ATTACKS
    	public static void loadAll () throws IOException{
    		Scanner pokemonStats = new Scanner(new BufferedReader(new FileReader("pokemon.txt")));
    		Scanner pokemonStats2 = new Scanner(new BufferedReader(new FileReader("pokemon.txt")));	
    		Scanner pokemonStats3 = new Scanner(new BufferedReader(new FileReader("pokemon.txt")));	
     
    		numPokes = Integer.parseInt(pokemonStats.nextLine());
    		for (int i=0; i<numPokes; i++){
    			pokes.add(new Pokemon(pokemonStats.nextLine()));
    		}
     
    		numPokes2 = Integer.parseInt(pokemonStats2.nextLine());	
    		for (int i=0; i<numPokes2; i++){
    			fakepokes.add(new Pokemon (pokemonStats2.nextLine()));
    		}
     
    //		numPokes3 = Integer.parseInt(pokemonStats3.nextLine());
    //		for (int i=0; i<numPokes3; i++){
    //			fakepokes.add(new Pokemon (pokemonStats3.nextLine()));
    //			attacks.add(new Attack(pokemonStats3.nextLine()));
    //		}
     
    	}
     
    	public static void main (String [] args) throws IOException{	
    		loadAll();										//loads all data from the pokemon.txt file
    		setUpGame();
    		setUpBattle();
    	}
     
    //IMPORTANT PROCEDURES THAT TAKE PLACE AT THE BEGINNING OF THE GAME
    	public static void setUpGame(){	
    		findGoodPokemon();
    		findEnemyPokemon();
    		setTurn();
    	}
     
    //IMPORTANT PROCEDURES THAT TAKE PLACE BEFORE EACH BATTLE
    	public static void setUpBattle(){
    		findCurrentEnemy();
    		findCurrentPokemon();
    		manageTurn();
    		changeTurn(userStunPass, enemyStunPass);
    		manageTurn();
    		unableToBattle();
    		healGoodPokemon();
     
    	}	
     
    //CREATES GOOD POKEMON GROUP AND ENEMY POKEMON GROUP
    	public static ArrayList findGoodPokemon (){
    		for (int i=0; i<numPokes; i++){
    			System.out.printf("%d)%s\n", i, pokes.get(i).getName());
    		}
     
    		String [] places = {"first", "second", "third", "fourth"};
    		String [] numbers = {"four", "three", "two", "one"};
     
     
    		for (int i=0; i<4; i++){
    			System.out.printf("You must choose %s pokemon. \nWhat is the %s pokemon that you choose? Select option by inputting corresponding number.\n", numbers[i], places[i]);
    			Scanner kb = new Scanner (System.in);
    			int pos = kb.nextInt();
     
    			goodPokemon.add(pokes.get(pos));
    			pokes.remove(pokes.get(pos));
    		}
     
    		System.out.printf("\nYour Pokemon are:\n");
    		for (int i=0; i<goodPokemon.size(); i++){
    			System.out.print(goodPokemon.get(i).getName() + "\n");
    		}
     
    		return goodPokemon;
    	}
     
    	public static ArrayList findEnemyPokemon (){
    		for (int i=0; i<pokes.size(); i++){
    			enemyPokemon.add(pokes.get(i));
    		}
    	return enemyPokemon;
    	}
     
     
    //FINDS CURRENT GOOD POKEMON
    	public static Pokemon findCurrentPokemon(){
    		System.out.printf(" Who will battle against this Pokemon? Select option by inputting corresponding number.\n");//, currentEnemy.getName());
     
    		for (int i=0; i<goodPokemon.size(); i++){
    			System.out.println(i + ")" + goodPokemon.get(i).getName());
    		}
     
    		Scanner kb = new Scanner(System.in);
    		int currentPos = 0;
    		currentPos = kb.nextInt();
    		Pokemon currentPokemon = fakepokes.get(currentPos);
    		currentPokemon = goodPokemon.get(currentPos);
    		System.out.printf("%s, I choose you!", currentPokemon.getName());
    		return currentPokemon;
    	}
     
    //FINDS CURRENT ENEMY
    	public static Pokemon findCurrentEnemy(){		//inside the bracket is what you put into the method, outside is what the method outputs
    		int enemyPos = (int)(Math.random()*enemyPokemon.size());
    		Pokemon currentEnemy = enemyPokemon.get(enemyPos);
    		System.out.printf("\nThe current enemy is %s", currentEnemy.getName() + ".");
    		return currentEnemy;
    	}	
     
    //RECHARGES ALL GOOD POKEMON AND CURRENT ENEMY
    	public static void rechargeAll(){
    		for (int i=0; i<goodPokemon.size(); i++){
    			goodPokemon.get(i).recharge(10);
    		}
    	}
     
     
    //HEALS ALL GOOD POKEMON AFTER END OF ROUND
    	public static void healGoodPokemon(){
    		for (int i=0; i<goodPokemon.size(); i++){
    			goodPokemon.get(i).healAll();
    			goodPokemon.get(i).checkHP();
    		}
    	}		
     
    //RANDOMLY ASSIGNS THE TURN AT THE BEGINNING OF EACH BATTLE
    	public static void setTurn(){	
    		//user turn = 1;		
    		//enemy turn = 2;			
    		turn = (int)(Math.random()*2);
     
    		userStunPass = 2;
    		enemyStunPass = 2;	
    	}
     
    //DETERMINES THE TURN AFTER EACH TURN TAKES PLACE
    	public static void changeTurn(int UserStunPass, int EnemyStunPass){										
    		if (turn == 1){
    			if (userStunPass == 1){
    				turn = 1;
    			}
     
    			else if (userStunPass == 2){
    				turn = 2;
    			}
    		}
     
    		else if (turn == 2){
    			if (enemyStunPass == 1){
    				turn = 2;
    			}
     
    		else if (enemyStunPass == 2){
    				turn = 1;
    			}
    		}
    	}
     
    //GIVES CONTROL TO THE USER OR COMPUTER DEPENDING ON THE CURRENT TURN
    	public static void manageTurn(){	
    		if (turn == 1){
    			System.out.print("It is your turn.");
    			userMove();
    		}
     
    		else if (turn == 2){
    			System.out.print("It is the other Pokemon's turn.");	
    			enemyMove();
    		}
    	}
     
    //IN CASE OF GAME ENDING
    	public static void gameOver(){
    		if (enemyPokemon.size() == 0){
    			System.out.print("Congratulations, you are now the TRAINER SUPREME! You have beat them all!");
    		}
    		if (goodPokemon.size() == 0){
    			System.out.print("Your Pokemon are no longer able to battle. Please try again!");
    		}
    	}
    	//has to reloop to the very beginning if user loses
     
     
    //IN CASE OF POKEMON BEING DEAD	
    	public static void unableToBattle(){
    		for (int i=0; i<goodPokemon.size(); i++){
    			if (goodPokemon.get(i).checkAlive() == false){
    				System.out.printf("%s is no longer able to battle!", goodPokemon.get(i).getName());
    				goodPokemon.remove(goodPokemon.get(i));
    			}
    		}
    	}
     
    //USER CHOOSES WHAT THEY WILL DO ON THEIR TURN
    	public static void userMove(){
    		System.out.print("What would you like to do next? Select option by inputting corresponding number.\n1)Atttack 2)Retreat 3)Pass\n");
    		Scanner kb = new Scanner (System.in);
    		int nextMove = kb.nextInt();
     
    		while (nextMove < 0 || nextMove > 3){
    			System.out.print("That is NOT a valid option. What would you like to do next? (Select option by inputting number.)");
    		}
    		if (nextMove == 1){
    			attack();
    		}
    		else if (nextMove == 2){
    			retreat();
    		}
    		else if (nextMove == 3){
    			pass();
    		}
    	}
     
    //WHAT THE ENEMY POKEMON WILL DO ON THEIR TURN
    	public static int enemyMove(){
    		int enemyAttack = (int)((Math.random())*currentEnemy.getNumAttacks());
     
    		boolean goodMove = false;
    		checkEPResistance();
     
    		//checks that energyLvl is enough for selected move
    		while (goodMove == false){				
    			if (currentEnemy.getEnergyLvl() > currentEnemy.getAttacks(enemyAttack).getEnergyCost()){
    				goodMove = true;
    			}
     
    			else if (currentEnemy.getEnergyLvl() < currentEnemy.getAttacks(enemyAttack).getEnergyCost()){
    				goodMove = false;
    			}
     
    			if (goodMove == true){
    				break;
    			}
    		}
     
    		if (currentEnemy.getAttacks(enemyAttack).getSpecial() == "stun"){
    			int enemyStunPass = (int)(Math.random()*2);
    			if (enemyStunPass == 1){
    				System.out.printf("%s attacks %s with %s!", currentEnemy.getName(), currentPokemon.getName(), currentEnemy.getAttacks(enemyAttack).getAttackName());
    				currentPokemon.getsHurt((int)(currentEnemy.getAttacks(enemyAttack).getDamage()*damageEffect));	
    				System.out.printf("%s has been stunned by %s and cannot move!", currentEnemy.getName(), currentEnemy.getName());
    			}
     
    			else if (enemyStunPass == 2){
    				System.out.printf("%s attacks %s with %s!", currentEnemy.getName(), currentPokemon.getName(), currentEnemy.getAttacks(enemyAttack).getAttackName());
    				currentPokemon.getsHurt((int)(currentEnemy.getAttacks(enemyAttack).getDamage()*damageEffect));
    			}
    		}
     
    		else if (currentEnemy.getAttacks(enemyAttack).getSpecial() == "wild card"){
    			int wildCardPass = (int)(Math.random()*2);
    			if (wildCardPass == 1){
    				System.out.printf("%s attacks %s with %s!", currentEnemy.getName(), currentPokemon.getName(), currentEnemy.getAttacks(enemyAttack).getAttackName());
    				currentPokemon.getsHurt((int)(currentEnemy.getAttacks(enemyAttack).getDamage()*damageEffect));
    			}
     
    			else if (wildCardPass == 2){
    				System.out.printf("%s's Wild Card attack failed!", currentPokemon.getName());
    			}
    		}
     
    		else if (currentEnemy.getAttacks(enemyAttack).getSpecial() == "wild storm"){
    			int wildStormPass = (int)(Math.random()*2);
    			if (wildStormPass == 1){
    				System.out.printf("%s attacks %s with %s!", currentEnemy.getName(), currentPokemon.getName(), currentEnemy.getAttacks(enemyAttack).getAttackName());
     
    				int wildStormNumber = (int)(Math.random());
    				System.out.printf("It's a Wild Storm! Wild Storm %d", wildStormNumber);
    				currentPokemon.getsHurt((int)(currentEnemy.getAttacks(enemyAttack).getDamage()*damageEffect)*wildStormNumber);
    			}
     
    			else if (wildStormPass == 2){
    				System.out.printf("%s's Wild Storm attack failed!", currentPokemon.getName());
    			}
    		}
     
    		else if (currentEnemy.getAttacks(enemyAttack).getSpecial() == "disable"){
    			System.out.printf("%s attacks %s with %s!", currentEnemy.getName(), currentPokemon.getName(), currentEnemy.getAttacks(enemyAttack).getAttackName());
    			System.out.printf("%s has been disabled!", currentPokemon.getName());
    			gpDisabled = true;
    		}
     
    		else if (currentEnemy.getAttacks(enemyAttack).getSpecial() == "recharge"){
    			System.out.printf("%s attacks %s with %s!", currentEnemy.getName(), currentPokemon.getName(), currentEnemy.getAttacks(enemyAttack).getAttackName());
    			currentPokemon.getsHurt((int)(currentEnemy.getAttacks(enemyAttack).getDamage()*damageEffect));
    			System.out.printf("%s recharges his energy by 20!", currentPokemon.getName());
    			currentEnemy.recharge(20);
    		}
     
    	return enemyStunPass;	
    	}
     
    //USER'S ATTACK 
    	public static int attack(){
    		boolean goodMove = false;
    		//checkGPResistance();
     
    		System.out.printf("The attacks of %s are:", currentPokemon.getName()); 
    		for (int i=0; i<currentPokemon.getNumAttacks(); i++){
    			System.out.print(currentPokemon.getAttacks(i).getAttackName());
    		}
     
    		Scanner kb = new Scanner (System.in);
     
    		int chosenAttack = 0;
     
    		//checks that energyLvl is enough for selected move
    		while (goodMove == false){				
    			System.out.println("What attack would you like to do? (Select option by inputting number.)");
    		    chosenAttack = kb.nextInt();
     
    			if (currentPokemon.getEnergyLvl() >currentPokemon.getAttacks(chosenAttack).getEnergyCost()){
    				goodMove = true;
    			}
     
    			else if (currentPokemon.getEnergyLvl() < currentPokemon.getAttacks(chosenAttack).getEnergyCost()){
    				goodMove = false;
    				System.out.print("You do not have enough energy to do this move! Please pick another attack.");
    			}
     
    			if (goodMove == true){
    				break;
    			}
    		}
     
    		if (currentPokemon.getAttacks(chosenAttack).getSpecial() == "stun"){
    			int userStunPass = (int)(Math.random()*2);
    			if (userStunPass == 1){
    				System.out.printf("%s attacks %s with %s!", currentPokemon.getName(), currentEnemy.getName(), currentPokemon.getAttacks(chosenAttack).getAttackName());
    				currentEnemy.getsHurt(currentPokemon.getAttacks(chosenAttack).getDamage());
    				System.out.printf("%s has been stunned!", currentEnemy.getName());
    			}
     
    			else if (userStunPass == 2){
    				System.out.printf("%s attacks %s with %s!", currentPokemon.getName(), currentEnemy.getName(), currentPokemon.getAttacks(chosenAttack).getAttackName());
    				currentEnemy.getsHurt((int)currentPokemon.getAttacks(chosenAttack).getDamage());
    			}
    		}
     
    		else if (currentPokemon.getAttacks(chosenAttack).getSpecial() == "wild card"){
    			int wildCardPass = (int)(Math.random()*2);
    			if (wildCardPass == 1){
    				System.out.printf("%s attacks %s with %s!", currentPokemon.getName(), currentEnemy.getName(), currentPokemon.getAttacks(chosenAttack).getAttackName());
    				currentEnemy.getsHurt((int)currentPokemon.getAttacks(chosenAttack).getDamage());
    			}
     
    			else if (wildCardPass == 2){
    				System.out.printf("%s's Wild Card attack failed!", currentPokemon.getName());
    			}
    		}
     
    		else if (currentPokemon.getAttacks(chosenAttack).getSpecial() == "wild storm"){
    			int wildStormPass = (int)(Math.random()*2);
    			if (wildStormPass == 1){
    				System.out.printf("%s attacks %s with %s!", currentPokemon.getName(), currentEnemy.getName(), currentPokemon.getAttacks(chosenAttack).getAttackName());
     
    				int wildStormNumber = (int)(Math.random());
    				System.out.printf("It's a Wild Storm! Wild Storm %d", wildStormNumber);
    				currentEnemy.getsHurt((int)currentPokemon.getAttacks(chosenAttack).getDamage()*wildStormNumber);
    			}
     
    			else if (wildStormPass == 2){
    				System.out.printf("%s's Wild Storm attack failed!", currentPokemon.getName());
    			}
    		}
     
    		else if (currentPokemon.getAttacks(chosenAttack).getSpecial() == "disable"){			//???
    			System.out.printf("%s attacks %s with %s!", currentPokemon.getName(), currentEnemy.getName(), currentPokemon.getAttacks(chosenAttack).getAttackName());
    			System.out.printf("%s has been disabled!", currentEnemy.getName());
    			epDisabled = true;
    		}
     
    		else if (currentPokemon.getAttacks(chosenAttack).getSpecial() == "recharge"){
    			System.out.printf("%s attacks %s with %s!", currentPokemon.getName(), currentEnemy.getName(), currentPokemon.getAttacks(chosenAttack).getAttackName());
    			currentEnemy.getsHurt((int)currentPokemon.getAttacks(chosenAttack).getDamage());
    			System.out.printf("%s recharges his energy by 20!", currentPokemon.getName());
    			currentPokemon.recharge(20);
    		}
     
    	return userStunPass;	
    	}
     
    //CHECKS IF THE GOOD POKEMON IS RESISTANT OR WEAK AGAINST THE ENEMY POKEMON
     
    	public static double checkGPResistance (){
    		String [] types = {"EARTH", "FIRE", "GRASS", "WATER", "FIGHTING", "ELECTRIC"};
    		damageEffect = 0;
     
    		if (currentPokemon.getType() == currentEnemy.getResistance()){		//checks if good Pokemon's attack damage is halved or not
    			damageEffect = 0.5;
    		}
     
    		else if (currentPokemon.getType() == currentEnemy.getWeakness()){		//checks if good Pokemon's attack damage is doubled or not
    			damageEffect = 2;
    		}
     
    		else{
    			damageEffect = 1;
    		}
     
    	return damageEffect;
    	}
     
    //CHECKS IF THE ENEMY POKEMON IS RESISTANT OR WEAK AGAINST THE USER'S POKEMON	
    	public static double checkEPResistance (){
    		String [] types = {"EARTH", "FIRE", "GRASS", "WATER", "FIGHTING", "ELECTRIC"};
    		damageEffect = 0;
     
    		if (currentEnemy.getType() == currentPokemon.getResistance()){		//checks if enemy Pokemon's attack damage is halved or not
    			damageEffect = 0.5;
    		}
     
    		else if (currentPokemon.getType() == currentEnemy.getWeakness()){		//checks if enemy Pokemon's attack damage is doubled or not
    			damageEffect = 2;
    		}
     
    		else{
    			damageEffect = 1;
    		}
     
    	return damageEffect;
    	}
     
     
     
    //USER'S RETREAT
    	public static void retreat(){
    		System.out.printf("What Pokemon should replace %s", currentPokemon.getName());
    		for (int i=0; i<goodPokemon.size(); i++){
    			if (goodPokemon.get(i).getName() != currentPokemon.getName()){
    				System.out.println(goodPokemon.get(i).getName());
    			}
    		}
     
    		Scanner kb = new Scanner (System.in);
    		int newCurrentPokemon = kb.nextInt();
    		System.out.printf("%s, return! Go, %s", currentPokemon.getName(), goodPokemon.get(newCurrentPokemon).getName());
    		currentPokemon = goodPokemon.get(newCurrentPokemon);
     
    	}
     
    //USER PASSES TURN
    	public static void pass(){
    		System.out.print("You passed your turn.");
    	}


    import java.util.*;
    import java.io.*;
     
    class Pokemon{
     
    	private String name = "";		//fields should be private, method should be public
    	private int HP = 0;
    	private String type = "";
    	private String resistance = "";
    	private String weakness = "";
    	private int numAttacks = 0;
    	private int energyLvl = 50;
     
    	private String attackName = "";
    	private	int energyCost = 0;
    	private	int damage = 0;
    	private	String special = "";
     
    	private static ArrayList <Attack> Attacks = new ArrayList<Attack>();
     
    	public Pokemon (String pokemonStats){
    		String []items = pokemonStats.split(",");
    		name = items[0];
    		HP = Integer.parseInt(items[1]);
    		type = items[2];
    		resistance = items[3];
    		weakness = items[4];
    		numAttacks = Integer.parseInt(items[5]);
     
    		String attackStats = pokemonStats.substring(5,pokemonStats.length());	//numAttacks to end 
     
    		String []items2 = attackStats.split(",");
    		for (int i=0; i<numAttacks; i++){
    			attackName = items[6];
    			energyCost = Integer.parseInt(items[7]);
    			damage = Integer.parseInt(items[8]);
    			special = items[9];
    		}
    	}
     
    	public String getName(){				
    		return name;
    	}
     
     
    	public boolean checkAlive(){
    		boolean isAlive = true;
    		if (HP <= 0){
    			isAlive = false;
    		}
    		else{
    			isAlive =  true;
    		}
     
    	return isAlive;
    	}
     
     
    	public int getHP(){
    		return HP;
    	}
     
    	public void checkHP(){
    		if (HP > 50){
    			HP = 50;
    		}
    	}
     
    	public void heal(int h){
    		HP += h;
    	}
     
    	public void healAll(){
    		HP += 20;
    	}
     
    	public int getEnergyLvl(){
    		return energyLvl;
    	}
     
    	public void recharge(int r){
    		energyLvl += r;
    		if (energyLvl > 50){
    			energyLvl = 50;
    		}
    	}
     
    	public void getsHurt(int dam){
    		HP -= dam;
    	}
     
    	public String getType(){
    		return type;
    	}
     
    	public String getResistance(){
    		return resistance;
    	}
     
    	public String getWeakness(){
    		return weakness;
    	}
     
    	public int getNumAttacks(){
    		return numAttacks;
    	}
     
    	public Attack getAttacks(int ID){
    		return this.Attacks.get(ID);
    	}
     
    }
     
    //FINDS ATTACKS FOR ALL POKEMON
     
     
    class Attack{
     
    	private String attackName = "";
    	private	int energyCost = 0;
    	private	int damage = 0;
    	private	String special = "";
     
    	public Attack(String pokemonStats){//string name,int energycost, int damage, string special
    		this.attackName = attackName;			//'this' access modifier that indicates the current object instance
    		this.energyCost = energyCost;
    		this.damage = damage;
    		this.special = special;
     
    		}
     
    	public String getAttackName(){
    		return attackName;
    	}		
     
    	public int getEnergyCost(){
    		return energyCost;
    	}			
    	public int getDamage(){
    		return damage;
    	}		
    	public String getSpecial(){
    		return special;
    	}
     
    	public void checkDisabled(boolean pDisabled){
    		if (pDisabled = true){
    			damage -= 10;
    		}
    	}


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Exception in thread "main" java.lang.NullPointerException - And everything else...

    Please post the full text of the error messages. The message gives the location of the error where the statement with the null value was executed.
    Look at the statement where the error happened, find the variable with the null value (use printlns to print the values of all the variables on the line) and then backtrack in the code to see why that variable does not have a valid non-null value.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jan 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Exception in thread "main" java.lang.NullPointerException - And everything else...

    Exception in thread "main" java.lang.NullPointerException
    at PokemonArena.attack(PokemonArena.java:337)
    at PokemonArena.userMove(PokemonArena.java:243)
    at PokemonArena.manageTurn(PokemonArena.java:202)
    at PokemonArena.setUpBattle(PokemonArena.java:81)
    at PokemonArena.main(PokemonArena.java:67)

    This occurs after getting into the program and trying to attack. It occurs when I reference currentPokemon, and I'm quite sure that currentPokemon is no longer defined at that point.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Exception in thread "main" java.lang.NullPointerException - And everything else...

    Exception in thread "main" java.lang.NullPointerException
    at PokemonArena.attack(PokemonArena.java:337)
    What variable at line 337 has the null value? Why is it null?

    I'm quite sure that currentPokemon is no longer defined at that point.
    The compiler would give an error message if the variable were not defined.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jan 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Exception in thread "main" java.lang.NullPointerException - And everything else...

    Again, I believe it's currentPokemon, but I'm not sure how to get it to be defined all throughout the program; I declare it at the very top of my code. Any ideas?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Exception in thread "main" java.lang.NullPointerException - And everything else...

    If currentPokemon has a null value, where does the code assign it a value?
    Is it assigned a value BEFORE it is used? Add a println that prints a message where it is assigned a value that will show if the code is executed before where it is used. If no message is printed, then it won't have a value.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Jan 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Exception in thread "main" java.lang.NullPointerException - And everything else...

    I'm sorry, but I don't quite understand. Are you suggesting that I add a println in the actual code?

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Exception in thread "main" java.lang.NullPointerException - And everything else...

    Yes, I am suggesting that you add a println in the actual code. Its a way to debug code and see how it is executing.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Jul 2012
    Posts
    71
    Thanks
    1
    Thanked 7 Times in 7 Posts

    Default Re: Exception in thread "main" java.lang.NullPointerException - And everything else...

    from what i see you're getting a null exception because ur trying to load a set of pokemon info from a text file that hasn't been initialized... instead of a buffer reader try using an input stream to read the file

  10. #10
    Junior Member
    Join Date
    Jan 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Exception in thread "main" java.lang.NullPointerException - And everything else...

    I'm not quite sure how to do that. I also notice that I'm getting a similar error:
    You passed your turn.It is the other Pokemon's turn.Exception in thread "main" java.lang.NullPointerException
    at PokemonArena.enemyMove(PokemonArena.java:264)
    at PokemonArena.manageTurn(PokemonArena.java:212)
    at PokemonArena.setUpBattle(PokemonArena.java:83)
    at PokemonArena.main(PokemonArena.java:67)

    --- Update ---

    This occurs when referencing the currentEnemy, or any information regarding currentEnemy.

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Exception in thread "main" java.lang.NullPointerException - And everything else...

    Exception in thread "main" java.lang.NullPointerException
    at PokemonArena.enemyMove(PokemonArena.java:264)
    Is currentEnemy null at line 264?
    Where is the variable: currentEnemy assigned a value? Is it assigned a value before the code tries to use it?
    Add a println to the code that prints out a message when it is assigned a value so you can see if it is assigned a value BEFORE the code tries to use it.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 5
    Last Post: December 9th, 2012, 02:25 PM
  2. Exception in thread "main" java.lang.NullPointerException
    By Legaly in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 14th, 2012, 11:16 AM
  3. Exception in thread "main" java.lang.NullPointerException problem.......
    By Adam802 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 20th, 2012, 02:23 AM
  4. Replies: 3
    Last Post: December 7th, 2011, 02:03 AM
  5. Exception in thread "main" java.lang.NullPointerException
    By MryJaho in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 4th, 2011, 05:36 PM