java - Getters and setters in abstract class? -
i'm working on game in java , have problem using setters , getters abstract class. let's class looks this:
public abstract class objects { protected int velx, vely; public objects(int velx, int vely){ this.velx = velx; this.vely = vely; } //getters, setters , other methods }
now, if want use these methods in class not extend abstract class objects, not work properly. game run throws nullpointerexception when tries execute part of code getters , setters. example:
public class hud { private objects o; public hud(......) public void somemethod(){ if (o.getvelx > 0){ o.setvely(3); } } }
this bad example, if ran throw nullpointerexception. have initialize class objects in different way?
i order use o
, must initialize reference instance of concrete class extends objects abstract class. otherwise o
remains null, , call o.something
throws nullpointerexception
. has nothing setters or getters.
Comments
Post a Comment