Java-Gaming.org    
Featured games (78)
games approved by the League of Dukes
Games in Showcase (429)
Games in Android Showcase (89)
games submitted by our members
Games in WIP (468)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Help? NullPointerException  (Read 745 times)
0 Members and 1 Guest are viewing this topic.
Offline torched_platypi

Junior Newbie





« Posted 2012-04-17 21:20:44 »

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)
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
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:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
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:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
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:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
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):
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
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.
Offline sproingie

JGO Kernel


Medals: 200



« Reply #1 - Posted 2012-04-17 21:26:37 »



If you paste just the relevant bits from your program, you might actually uncover where the NPE came from in the first place. Smiley
Offline jonjava
« Reply #2 - Posted 2012-04-17 21:27:53 »

In your Farm class

1  
2  
3  
4  
5  
6  
7  
8  
9  
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:
1  
2  
3  
4  
5  
6  
7  
{

   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.

1  
2  
3  
4  
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)

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

theagentd (6 views)
2014-04-24 23:00:44

xsi3rr4x (83 views)
2014-04-15 18:08:23

BurntPizza (75 views)
2014-04-15 03:46:01

UprightPath (86 views)
2014-04-14 17:39:50

UprightPath (69 views)
2014-04-14 17:35:47

Porlus (86 views)
2014-04-14 15:48:38

tom_mai78101 (109 views)
2014-04-10 04:04:31

BurntPizza (169 views)
2014-04-08 23:06:04

tom_mai78101 (265 views)
2014-04-05 13:34:39

trollwarrior1 (216 views)
2014-04-04 12:06:45
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!