Results 1 to 2 of 2
- 12-09-2011, 06:46 PM #1
Member
- Join Date
- Dec 2011
- Posts
- 1
- Rep Power
- 0
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:
The error is on line 23Java 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 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:
getNearestTurtle starts on line 53.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; } }
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:
CaloveChaserTurtle: (I haven't started this turtle yet.)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; } }
Help! Thanks in advanced. :)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() { } }
~ Romelako
- 12-09-2011, 07:01 PM #2
Similar Threads
-
null pointer exception
By Herah in forum New To JavaReplies: 1Last Post: 12-01-2011, 08:44 AM -
Null Pointer exception (Again !!)
By mobosecomin in forum New To JavaReplies: 6Last Post: 03-29-2011, 05:04 PM -
Help with Null Pointer Exception
By Beginner in forum New To JavaReplies: 2Last Post: 04-17-2010, 04:41 PM -
null pointer exception
By cityguy503@yahoo.com in forum New To JavaReplies: 4Last Post: 08-22-2008, 07:22 PM -
getting a null pointer exception
By Rjava in forum XMLReplies: 4Last Post: 07-16-2008, 05:56 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks