bthug211 0 Newbie Poster

Please help me ... a NullPointerException appears
here's the codes

import java.util.Random;

public class Dice {

    Random r =new Random();
    int sides;

 //constructor
    public Dice(int s){
       sides = s;
    }
 //roll method depending on sides on top
    public int roll(){

    return r.nextInt(sides-1)+1;

    }
}
// end of class



public class Maze {
char [][] maze;
    public Maze(int s){
        maze=new char[s][s];
        for (int x=0; x<s; x++)
            for (int y=0; y<s; y++)
                maze[x][y]=' ';
   }
   }
//end of class

public class karaKter {


//Dice needed for initial karakter position in maze
    Dice d = new Dice(10);
    String name;
    String race;
    String raceDescription;

    // if the Character picked object
    Object [] dala=new Object[2];

    // decreases per attack. if 0, you lose baby.
    int life;
    // used for normal attack. to be subtracted from opponent's life
    int damage;
    //used to determine if how far will the Character move
    int speed;
    //initial positions
    int xpos;
    int ypos;

    //KaraKter symbol in maze
    char symbol;

    //variable to determine if KaraKter is dead
    boolean isDead;

    String [] raceChoice = {"Homo Sapien", "Homo Alien", "Super Goo" , "Enkantado" };
    String [] raceDescriptionChoice= { "Tao. with normal human strength. Superpower: Suntok sa buwan (+100 to damage, +100 to life)",
                                       "Alien from another planet. Human-like strength. Superpower: Alien-nation (destroys any target within 9 squares, -50 to life)",
                                       "Gooey-like alien from another planet. Slow creature yet powerful. Superpower: Super Ga-Goo (attack for +100 damage to each target within 9 squares) ",
                                       "Enkanto from planet Earth. calm and fierceful. Superpower: MagikStik (+50 to damage, +200 life)"};
    int []lifechoice={100,100,100,100};
    int []damagechoice={4,4,4,4};
    int []speedchoice={1,2,3,4};

    public karaKter(String n,int r){
        name=n;
        race=raceChoice[r];
        raceDescription=raceDescriptionChoice[r];
        life=lifechoice[r];
        damage=damagechoice[r];
        speed=speedchoice[r];
        symbol = name.charAt(0);
        xpos =d.roll();
        ypos =d.roll();
        isDead = true;
    }

    //moves depending on the dice roll on Main Game
    public void move(int x, int y){

    }

    //method to attack and inflict damage, affected by weapon which Character holds
    public int  attack(){
        return d.roll() + 10;
    }
    //method to use superpower
    public void superAttack(){

    }

    //method to get wounded and decrease life, affected by armor which Character holds
    public int  getWounded(){
    life=life-attack();
    return life;
    }
}
//end of class

public class Item {

    String type;
    int cost;
    int strength;
    boolean isDestroyed = false;
    char symbol;
     int xpos;
     int ypos;
     int roll;   

}
//end of class

public class Armor extends Item {

    int defensePoint;

    String [] typechoice = {"chainmail","iorica Segmentata", "plate Armor"};
    int [] strengthChoice = {30, 20, 25};
    int [] costChoice = {8, 6, 7};
    int [] defenseChoice = {20, 30, 25};

    public Armor(int i){

        type = typechoice[i];
        strength = strengthChoice[i];
        defensePoint = defenseChoice[i];
        cost = costChoice[i];
        symbol = type.charAt(0);

    }


    public int defend(){

        return defensePoint;

    }

    public void getAttacked(int damage){

        strength = strength - damage;
        if (strength<=0) isDestroyed = true;

    }


}
//end of class

public class Weapon extends Item{

Dice d=new Dice(10);
    int attackPoint;

    String [] typechoice = {"kris","sword", "bolo"};
    int [] strengthChoice = {30, 20, 25};
    int [] costChoice = {8, 6, 7};
    int [] defenseChoice = {20, 30, 25};

    public Weapon(int i){

        type = typechoice[i];
        strength = strengthChoice[i];
        attackPoint = defenseChoice[i];
        cost = costChoice[i];
        symbol = type.charAt(0);
         xpos=d.roll();
         ypos=d.roll();
    }
    public int attack(){

        return attackPoint;

    }

    public void getAttacked(int damage){

        strength = strength - damage;
        if (strength<=0) isDestroyed = true;

    }



}
//end of class

import javax.swing.JOptionPane;

public class MainGameV1 {

   static karaKter [] kararray = new karaKter[6];
   static int karakterCount = 0;
   static int armorCount = 0;
   static Dice d = new Dice(3);
   static Maze m = new  Maze(10);
   static Dice initpos = new Dice (10);
   static Dice movex = new Dice (10);
   static Dice movey = new Dice (10);
   static Armor [] armorarray = new Armor[3];
   static Weapon [] weparray = new Weapon[3];
   static Weapon w1 = new Weapon(d.roll()-1);
   static Armor a1 = new Armor(d.roll()-1);

   public static void main (String []args){

        boolean addMore = true;
        int pTurn = 0;
        int itemturn = 0;

   while(addMore){

       newkarakter();
       String c = JOptionPane.showInputDialog("add Another?(y/n)");

        if(c.equalsIgnoreCase("n"))
            addMore=false;

        if( m.maze[w1.xpos][w1.ypos]==' ' && m.maze[a1.xpos][a1.ypos]==' ') {

            w1.xpos=movex.roll();
            w1.ypos=movey.roll();
            a1.xpos=movex.roll();
            a1.ypos=movey.roll();          
       }
            m.maze[w1.xpos][w1.ypos]=w1.symbol;
            m.maze[a1.xpos][a1.ypos]=a1.symbol;
            printMap();
}
//for putting weapons and armors in the map
   while(true){

        JOptionPane.showMessageDialog(null," Player " + (pTurn+1)  + "\n" + kararray[pTurn].name + "'s turn");
        printMap();

        for(itemturn=0;itemturn < 3;itemturn++){

                    armorarray[itemturn].xpos = movex.roll();
                    armorarray[itemturn].ypos = movey.roll();
                    m.maze[armorarray[itemturn].xpos][armorarray[itemturn].ypos] = armorarray[itemturn].symbol;
                    m.maze[weparray[itemturn].xpos][weparray[itemturn].ypos] = weparray[itemturn].symbol;
                    printMap();
    //Condition if Player land on an Armor
            if( m.maze[armorarray[itemturn].xpos][armorarray[itemturn].ypos] != ' ' ){

                    char t = m.maze[armorarray[itemturn].xpos][armorarray[itemturn].ypos];

                        for(int s=0;s<=armorCount;s++){

                            char f=m.maze[armorarray[s].xpos][armorarray[s].ypos];

           if(f == t && s != itemturn ){

                    char Armor = armorarray[s].symbol;
                    printMap();
                    JOptionPane.showMessageDialog(null,kararray[pTurn].name  + " picked up an " + Armor );

                    break;
            }
       }
   }
         //Condition if Player land on a Weapon
            if( m.maze[weparray[itemturn].xpos][weparray[itemturn].ypos] != ' ' ){

                    char t = m.maze[weparray[itemturn].xpos][weparray[itemturn].ypos];

                        for(int s=0;s<=armorCount;s++){

                            char f=m.maze[weparray[s].xpos][weparray[s].ypos];

           if(f == t && s != itemturn ){

                    char Weapon = weparray[s].symbol;
                    printMap();
                    JOptionPane.showMessageDialog(null,kararray[pTurn].name  + " picked up a " + Weapon );

                    break;
            }
       }
   }    
        }
            //Condition if 2 Players have the same location
            if( m.maze[kararray[pTurn].xpos][kararray[pTurn].ypos] != ' ' ){

                    char t = m.maze[kararray[pTurn].xpos][kararray[pTurn].ypos];

                        for(int s=0;s<=karakterCount;s++){

                            char f=m.maze[kararray[s].xpos][kararray[s].ypos];

           if(f == t && s != pTurn ){

                    String Player = kararray[s].name;
                    printMap();
                    JOptionPane.showMessageDialog(null,kararray[pTurn].name  + " will Battle " + Player + "\n Let THe Fight Begin" );
                    doBattle(kararray[pTurn].name,Player);
                    break;
            }
       }
   }

                    m.maze[kararray[pTurn].xpos][kararray[pTurn].ypos]=kararray[pTurn].symbol;
                    pTurn++;
                    printMap();

                    if(pTurn==karakterCount)
                    pTurn = 0;
}
   }


     //prints map and position of characters
    public static void printMap(){

        System.out.println("---------------------");

           for(int x=0; x<10; x++){
           for(int y=0;y<10;y++)
                System.out.print(m.maze[x][y]+"|");
                System.out.println();
                System.out.println("---------------------");
        }
        }


    //creates a new character
    public static void newkarakter(){

        if (karakterCount>5) JOptionPane.showMessageDialog(null, "Karakter capacity exceeded");
            else {
                kararray[karakterCount] = new karaKter(JOptionPane.showInputDialog("Enter your name, challenger: "),Integer.parseInt(JOptionPane.showInputDialog("Enter your race: \n 1. Homo Sapien\n 2. Homo Alien\n 3. Super Goo\n 4. Enkantado"))-1);

                // checks kung may laman yung initial postion  ni character sa maze
            while(m.maze[kararray[karakterCount].xpos][kararray[karakterCount].ypos]!=' '){

                kararray[karakterCount].xpos=initpos.roll();
                kararray[karakterCount].ypos=initpos.roll();
            }

                 m.maze[kararray[karakterCount].xpos][kararray[karakterCount].ypos]=kararray[karakterCount].symbol;
        }
                karakterCount++;
    }

    public static void doBattle(String P1,String P2){

        karaKter Player1=new karaKter(P1,2);
        karaKter Player2=new karaKter(P2,3);       
        Dice Attack=new Dice(10);
        int p=Attack.roll();
        int p2=Attack.roll();
        int life;
        int Attack1=Player1.attack() + p;
        int Attack2=Player2.attack() + p2;
            JOptionPane.showMessageDialog(null,"=====================================In One Corner========================================= "   +    " \nName : " + P1 + "\nRace : "+ Player1.race + "\nRace Description:"+ Player1.raceDescription +  "\nLife :"  + Player1.life  + "\n==================================In the Other Corner====================================== " + "\nName : " + P2 + "\nRace : "+ Player2.race + "\nRace Description:"+ Player2.raceDescription  + "\nLife :"  + Player2.life  );

            while(Player1.isDead && Player2.isDead){

                int life1=Player1.getWounded();
                int life2=Player2.getWounded();


            if(life1<=0||life2<=0){
            if(Player1.life !=0 )

                JOptionPane.showMessageDialog(null,P1+ " Wins");
            else{

                JOptionPane.showMessageDialog(null,P2 + " Wins");
                JOptionPane.showMessageDialog(null,"========================================== Lets Return To THe Map ==========================================");
                break;
            }
                JOptionPane.showMessageDialog(null,Player1 + " Attacks " + Player2 + "\nDamage : " + Player1.attack()  +  "\n" +  Player2 + "'s life is now " + Player2.getWounded() + "\n============================================\n " + Player2 + " Attacks " + Player1 + "\nDamage : " + Player2.attack() +  "\n" + Player1 + "'s life is now " + Player1.getWounded() );
        }   //
    }
}
}
//end of class

//please help me..why when I run the MainGameV1 class, a NullPointerException appears?please solve this error..tnx
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.