【发布时间】:2014-01-06 01:50:21
【问题描述】:
我有 4 个类(Core、GameMain、Spriter、Character)
- GameMain 扩展核心(核心 >> GameMain)
- 字符扩展 Spriter(Spriter >> 字符)
如果我调用 - b.get...() - 方法而不在 Character 中覆盖它们,我会得到 NullPointerException(它们最初在 Spriter 中,我从 GameMain 通过 Character 调用它们)。
我在 Core 中创建 Character 对象并将其放入 ArrayList 中
public abstract class Core {
ArrayList<Character> mon = new ArrayList<Character>();
void gameloop(){
while(running){
//some code
while(mon.size() < 2){
mon.add(new Character(blk_1,5,1));// Character(Spriter, Long, Long){}
}
draw(g);
}
}
}
然后在 GameMain 中我调用了一个原本在 Spriter 中的方法,但我通过 Character 调用它。
public class GameMain extends Core{
public void draw(Graphics2D g){
for(Character b : mon){ //mon is the ArrayList<Character>
if(test.getCoordY() <= b.getCoordY() - (b.getHeight()/2)){ //<<<< NullPointerException caused by these two Methods , test is a Spriter class
g.drawImage(chChange(), Math.round(test.getX()), Math.round(test.getY()), test.getWidth(), (test.getHeight()), null);
g.drawImage(b.getImage(), Math.round(b.getX()), Math.round(b.getY()), null);
}else {
g.drawImage(b.getImage(), Math.round(b.getX()), Math.round(b.getY()), null);
g.drawImage(chChange(), Math.round(test.getX()), Math.round(test.getY()), null);
}
}
}
}
这是 Spriter 的方法,除非我重写它,否则我会得到错误。
public class Spriter {
Spriter(Image i){
this.i = i;
scenes = new ArrayList();
}
Spriter(){
scenes = new ArrayList();
start();
}
public int getHeight(){
return i.getHeight(null);
}
public float getCoordY(){
float cy;
cy = y + i.getHeight(null); //<<<< NullPointerException happens here, Image i;
return cy;
}
public void setX(float x){
this.x = x;
}
public void setY(float y){
this.y = y;
}
// other similar Methods but no problem with them
//-------------------------------------- Animation Part ----------------------------------------------
public synchronized void addScene(Image i,long t){
total_t+=t;
scenes.add(new oneScene(i, total_t));
}
//start animation
public synchronized void start(){
mov_time = 0;
scn_indx = 0;
}
// get current scene
public synchronized Image getAnimeImage(){
if(scenes.size()==0){
return null;
}else{
return getScene(scn_indx).pic;
}
}
//get the scene
private oneScene getScene(int x){
return (oneScene)scenes.get(x);
}
private class oneScene{
Image pic;
long endTime;
public oneScene(Image pic, long endTime){
this.pic = pic;
this.endTime = endTime;
}
}
}
如果我这样做会起作用:
public class Character extends Spriter{
public Character(Spriter s, long health, long power) {
this.s = s;
this.health = health;
this.power = power;
s.setX(randomY());
s.setY(randomY());
}
public float getCoordY(){
return s.getCoordY();
}
public float getHeight(){
return s.getgetHeight();
}
//some other methods for health and power
}
但是如果我这样做它可以工作吗(它已经给出了错误但如何避免它):
public class Character extends Spriter{
public Character(Spriter s, long health, long power) {
this.s = s;
this.health = health;
this.power = power;
s.setX(randomY()); //setting x for the dynamiclly generated Character Object
s.setY(randomY()); // same for y
}
//some methods for power and health
}
设置 x,y 进行测试(它是一个精灵并且工作得很好)
public class GameMain extends Core{
public void init(){
test.setX(500); test.setY(488);
}
public void draw(Graphics2D g){
//the above code
}
}
如果它们完全相同,我看不出覆盖它们的意义。
【问题讨论】:
-
您是在问如何修复异常,还是在问如何设计您的类以及是否要覆盖?在任何一种情况下,您都需要显示所有相关代码,而不是需要猜测才能填写 deyails 的零碎代码。
-
什么是
i,设置它的代码在哪里? -
@atk 如果我覆盖该错误将不会出现......所以我问为什么我必须覆盖以及是否有导致此问题的错误
-
除非您向我们展示您的代码并更具体地说明 NPE 发生的位置,否则我们无法判断。
-
@Sotirios,他指出了它发生的地方。我认为他在两个类之间有一个重复的字段,他正在设置一个副本并取消引用另一个。但这只是一个猜测——这就是我问这个问题的原因。
标签: java class methods extends dynamically-generated