2016-05-21 11 views
-1

Ich programmiere ein einfaches Maus- und Katzenspiel. Aber ich habe immer einen Debug-Fehler, wenn ich versuche, setLocation zu verwenden.Java-Fehler bei setLocation (x, y)

Können Sie mir sagen, wie kann ich es beheben? Ich glaube nicht, dass etwas falsch ist.

import java.awt.Point; 
import java.util.Random; 

public abstract class Animal { 

Point location_; 
String name_; 
Random rng_; 

public Animal() { 
} 

public Animal(String name, Random rng){ 
    name_=name; 
    rng_=rng; 
} 


abstract void move(); 

public String getName(){ 
    return name_; 
} 

public Point getLocation(){ 
    return location_; 
} 

public void setStartLocation(){ 
    int x = rng_.nextInt(4); 
    int y = rng_.nextInt(4); 


// error 
    location_.setLocation(x, y); 
} 

} 

eine andere Datei:

import java.util.Random; 

public class Cat extends Animal { 



public Cat(String name, Random rng){ 
    super(name,rng);  
} 


void move() { 
    int i = rng_.nextInt(3); 
    int x = (int) location_.getX(); 
    int y = (int) location_.getY(); 

    if(i==0 && y<4){ 

     int newY = y++; 

     location_.move(x,newY); 
    } 

    else if (i==1 && y>0){ 

     int newY = y--; 

     location_.move(x,newY); 
    } 

    else if (i==2 && x>0){ 

     int newX = x--; 

     location_.move(newX,y); 
    } 

    else if (i==3 && x<4){ 

     int newX = x++; 

     location_.move(newX,y); 
    } 

    else move(); 

} 



} 

eine andere Datei:

import java.util.Random; 

public class Mouse extends Animal { 

String mobility; 

public Mouse(String name, Random rng){ 
    super(name,rng);  
} 


void move() { 
    int i = rng_.nextInt(3); 
    int x = (int) location_.getX(); 
    int y = (int) location_.getY(); 

    if(i==0){ 

     int newY = y++; 

     location_.move(x,newY); 
    } 

    else if (i==1){ 

     int newY = y--; 

     location_.move(x,newY); 
    } 

    else if (i==2){ 

     int newX = x--; 

     location_.move(newX,y); 
    } 

    else if (i==3){ 

     int newX = x++; 

     location_.move(newX,y); 
    } 



} 

public String checkMoblity(){ 
    int x = (int) location_.getX(); 
    int y = (int) location_.getY(); 

    if(x==-1 || x==5){ 
     if(y==0 || y==2 || y==4){ 
      return "drowned"; 
     } 

     else return "escaped"; 
    } 

    else if(y==-1 || y==5){ 
     if(x==0 || x==2 || x==4){ 
      return "drowned"; 
     } 

     else return "escaped"; 
    } 

    else return null; 

} 

} 

eine andere Datei:

import java.util.Random; 

public class Chase { 

public void playGame(){ 

    Random rand = new Random(); 

//error 
    Cat cat = new Cat("Tom", rand); 
    Mouse mouse = new Mouse("Jerry", rand); 

    do{ 
     cat.setStartLocation(); 
     mouse.setStartLocation(); 
    }while(cat.getLocation()== mouse.getLocation()); 

    String status = Status(cat,mouse); 
    boolean end = end(cat, mouse); 

    do{ 
     mouse.move(); 

     if(end==false){ 
     cat.move(); 
     } 

    }while(end==false); 

    if (status == "drowned"){ 
     System.out.println(mouse.getName() + " drowned!"); 
    } 

    else if (status == "escaped"){ 
     System.out.println(mouse.getName() + " escaped!"); 
    } 

    else if (status == "snack"){ 
     System.out.println(cat.getName() + " got the snack!"); 
    } 


} 



public String Status(Cat cat, Mouse mouse){ 
    if(mouse.checkMoblity()== "drowned"){ 
     return "drowned"; 
    } 

    else if(mouse.checkMoblity() == "escaped"){ 
     return "escaped"; 
    } 

    else if(mouse.getLocation()==cat.getLocation()){ 
     return "snack"; 
    } 

    else return null; 
} 

public boolean end(Cat cat, Mouse mouse){ 
    if(mouse.checkMoblity()== "drowned"){ 
     return true; 
    } 

    else if(mouse.checkMoblity() == "escaped"){ 
     return true; 
    } 

    else if(mouse.getLocation()==cat.getLocation()){ 
     return true; 
    } 

    else return false; 
} 



public static void main(String[] args){ 
    Chase chase = new Chase(); 

//error 
    chase.playGame(); 
} 
} 
+0

Ich wette, Sie erhalten Nullzeiger Ausnahme? – SMA

+0

was bedeutet das? – Allen

Antwort

1

Sie sollten instantiate an object, bevor es mit:

public void setStartLocation(){ 
    int x = rng_.nextInt(4); 
    int y = rng_.nextInt(4); 

    location_ = new Point(x, y); 
} 

Die Setter-Methode setLocation(int, int) nicht verwendet werden kann, wenn Ihr Objekt Point location_ nicht instanziiert wird.

+0

Ich habe das versucht. Es klappt. Wenn ich jedoch versuche, mein Projekt zu debuggen oder auszuführen, passiert nichts. Ich könnte ein neues Problem bekommen. – Allen

+0

Was ist dein neues Problem? Könntest du mehr erklären? –

+0

Ich habe meinen Code als das, was Sie gesagt haben, korrigiert. Ich benutze Eclipse, um meinen Code zu schreiben. Ich klickte auf Als Java-Projekt ausführen, nichts passierte. – Allen

Verwandte Themen