Help? NullPointerException

Dungeons and dragons game. Separate classes for the different areas of the game, I pass my Character object to the Farm class from the Town class and try to attack a monster with the Character.attack(Monster) method in the Character class.

I get the runtime error:

java.lang.NullPointerException
at Farm.slimefight(Farm.java:51)
at Farm.enter(Farm.java:25)
at DAndDWorld.main(DAndDWorld.java:101)

Help?

Code:

DAndDWorld Class (main)

import java.util.Scanner;
public class DAndDWorld
{
    public static void main(String args[]){
        int win = 0;
        int hp;
        int maxhp;
        int att;
        int def;
        int block;
        int healability;        
        Scanner scan = new Scanner(System.in);
        Character me = new Character(1, 1, 10, 10, 1, 1);
        Farm farm = new Farm(me);
        
        System.out.println("Welcome to ******'s...");
        System.out.println("  ____                                                           _   ____                                  ");
        System.out.println(" |  _ \\ _   _ _ __   __ _  ___  ___  _ __  ___    __ _ _ __   __| | |  _ \\ _ __ __ _  __ _  ___  _ __  ___ ");
        System.out.println(" | | | | | | | '_ \\ / _` |/ _ \\/ _ \\| '_ \\/ __|  / _` | '_ \\ / _` | | | | | '__/ _` |/ _` |/ _ \\| '_ \\/ __|");
        System.out.println(" | |_| | |_| | | | | (_| |  __/ (_) | | | \\__ \\ | (_| | | | | (_| | | |_| | | | (_| | (_| | (_) | | | \\__ \\");
        System.out.println(" |____/ \\__,_|_| |_|\\__, |\\___|\\___/|_| |_|___/  \\__,_|_| |_|\\__,_| |____/|_|  \\__,_|\\__, |\\___/|_| |_|___/");
        System.out.println("                    |___/                                                            |___/                 ");
        System.out.println("========================================================================================================================");
        System.out.println("========================================================================================================================");
        System.out.println("Warning... This game has no point or plot line!");
        System.out.println("Have fun!");
        
        me.begin();
        System.out.println("You wake up in a small bed in what looks like an inn.");
        System.out.println("A lady walks in.");
        System.out.println("Lady: Hello, I found you yesterday knocked out on the street and decieded to take you in. What's your name?");
        String name = scan.next();
        System.out.println("Lady: ..." + name + "... That's a weird name");
        System.out.println("Lady: Anyway... I really don't care about this room so you can have it if you want. \n Whenever you sleep there, your health should be replenished. You should check out the town.");
        int q = 0;
        while(q == 0){
            System.out.println("What would you like to do?");
            System.out.println("1)Leave");
            System.out.println("2)Hang around a bit");
            int choice = scan.nextInt();
            if(choice == 1){
                q++;
                System.out.println("You thank the lady for her hospitality and leave the building");
            }
            if(choice == 2){
                System.out.println("You sit for a second");
                System.out.println("Lady: Are you going to leave now?");
            }
        }
        System.out.println("You walk outside and see the town.");
        while(win==0){
            System.out.println("Where would you like to go...");
            System.out.println("1)Home");
            System.out.println("2)Store");
            System.out.println("3)Outside of Village");
            System.out.println("4)The Stats Menu");
            System.out.println("5)Exit Game");
            int townchoice = scan.nextInt();
            if(townchoice==5){
                System.out.println("Are you sure you want to stop?");
                System.out.println("1)Yes");
                System.out.println("2)No");
                int endgamechoice = scan.nextInt();
                if(endgamechoice==1){
                    System.out.println("Ok then... Bye.");
                    win++;
                }
                if(endgamechoice==2){
                    
                }
            }
            if(townchoice==4){
                me.displayData();
            }
            if(townchoice==1){
                System.out.println("You enter your room.");
                System.out.println("What would you like to do?");
                System.out.println("1)Sleep");
                System.out.println("2)Exit");
                int roomchoice = scan.nextInt();
                if(roomchoice==1){
                    me.hp = me.maxhp;
                    System.out.println("You have been fully healed!");
                }
            }
            
            if(townchoice==2){
                Shop shop = new Shop();
                shop.enter();
            }
            
            if(townchoice==3){
                System.out.println("You make your way outside the town and see a sign.");
                System.out.println("It reads: Farm to the south; Goblin field to the east; Dragon cave to the north.");
                System.out.println("Where would you like to go?");
                System.out.println("1)To the farm");
                System.out.println("1)To the field");
                System.out.println("1)To the cave");
                int locchoice = scan.nextInt();
                if(locchoice==1){
                    farm.enter();
                }
                if(locchoice==2){
                    
                }
                if(locchoice==3){
                    
                }
            }
        }
    }
}

Character Class:

import java.lang.Math;
import java.util.Scanner;

public class Character {
	String name;
	int hp = 10;
	int maxhp = 10;
	int att = 1;
	int def = 1;
	int block = 1;
	int healability = 1;
	int money = 0;

	public void begin() {

		int clas;
		do {
			Scanner scan = new Scanner(System.in);
			clas = 0;
			System.out.println("Before your adventure begins, what type of warrior do you wish to be?");
			System.out.println("1)Knight");
			System.out.println("2)Ranger");
			System.out.println("3)Mage");

			try {
				clas = scan.nextInt();
			} catch (Exception e) {
				clas = -1;
				System.out.println("Invalid choice, please try again");

			}
		} while (clas == -1);
		if (clas == 1) {
			hp += 3;
			maxhp += 3;
			def += 2;
			att++;
			System.out.println("You have chosen a path of better defense and hp");
		}
		if (clas == 2) {
			hp += 2;
			maxhp += 2;
			def += 2;
			att += 2;
			System.out.println("You have chosen a path of balanced attributes");
		}
		if (clas == 3) {
			def++;
			att += 5;
			System.out.println("You have chosen a path of better attack");
		}
	}

	void printMenu() {
		System.out.println("0)Display Stats");
		System.out.println("1)Attack");
		System.out.println("2)Heal");
		System.out.println("3)Defend");
		System.out.println("4)Runway");
	}

	Character(int money) {
	    this.money = money;
	}
	
	Character() {
	}

	Character(int att, int def, int hp, int maxhp, int block, int healability) {
		this.att = att;
		this.def = def;
		this.hp = hp;
		this.block = block;
		this.healability = healability;
	}

	public void displayData() {
		System.out.println("Attack=" + this.att);
		System.out.println("Defense=" + this.def);
		System.out.println("HP=" + this.hp + "/" + this.maxhp);
		System.out.println("Heal Ability=" + this.healability);
	}

	public void attack(Character me, Monster m1) {
		int damage = (int) (Math.random() * me.att);
		int totalDamage = damage - m1.def;
		m1.hp = m1.hp - totalDamage;
	}

	int heal() {
		System.out.println("You healed yourself for " + healability  + " hitpoint(s).");
		return this.healability;
	}

	int defend() {
		return block;
	}

	boolean runaway() {
		boolean run = false;
		return run;
	}

	void inchp() {
		hp++;
	}

	void incatt() {
		incatt(1);
	}

	void incatt(int count) {
		att += count;
	}

	void incdef() {
		def++;
	}

	void incblock() {
		block++;
	}

	void inchealability() {
		healability++;
	}

	boolean death() {
		boolean dead = false;
		if (hp < 0) {
			dead = true;
		}
		return dead;
	}

	void instantkill(Monster m1) {
		int ikill = 1000000;
		m1.hp = m1.hp - ikill;
		System.out.println("You did " + ikill + " damage");
	}
}

Monster Class:

import java.lang.Math;
public class Monster
{
    String name;
    int hp;
    int att;
    int def;
    
    Monster(int att,int def,int hp){
        this.att = att;
        this.def = def;
        this.hp = hp;
    }
    
        void attack(Character me){
        int damage = (int) (Math.random()*this.att);
        int totalDamage = damage - this.def;
        me.hp = me.hp - totalDamage;
    }
    
    void displayhp(){
        System.out.println(hp);
    }
    
        boolean death(){
        boolean dead = false;
        if(hp<0){
            dead = true;
        }
        return dead;
    }
}

Farm Class:

import java.util.Scanner;
public class Farm
{

	Character me;
	
	public Farm(Character me){
	   me=me;
	   }
	
    void enter(){
       System.out.println("Hello Traveler! I'm glad you showed up my farm is being overrun by vermin, and they are driving me crazy. Could you kill a few for me?");
       Scanner scan = new Scanner(System.in);
       System.out.println("1)Yes");
       System.out.println("2)No");
       int yon = scan.nextInt();
       
       if(yon == 1){
           System.out.println("Great! What can you kill first?");
           System.out.println("Slimes (Easy)");
           System.out.println("Rats (Medium)");
           System.out.println("Small Rabid Furry Things (Hard)");
           int fch = scan.nextInt();
           if(fch==1){
               slimefight();
            }
            
           if(fch==2){
               ratfight();
            }
            
           if(fch==3){
               rabidfight();
            }
       }
    }
    
    public void slimefight(){
        Scanner scann = new Scanner(System.in);
        Monster slime = new Monster(10,1,1);
        System.out.println("A wild slime appeared!");
        int cont = 0;
        while(cont==0){
            System.out.println("What are you going to do?");
            System.out.println("1)Attack");
            System.out.println("2)Heal");
            System.out.println("3)Show stats");
            System.out.println("4)Run Away");
            int fightcommand = scann.nextInt();
            if(fightcommand==1){
                me.attack(me, slime);
            }
            if(fightcommand==2){
                me.hp = me.heal() + me.hp;
            }
            if(fightcommand==3){
                me.displayData();
            }
            if(fightcommand==4){
                System.out.println("You run away like a little baby girl. The farmer laughs at you");
                System.out.println("You go back to the city.");
                cont++;
            }
        }
        
    }
    
    void ratfight(){
        
    }
    
    void rabidfight(){
        
    }
}

Shop Class (unrelated to problem):

import java.util.Scanner;

public class Shop {
    int money = 0;
    Character me = new Character(money);
    Scanner scan = new Scanner(System.in);
    boolean s1b = false;

    void enter() {
        System.out.println("You enter the the shop");
        System.out.println("Shop Keeper: Welcome sir. What kinds of equipment are you looking for today?");
        System.out.println("1)Weapons");
        System.out.println("2)Armor");
        System.out.println("3)Potions");
        System.out.println("4)Exit");
        int type = scan.nextInt();
        if (type == 1) {
            weapons();
        }
        if (type == 2) {
            armor();
        }
        if (type == 3) {
            potions();
        }
        if (type == 4) {
            System.out.println("Ok then. Come back soon!");
        }
    }

    void weapons() {
        int weaponsstore = 0;
            while(weaponsstore == 0){
                System.out.println("What weapon would you like to buy?");
                System.out.println("1)Wooden sword------Free");
                System.out.println("2)Stone Sword-------10");
                System.out.println("3)Steel sword-------100");
                System.out.println("4)Diamond sword-----500");
                System.out.println("5)Darkmatter sword--1000");
                System.out.println("6)Invisable sword---10000");
                System.out.println("7)Never Mind");
                int sword = scan.nextInt();
                if(sword == 1) {
                    if(s1b=false){
                        if(money >= 0){
                            me.incatt();
                            System.out.println("Your attack has increased by 1!");
                            s1b = true;
                            money = money - 0;
                        }
                    }
                
                    if(s1b=true){
                        System.out.println("You already bought this weapon...");
                    }
                }
                if (sword == 2) {
                    me.incatt(3);
                    
                    System.out.println("Your attack has increased by 3!");
                }
                if (sword == 3) {
                    me.incatt(5);
                    
                    System.out.println("Your attack has increased by 5!");
                }
                if (sword == 4) {
                    me.incatt(9);
            
                    System.out.println("Your attack has increased by 9!");
                }
                if (sword == 5) {
                    me.incatt(15);
                    
                    System.out.println("Your attack has increased by 15!");
                }
                if (sword == 6) {
                    me.incatt(30);
                    
                    System.out.println("Your attack has increased by 30!");
                }
                if (sword == 7) {
                    weaponsstore ++;
                    System.out.println("Oh. Ok, anything else I can help you with?");
                    enter();
                }
            }
    }

    void armor() {

    }

    void potions() {

    }
}

thanks.

If you paste just the relevant bits from your program, you might actually uncover where the NPE came from in the first place. :slight_smile:

In your Farm class

import java.util.Scanner;
public class Farm
{

   Character me; // member 
   
   public Farm(Character me){ // parameter
      me=me;
      }

You have the same name ( me ) for your member name and parameter name. To specify that you want to set your member value to the parameter, use the this java keyword, like this:


{

   Character me;
   
   public Farm(Character me){
      this.me=me;
      }

EDIT:

I just read the first line in your error and followed the trails and figures that either the me variable or the slime variable was null - and following the trails I found a possible error.

java.lang.NullPointerException
 at Farm.slimefight(Farm.java:51) /* This line points to ->*/ me.attack(me, slime);
 at Farm.enter(Farm.java:25)
 at DAndDWorld.main(DAndDWorld.java:101)