Java中的Getter错误

Java中的Getter错误,java,getter,Java,Getter,今年夏天我开始玩java,并在空闲时间设计了一个小游戏。我现在的问题是有一个getter。在播放器类中,我有一个整数的getter“速度” 代码如下: public int getSpeed(){ return this.speed; } 此整数“速度”在构造函数中设置: public Player(int x, int y, String n, BufferedImage s, int spd) { super(x, y); this.name = n; th

今年夏天我开始玩java,并在空闲时间设计了一个小游戏。我现在的问题是有一个getter。在
播放器
类中,我有一个整数的getter“
速度

代码如下:

public int getSpeed(){
    return this.speed;
}
此整数“
速度
”在构造函数中设置:

public Player(int x, int y, String n, BufferedImage s, int spd) {
    super(x, y);
    this.name = n;
    this.sprite = s;
    spd = this.speed;
    this.l_x = x;
    this.l_y = y;
}
当我尝试在运动代码中使用“
speed
”变量时:

if (w) {
    p_y -= player.getSpeed();
}
我在运行时遇到此错误(感谢Martijn Courtaux):

其中,第81行是显示移动代码的行

我将非常感谢任何能给我的帮助,因为我可以通过对每件事使用单独的变量使每件事都“起作用”,但如果我能知道为什么我的getter不起作用,这将是10倍的容易和干净

提前谢谢你

编辑:我变了

spd=该速度

该速度=spd

但是,我仍然得到空指针异常错误。事实上,我试图从中使用的任何变量都会抛出相同的错误

有人能看到任何重大错误吗? 感谢迄今为止帮助过我们的每一个人!我非常感激

主包装

导入java.awt.image.buffereImage

公共类玩家扩展角色{

private String name;
private BufferedImage sprite;
private int speed, l_x, l_y;

public Player(int x, int y, String n, BufferedImage s, int spd) {
    super(x, y);
    this.name = n;
    this.sprite = s;
    speed = spd;
    this.l_x = x;
    this.l_y = y;
}

public int getSpeed(){
    return this.speed;
}

public void setSpeed(int i){
    this.speed = i;
}

public int getOriginalX(){
    return super.o_loc_x;
}

public int getOringinalY(){
    return super.o_loc_y;
}

public int getCurrentY(){
    return this.l_y;
}

public int getCurrentX(){
    return this.l_x;
}

public void setCurrentY(int i){
    i = this.l_y;
}

public void setCurrentX(int i){
    i = this.l_x;
}

public void moveUp(){
    this.l_y -= speed;
}

public void moveDown(){
    this.l_y += speed;
}

public void moveLeft(){
    this.l_x -= speed;
}

public void moveRight(){
    this.l_x += speed;
}

public void setName(String input){
    this.name = input;
}

public String getName(){
    return this.name;
}

public void setSprite(BufferedImage m){
    this.sprite = m;
}

public BufferedImage getSprite(){
    return this.sprite;
}
}

编辑:我真是个白痴。当我声明Player的新实例时,我将

玩家=

而不是:

玩家=

更改:

spd = this.speed;
与:

更改:

spd = this.speed;
与:

更改:

`spd = this.speed;`
致:

更改:

`spd = this.speed;`
致:

这是你的问题:

spd = this.speed;
你想要

this.speed = spd;
在Java(以及大多数编程语言)中,分配给它的变量需要在左边,而不是右边

您所做的是将速度的现有值分配给构造函数中的局部变量。

这是您的问题:

spd = this.speed;
你想要

this.speed = spd;
在Java(以及大多数编程语言)中,分配给它的变量需要在左边,而不是右边


您所做的是将现有的速度值赋给构造函数中的局部变量。

您还可以将参数设置为最终参数。在这种情况下,不能错误地将其他值(或引用)分配给参数。同样,如果你搞砸了,你会得到一个明确的编译器错误

确保未更改的字段(基类型的值和所有其他类型的引用)设置为final,并且在构造过程中忘记设置它们时,会出现明显的编译器错误。如果您只有“final”字段,那么您的类将是不可变的(有关使用不可变类的原因,请参阅Joshua Bloch的有效Java)。但是,运行应用程序时,速度可能会发生变化,因此这对这个特定字段不起作用。我将演示它的名称

这就是:

private final String name;
private int speed;

public Player(final String n, final int spd) {
    spd = this.speed; // cannot assign, it's final
    // whoops, another error, forgot to assign a value to the name field
}

你也可以让你的论点成为最终的。在这种情况下,不能错误地将其他值(或引用)分配给参数。同样,如果你搞砸了,你会得到一个明确的编译器错误

确保未更改的字段(基类型的值和所有其他类型的引用)设置为final,并且在构造过程中忘记设置它们时,会出现明显的编译器错误。如果您只有“final”字段,那么您的类将是不可变的(有关使用不可变类的原因,请参阅Joshua Bloch的有效Java)。但是,运行应用程序时,速度可能会发生变化,因此这对这个特定字段不起作用。我将演示它的名称

这就是:

private final String name;
private int speed;

public Player(final String n, final int spd) {
    spd = this.speed; // cannot assign, it's final
    // whoops, another error, forgot to assign a value to the name field
}

NullPointerException在您的gameMain类中,因此我们需要查看它以了解问题所在


正如您现在在问题中指出的,问题是由您对player变量的赋值引起的。您创建了一个新的局部变量并分配给该变量,而不是分配给该实例变量,因此,将来对该实例变量的访问将导致NullPointerException。

NullPointerException位于gameMain类中,因此我们需要查看它以了解问题所在



正如您现在在问题中指出的,问题是由您对player变量的赋值引起的。您创建了一个新的局部变量并分配给该变量,而不是分配给实例变量,因此将来对实例变量的访问将导致NullPointerException。

请注意,您可以使用Eclipse中的“Clean Code”等命令,自动将最终修改器分配给尚未收到第二个值的变量,如果您厌倦了一直键入“final”。请注意,您可以在Eclipse中使用例如“Clean Code”来自动为尚未收到第二个值的变量指定final修饰符,以防您厌倦了一直键入“final”。编译器没有给出该错误。它是在运行时抛出的。我更改了问题以进行反思。感谢所有帮助过我的人!我建议你不要接受一个事实上不是答案的答案。如果你自己找到了答案,就把它贴出来,然后接受它。我不能回答我自己的问题6个小时,因为我的声誉很低。编译器没有给出那个错误。它是在运行时抛出的。我更改了问题以进行反思。感谢所有帮助过我的人!我建议你不要接受一个事实上不是答案的答案。如果你自己找到了答案,就把它贴出来,然后接受它。我不能回答我自己的问题6个小时,因为我的声誉很低。我改变了代码,但我仍然得到错误(与我所有的getter)。IDE是Eclipse如果这有帮助的话。speed和spd是int,这怎么会是NullPointerException的源呢?是的,如果你从来没有给int赋值,它最初会是0I。我更改了代码,但仍然会出错(使用我所有的getter)。如果有帮助的话,IDE就是Eclipse