Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Applets  RSS feed

 
Valerie String
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For this applet i have 50 sailors that go for shore leave on the islands of booze to whickey. so obviously they all get drunk, the point is to randomly generate numbers and depending on what they are it move each sailor a certain distance, some get to the ship, some fall off the plank and some stay in whickey drinking, depending on the randomly generated number. so then when a certain amount of sailors reach the boat (eg 10) the boat can leave. The thing about my code is that when i try and make the boat move across the applet it causes a null pointer exception. Help!!!



public class Boat {
int boatXvalues, boatYvalues;

public Boat(){
int boatXvalues = 150;
int boatYvalues = 210;
}

public int getBoatX(){
return boatXvalues;
}

public int getBoatY(){
return boatYvalues;
}

public void moveBoat(int gotToShip){
if (gotToShip == 10){
boatXvalues = boatXvalues - 20;
}
}

}


Sailor whereHeAt;
Boat posistion;
private void runThis(){

Graphics g = this.getGraphics();
//counter to do it 50 times
for(int sailor = 0; sailor < 50; sailor++){
//repaints over the scence so that the scence can be clearly read, although this is not
//working perfectly, sometimes my counts get blurred.
repaint();
//show the count
g.drawString("Sailor No: " + (sailor + 1) + "/50", 0, 440);

//gets the location of the sailor(where he at)
whereHeAt = new Sailor();
g.setColor(Color.BLACK);
g.fillRect(whereHeAt.getX() , whereHeAt.getY(), 5, 5);

//move the sailor on by the certain condition,depending on the random number generated,
//and if he is out od bounds or not
while(!whereHeAt.outOfBounds()){
whereHeAt.moveSailor();
g.setColor(Color.BLACK);
g.fillRect(whereHeAt.getX(), whereHeAt.getY(), 5,5);

//slows the applet down slightly so can see it better
try{
Thread.sleep(30);
}catch (InterruptedException ex){
}

g.setColor(Color.black);
g.fillRect(whereHeAt.getX(), whereHeAt.getY(), 3, 3);

}

if (sailor == 49){
posistion.moveBoat(gotToShip);
}
}
}
 
Shyam Prasad Murarka
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Valerie,
I noticed that the object of Boat class has only been declared and not initialized.


Also your gotToShip variable does not appear to have been declared or iniialized. Well, its seems that your complete code has not been posted. So chances are there that you might have initialized them elsewhere.
 
Valerie String
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeh i figured that my X -values we not there so then i declared them using arrays but i still have the same prob. anyway i think i will leave the program the way it is, part from my repaint method(well the one supplied by java) is not fully working. is there anything that could be up with it that you know of
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!