Results 1 to 2 of 2
  1. #1
    romelako is offline Member
    Join Date
    Dec 2011
    Posts
    1
    Rep Power
    0

    Default Null Pointer Exception: Calling a class from another one

    Hello everyone @ Java Forums. I just had a question about calling a class from another class because I keep getting an error in my code. Here is the outline of my project:

    I need to create 10 CaloveGameTurtle's that has two subclasses (polymorphism): CaloveChaseTurtle & CaloveEvadeTurtle. They are created in their respective corners and try to traverse across the "World" to their safe zone. If they see an enemy turtle within 100 pixels, then they act according to their properties: chase if they're a chaser turtle and evade if they're an evader turtle. If they reach their safe zone, they're considered "not-playing" and play continues until there aren't any more playing turtles. The problem I'm having is in my subclasses. Here's the code:

    Java Code:
    import java.awt.Color;
    import java.util.Random;
    
    public class CaloveEvaderTurtle extends CaloveGameTurtle {
      
      private int evadeAngle = 90;
      private int evadeBaseStep = 400;
      private int evadeOffStep = 8;
      private int alivePlayers, NGAMETURTLE;
      
      
      public CaloveEvaderTurtle(int x, int y, World w, CaloveTagGame ctg, int id) {
        super(x,y,w,ctg,id);
        setColor(java.awt.Color.cyan);
        isChaser = false;
        this.ctg = ctg;
      }
      
      public void move(CaloveGameTurtle cgt) {
        CaloveGameTurtle temp;
        Random rnd = new Random();
        
        temp = ctg.getNearestTurtle(cgt);
        System.out.println("Obtained the nearest turtle.");
        if (cgt.getDistance(temp.getXPos(), temp.getYPos()) <= 100) {
          evadeOffStep = rnd.nextInt(evadeOffStep);
          if (rnd.nextInt(100) < 50) {
            evadeAngle = -(evadeAngle);
            evadeOffStep = -(evadeOffStep);
          }
          cgt.turn(evadeAngle);
         
        } else {
          cgt.turnToFace(0,480);
        }
        System.out.println("Movement complete.");
        cgt.forward(evadeBaseStep + evadeOffStep);
      }
      
    }
    The error is on line 23

    The evader turtle cannot act accordingly without knowing the closest turtle to it, so I have a method in the main class (with a main method) that returns the closest turtle. Here it is:

    Java Code:
    import java.util.Random;
    import java.awt.Color;
    
    public class CaloveTagGame {
      //GLOBAL VARIABLES
      static final int NGAMETURTLE = 10;
      static final int DELAYINTERVAL = 400;
      static CaloveTagGame r;
      CaloveGameTurtle [] player;
      int alivePlayers = 0;
        
      //MAIN METHOD
      public static void main(String args[]) {
        r = new CaloveTagGame();
        System.out.println("CaloveTagGame COMP115 Fall 2011 running.  Initiating game.");
      }
      
      public CaloveTagGame() {
        World w = new World();
        Random rnd = new Random();
        Picture pic = new Picture("bg.png");
        player = new CaloveGameTurtle[NGAMETURTLE];
        
        w.setPicture(pic);
        w.repaint();
    
        for (int i = 0; i < NGAMETURTLE; i++) {
          if (i < NGAMETURTLE/2) {
          player[i] = new CaloveChaserTurtle(rnd.nextInt(160), rnd.nextInt(120)+360, w, r, i); 
          } else {
            player[i] = new CaloveEvaderTurtle(rnd.nextInt(160)+480, rnd.nextInt(120), w, r, i);
          }
        }
        play();    
      }
      
      private void play() {
        Random rnd = new Random();
        
        System.out.println("Commencing tag game!");
        player[6].move(player[6]);
        /*do {
          player[rnd.nextInt(NGAMETURTLE-1)].move();
        } while (alivePlayers != 0);*/
      }
      
      private void delay() {
        try { Thread.sleep(DELAYINTERVAL); }
        catch (InterruptedException ie) { 
        System.out.println("Error in delay() " + ie.toString());}
      }
      
      public CaloveGameTurtle getNearestTurtle(CaloveGameTurtle cgt) {
        double distance, min;
        int id = cgt.getTurtleId();
        int nearestTurtleId = 0;
        min = 100000;
        
        for (int i = 0; i < NGAMETURTLE; i++) {
          if (i != id) {
            if (!(cgt.isTurtleChaser() == player[i].isTurtleChaser())) {
              distance = cgt.getDistance(player[i].getXPos(), player[i].getYPos());
              if (distance < min) {            
                min = distance;
                nearestTurtleId = i;
              }
            }
          }
        }
        
        return player[nearestTurtleId];
      }
      
      public int getNumberPlaying() {
        return alivePlayers;
      }
      
      public int getNumberOfTurtles() {
        return NGAMETURTLE;
      }
    }
    getNearestTurtle starts on line 53.

    What's going on here? I made the CaloveTagGame object "r" static so I can create those turtles with that "r" referenced so I could access the methods of the main class when I need to. Here are the codes for the other classes:

    CaloveGameTurtle:
    Java Code:
    public abstract class CaloveGameTurtle extends Turtle {
      
      protected boolean alive, isChaser;
      protected CaloveTagGame ctg;
      protected int id;
      
      public CaloveGameTurtle(int x, int y, World w, CaloveTagGame ctg, int id) {
        super(x, y, w);
        this.ctg = ctg;
        this.id = id;
        alive = true;
      }
      
      public void isAlive() {
        if (alive) {
          System.out.println("This turtle is alive.");
        } else {
          System.out.println("This turtle is dead.");
        }
      }
      
      public void kill() {
        alive = false;
      }
      
      public void move(CaloveGameTurtle cgt) {
        System.out.println("Error!  Subclass method didn't override.");
      }
      
      public int getTurtleId() {
        return id;
      }
      
      public boolean isTurtleChaser() {
        return isChaser;
      }
    }
    CaloveChaserTurtle: (I haven't started this turtle yet.)
    Java Code:
    import java.awt.Color;
    
    public class CaloveChaserTurtle extends CaloveGameTurtle {
      
      protected int moveNumber;
      
      public CaloveChaserTurtle(int x, int y, World w, CaloveTagGame ctg, int id) {
        super(x,y,w,ctg,id);
        setColor(java.awt.Color.orange);
        isChaser = true;
      }
      
      public void move() {
      }
    }
    Help! Thanks in advanced. :)

    ~ Romelako

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default Re: Null Pointer Exception: Calling a class from another one

    The problem I'm having is in my subclasses
    Can you explain what your problem is?
    What's going on here?
    If you are getting errors, please copy and paste here the full text of the messages.

Similar Threads

  1. null pointer exception
    By Herah in forum New To Java
    Replies: 1
    Last Post: 12-01-2011, 09:44 AM
  2. Null Pointer exception (Again !!)
    By mobosecomin in forum New To Java
    Replies: 6
    Last Post: 03-29-2011, 06:04 PM
  3. Help with Null Pointer Exception
    By Beginner in forum New To Java
    Replies: 2
    Last Post: 04-17-2010, 05:41 PM
  4. null pointer exception
    By cityguy503@yahoo.com in forum New To Java
    Replies: 4
    Last Post: 08-22-2008, 08:22 PM
  5. getting a null pointer exception
    By Rjava in forum XML
    Replies: 4
    Last Post: 07-16-2008, 06:56 AM

Posting Permissions

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