Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (538)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (600)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
   Home   Help   Search   Login   Register   
  Show Posts
Pages: [1]
1  Game Development / Newbie & Debugging Questions / Help? NullPointerException on: 2012-04-17 19: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.
2  Game Development / Newbie & Debugging Questions / Re: Looking for where to Start with 2D games in Java. Help? on: 2012-02-23 01:09:28
Alright, thanks for everything. Hopefully I'll have Pong up and running in no time! (no time=hours of debugging and practice)
3  Game Development / Newbie & Debugging Questions / Re: Looking for where to Start with 2D games in Java. Help? on: 2012-02-22 00:07:19
Ok, that sounds great. I read some other "Help me, I'm New" posts, and I was thinking about starting with Slick2D because it sounded better than Java2D. I think I'll try to create Pong to start with.

2 questions

1. How is OpenGL related to Slick2D?

2. Do you know any tutorials that I could go to in order to start learning Slick2D?
4  Game Development / Newbie & Debugging Questions / Looking for where to Start with 2D games in Java. Help? on: 2012-02-21 23:47:31
Hello, I'm new to this forum and I have looked around and it looks very helpful. I'm also rather new to java. Currently, I have taken an intro to Java course and learned basic Java syntax. I have (pretty much) mastered text based stuff, and would I'm interested in learning 2D graphics programming with Java. I just recently downloaded Eclipse and am playing with it as well. (Previously used BlueJ)

I was inspired by the game recently programmed by Mojang at the HumbleBundle.com Mojam, and I would like to learn to program games that are similar in looks. It's called Catacomb Snatch, and you should be able to find screenshots on google. The graphics seem like they wouldn't be too hard to code, but they released the source code, and after opening it with eclipse, it's just WAY over my head. The biggest project I worked with using just text was a "Dungeons & Dragons" type text co-op adventure with 4 or 5 rooms (each a separate class), two playable characters (each a separate class), and 4 or 5 monsters (all from one class). This project ended up with only around 10 classes, plus a main class which housed the main method.

What I'm looking for I guess, is just where to start to go from a 10 class text game to a 20 class graphical 2D Shoot 'em up using lib, res, and src folders and files. That gap is where I'm stuck currently, and I've looked for a way to get across and haven't found it.

P.S. In the one programming class I took, we made a PacMan game using Greenfoot towards the end of the year. It was terrible. Greenfoot took 2-5 minutes to load one world with 40 or so objects in it. I learned enough to accomplish a Shooter game in Greenfoot, but it would be slow, bulky and not nearly advanced as I would like. I'm looking to transition to more advanced programming, not simply improvise with the noob skills I have currently.

If you made it this far, thanks for reading my post and thanks in advance for the help! Grin
Pages: [1]
 

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

The first screenshot will be displayed as a thumbnail.

rwatson462 (29 views)
2014-12-15 09:26:44

Mr.CodeIt (20 views)
2014-12-14 19:50:38

BurntPizza (40 views)
2014-12-09 22:41:13

BurntPizza (75 views)
2014-12-08 04:46:31

JscottyBieshaar (37 views)
2014-12-05 12:39:02

SHC (50 views)
2014-12-03 16:27:13

CopyableCougar4 (47 views)
2014-11-29 21:32:03

toopeicgaming1999 (113 views)
2014-11-26 15:22:04

toopeicgaming1999 (100 views)
2014-11-26 15:20:36

toopeicgaming1999 (30 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
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!