Results 1 to 6 of 6
  1. #1
    Sinnergy's Avatar
    Sinnergy is offline Member
    Join Date
    Feb 2009
    Location
    Philippines
    Posts
    4
    Rep Power
    0

    Unhappy I can't fix this runtime error in my program

    im still new at java and starting to create my programs for our project in "Computer Science 102"

    the program is like a text based game and each time i run the program it runs safe without any errors and after running some lines the program terminated due to this error

    Java Code:
    Exception in thread "main" java.lang.NullPointerException
            at ratcorb.Start.fight(Start.java:131)
            at ratcorb.Start.enemy(Start.java:208)
            at ratcorb.Start.main(Start.java:250)
    Java Result: 1
    im also new to the forums and i dont know how to show u guys where the error points, thats why i added comments to my program to let u know where the error points at

    please dont mind the program structure, im really a beginner in java and havent learned any "programming techniques in java", it would be great if someone could lend some of his/her techniques in java programming ;) that can greatly optimize my program structure

    the program is like an RPG text based game, there are some words i used in our language, but mostly english

    heres the program with the errors pointed out using comments
    Java Code:
    package ratcorb;
    import java.util.*;
    public class Start{
        static String net; //For input purposes only
        static Integer weapon, life, dmg, def, exp, expMax, expGain, tDmg, tDef, level;
        static Integer eLife, eDmg, eDef, eExp, etDmg, etDef;
        static Integer tempDmg, tempDef;
    
    //****************************************************************************
    //**************************START OF SYSTEMS************************************
    //****************************************************************************
    
        public static void showMsg(String msg, int r){
            /*A shortcut for printing messages
             0 for println; 1 for print*/
            if(r==0){
                System.out.println(msg);
            }else{
                System.out.print(msg);
            }
        }
    
        public static int getRandomNumber(int max){
            //Generates a random number from 1 to 'max'
    		Random ran = new Random();
    		return 1 + ran.nextInt(max);
    	}
    
    	public static int roll(){
            //Rolls the dice for the player and prints out the result
            int out = getRandomNumber(6);
    		//System.out.println("You rolled the dice and got " +out);
            return out;
    	}
    
        public static void postScript(){
            //A template for skipping the message
            Scanner input = new Scanner(System.in);
            showMsg("Press enter.", 1);
            net = input.nextLine(); net = "";
        }
    
        public static void replenish(){
            switch(roll()){
                case 1: showMsg("You found nothing on the decaying corpse.", 0); postScript(); break;
                case 2: {
                    showMsg("You devoured the edible parts of the corpse restoring some life", 0);
                    life = life + (roll() + roll());
                    showMsg("You restored " + (life + (roll() + roll())) + " hit points", 0);
                    break;
                }
                case 3: {
                    showMsg("You found 1 pc. Chicken Joy and ate it.", 0);
                    life = life + (roll() + roll());
                    showMsg("You restored " + (life + (roll() + roll())) + " hit points", 0);
                    break;
                }
                case 4: {
                    showMsg("You ate the flesh of the decaying corpse.", 0);
                    life = life + (roll() + roll());
                    showMsg("You restored " + (life + (roll() + roll())) + " hit points", 0);
                    break;
                }
                case 5: {
                    showMsg("You found Macaroni Salad and ate it, restoring some life.", 0);
                    life = life + (roll() + roll());
                    showMsg("You restored " + (life + (roll() + roll())) + " hit points", 0);
                    break;
                }
                case 6: showMsg("You examined the corpse and found nothing", 0); postScript(); break;
            }
        }
    
        public static void levelUp(){
            life = 110;
            dmg += 2;
            def += 1;
            expMax += 100;
            showMsg("You leveled up! you are now level:" + (level += 1), 0);
            postScript();
        }
        
        public static void fight(int r){
            int out = 0;
            Scanner input = new Scanner(System.in);
            switch(r){
                case 1: {
                    eLife = 25;
                    eDmg = 5;
                    eDef = 0;
                    eExp = 50;
                    showMsg("You attack first(kay lab man ka ni lord).", 0);
                    while(out != 1){
                        tempDmg = 0;
                        if(eLife < 1){
                            replenish();
                            exp += eExp;
                            showMsg("Gained " +eExp +" experience", 0);
                            if(exp > expMax){
                                levelUp();
                            }
                            eLife = 0;
                            eDmg = 0;
                            eDef = 0;
                            eExp = 0;
                            break;
                        }else{
                            showMsg("Press enter to roll the dice", 0);
                            net = input.nextLine(); net = "";
                            tDmg = dmg + roll();
                            etDef = eDef + roll();
                            if(tDmg - etDef < 1){
                                tempDmg = 0;
                            }else{
                                tempDmg = tDmg - etDef;
                            }
                            showMsg("You deal " + tempDmg +" damage " +" on Infected", 0);
                            eLife -= tempDmg;
                            showMsg("Infected's remaining life is " + eLife + " na lang", 0);
                            tempDmg = 0;
                            postScript();
                            showMsg("Infected will now attack you", 0); postScript();
                            etDmg = eDmg + roll();
                            tDef = def + roll();
                            if(etDmg - tDef < 1){
                                tempDmg = 0;
                            }else{
                                tempDmg = etDmg - tDef;
                            }
                            showMsg("Infected deals " + tempDmg +" damage " + " on you", 0);
    [COLOR="Red"][B]                        life -= tempDmg; //1.POINTING HERE[/B][/COLOR]
                            showMsg("Your remaining life is " + life + " na lang", 0);
                            tempDmg = 0;
                            if(life < 1){
                                showMsg("You have died",0);
                                showMsg("Game OVER!!", 0);
                                showMsg("Thanks for playing the game.",0);
                                showMsg("......by: Rol John Torralba......", 0);
                                System.exit(0);
                            }
                        }
                        
                    }break;
                }
                case 2: {
                    eLife = 50;
                    eDmg = 10;
                    eDef = 3;
                    eExp = 150;
                    showMsg("You attack first(kay lab man ka ni lord).", 0);
                    while(out != 1){
                        tempDmg = 0;
                        if(eLife < 1){
                            replenish();
                            exp += eExp;
                            showMsg("Gained " +eExp +" experience", 0);
                            if(exp > expMax){
                                levelUp();
                            }
                            eLife = 0;
                            eDmg = 0;
                            eDef = 0;
                            eExp = 0;
                            break;
                        }else{
                            showMsg("Press enter to roll the dice", 0);
                            net = input.nextLine(); net = "";
                            tDmg = dmg + roll();
                            etDef = eDef + roll();
                            if(tDmg - etDef < 1){
                                tempDmg = 0;
                            }else{
                                tempDmg = tDmg - etDef;
                            }
                            showMsg("You deal " + tempDmg +" damage " + " on Infected", 0);
                            eLife -= tempDmg;
                            showMsg("Rugy Boy's remaining life is " + eLife + " na lang", 0);
                            tempDmg = 0;
                            postScript();
                            showMsg("Rugby Boy will now attack you", 0); postScript();
                            etDmg = eDmg + roll();
                            tDef = def + roll();
                            if(etDmg - tDef < 1){
                                tempDmg = 0;
                            }else{
                                tempDmg = etDmg - tDef;
                            }
                            showMsg("Rugby Boy deals " + tempDmg +" damage " + " on you", 0);
                            life -= tempDmg;
                            showMsg("Your remaining life is " + life + " na lang", 0);
                            tempDmg = 0;
                            if(life < 1){
                                showMsg("You have died",0);
                                showMsg("Game OVER!!", 0);
                                showMsg("Thanks for playing the game.",0);
                                showMsg("......by: Rol John Torralba......", 0);
                                System.exit(0);
                            }
                        }
    
                    }break;
                }
            }
        }
    
        public static void enemy(int r){
            switch(r){
    [COLOR="Red"][B]            case 1: fight(1); break; //infected 2.POINTING HERE[/B][/COLOR]
                case 2: fight(2); break; //rugby boy
            }
        }
    
    //****************************************************************************
    //**************************END OF SYSTEMS************************************
    //****************************************************************************
    
        public static void main(String[] args){
            int weaponType;
            int wCounter = 0;
            Scanner input = new Scanner(System.in);
            showMsg("Welcome to Rol John and the Curse of the Rugby Boys.",0);
            showMsg("A text based RPG game made by Rol John Torralba.",0);
            showMsg("A project to be submitted in CS102 with Instructor Mr. Priguil O. Fuertes.",0);
            showMsg("Enjoy playing the game and good luck!",0);
            showMsg("PLEASE INPUT THE RIGHT DATA to avoid errors", 0);
            showMsg("Loading Modules, even though there are no modules", 0);
            showMsg("...", 0); postScript();
            showMsg("Monday, 6:30 am, December 6, 2069", 0);
            showMsg("6 years after the spread of Rugby on the Land...", 0); postScript();
            showMsg("Another cold day under the sun, where a group consisits of 5 members", 0);
            showMsg("wanders the land, wandering the heavy mist of the plains of Butuan", 0);
            showMsg("the forbidden City.", 0); postScript();
            showMsg("A group led by Rol John(controlled by you), and followed by Maia, Del, Mons and Lemz,",0);
            showMsg("a group named 'Floo Fighters', the only group that was destined",0);
            showMsg("to protect the uninfected exiles from Butuan.", 0); postScript();
            while(wCounter == 0){
                showMsg("Pick your weapon",0);
                showMsg("1. Dako nga Hatsa(Axe): Damage 10, Defense 0",0);
                showMsg("2. Sundang w/ Taklob sa Kaldero: Damage 7, Defense 3",0);
                showMsg("3. Gamay nga Bolo w/ Kalha: Damage 5, Defense 5",0);
                showMsg("Input the weapon number: ", 1);
                weaponType = input.nextInt();
                switch(weaponType){
                    case 1: dmg = 10; def = 0; wCounter = 1; weapon = 1; break;
                    case 2: dmg = 7; def = 3; wCounter = 1; weapon = 2; break;
                    case 3: dmg = 5 ; def = 5; wCounter = 1; weapon = 3; break;
                    default: showMsg("Pick the right number", 0); break;
                }
            }
    [COLOR="Red"][B]        enemy(1); //3. POINTING HERE[/B][/COLOR]
            exp = 0;
            expMax = 100;
            level = 1;
        }
    
    }
    and the complete output
    Java Code:
    run:
    Welcome to Rol John and the Curse of the Rugby Boys.
    A text based RPG game made by Rol John Torralba.
    A project to be submitted in CS102 with Instructor Mr. Priguil O. Fuertes.
    Enjoy playing the game and good luck!
    PLEASE INPUT THE RIGHT DATA to avoid errors
    Loading Modules, even though there are no modules
    ...
    Press enter.
    Monday, 6:30 am, December 6, 2069
    6 years after the spread of Rugby on the Land...
    Press enter.
    Another cold day under the sun, where a group consisits of 5 members
    wanders the land, wandering the heavy mist of the plains of Butuan
    the forbidden City.
    Press enter.
    A group led by Rol John(controlled by you), and followed by Maia, Del, Mons and Lemz,
    a group named 'Floo Fighters', the only group that was destined
    to protect the uninfected exiles from Butuan.
    Press enter.
    Pick your weapon
    1. Dako nga Hatsa(Axe): Damage 10, Defense 0
    2. Sundang w/ Taklob sa Kaldero: Damage 7, Defense 3
    3. Gamay nga Bolo w/ Kalha: Damage 5, Defense 5
    Input the weapon number: 1
    You attack first(kay lab man ka ni lord).
    Press enter to roll the dice
    
    You deal 10 damage  on Infected
    Infected's remaining life is 15 na lang
    Press enter.
    Infected will now attack you
    Press enter.
    Infected deals 6 damage  on you
    Exception in thread "main" java.lang.NullPointerException
            at ratcorb.Start.fight(Start.java:131)
            at ratcorb.Start.enemy(Start.java:208)
            at ratcorb.Start.main(Start.java:250)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 1 minute 4 seconds)
    any help is greatly appreciated
    thank you in advance
    Last edited by Sinnergy; 02-03-2009 at 10:22 AM.

  2. #2
    Join Date
    Aug 2008
    Location
    Saint-Petersburg, Russia
    Posts
    66
    Rep Power
    0

    Default

    The problem is that "life" variable is not initialized at the moment when you decrement it, nothing was assigned to it before. You should review your algorithm and find the bug.

    As to structure...well, it is "spaghetti" out of user input/output and calculations. This is main flaw I see. In short - you must separate user interface and "business logic"(calculations). Try to read something about MVC, say
    Model-view controller - Wikipedia, the free encyclopedia
    Java BluePrints - J2EE Patterns
    Last edited by ProjectKaiser; 02-03-2009 at 01:20 PM.

  3. #3
    Sinnergy's Avatar
    Sinnergy is offline Member
    Join Date
    Feb 2009
    Location
    Philippines
    Posts
    4
    Rep Power
    0

    Default

    thx ProjectKaiser, ill look into my program and fix the error :D
    and thanks for the tutorials

  4. #4
    beezerbutt is offline Member
    Join Date
    Feb 2009
    Location
    South Africa
    Posts
    18
    Rep Power
    0

    Default

    Hi Sinn

    I also would like to comment more specifically about the Kaiser's comments regarding Spaghetti based on a very quick scan of your code that i just did.

    Newbies to Java love static variables, methods etc.

    If you want to write better code, you should try and understand OO concepts such as encapsulation, cohesion etc.

    Static vars and methods have their own raison d'etre; understand these reasons and apply them where necessaryand become a better programmer :=)

    Best of luck.

  5. #5
    Francis123 is offline Member
    Join Date
    Feb 2009
    Posts
    1
    Rep Power
    0

    Default

    Hi Sinnergy..I would also try to comment about it...Full of Escabetche ug spaghetti ang imung makuha dha...

  6. #6
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    usually, people don't understand java well until they design their own projects and have that kind of freedom to do what they want. students often get skeleton code or portions of code that they won't understand because they don't/won't know what it means, because they're left to just concentrating on the specific goal of the project.

Similar Threads

  1. Replies: 3
    Last Post: 01-23-2009, 08:53 AM
  2. Replies: 1
    Last Post: 11-14-2008, 06:56 AM
  3. Weblogic 5.0 runtime error with EJB 1.0
    By somesh in forum Enterprise JavaBeans (EJB)
    Replies: 0
    Last Post: 08-25-2008, 03:09 PM
  4. Runtime error : BOOTSTRAP server
    By vishnuvardhan.h in forum New To Java
    Replies: 0
    Last Post: 08-12-2008, 10:59 AM
  5. Which statement is throwing a runtime error....
    By money123 in forum New To Java
    Replies: 1
    Last Post: 07-30-2007, 12:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •