Closed. This question does not meet Stack Overflow guidelines。它当前不接受答案。
想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
6年前关闭。
我有4个班级(Core,GameMain,Spriter,Character)
-GameMain扩展了核心(核心>> GameMain)
-角色扩展了Spriter(Spriter >>角色)
如果我调用-b.get ...()-没有在Character中覆盖它们的方法,则会得到NullPointerException(它们最初位于Spriter中,我是从GameMain通过Character调用它们的)。
我在Core中制作了Character对象,并将其放在ArrayList中
然后在GameMain内部,我调用了最初在Spriter内部的方法,但我通过Character调用了它。
这是Spriter方法,除非我重写它,否则会得到错误。
如果我这样做它会工作:
但是如果我这样做它可以工作(它已经给了错误但是如何避免):
将x,y设置为测试(它是sprite,效果很好)
如果它们完全相同,我看不到覆盖它们的意义。
@ElliottFrisch-我很抱歉,如果您要这样回答。我不是要重复;我只是不确定那是否是您的意思。无论如何,这绝对是问题所在。
想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
6年前关闭。
我有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设置为测试(它是sprite,效果很好)
public class GameMain extends Core{
public void init(){
test.setX(500); test.setY(488);
}
public void draw(Graphics2D g){
//the above code
}
}
如果它们完全相同,我看不到覆盖它们的意义。
最佳答案
您的问题是,创建Character
时,您正在调用Spriter
的no-arg构造函数,而不是带有Image
参数的构造函数。这意味着您的形象永不动摇;这就是为什么您有空指针异常的原因。
您可能应该在Image
的构造函数参数中添加Character
,并将其传递给超类构造函数,如下所示。
public Character(Spriter s, long health, long power, Image img) {
super(img);
// and so on.
@ElliottFrisch-我很抱歉,如果您要这样回答。我不是要重复;我只是不确定那是否是您的意思。无论如何,这绝对是问题所在。
10-07 21:49