Java 中的 Getter 错误

发布于 2024-12-22 12:59:54 字数 2503 浏览 0 评论 0原文

我今年夏天开始学java,并在空闲时间设计一个小游戏。我现在遇到的问题是吸气剂。在 Player 类中,我有一个整数“speed”的 getter。

这是代码:

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

这个整数“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 Courteaux):

Exception in thread "Thread-3" java.lang.NullPointerException
    at main.gameMain.update(gameMain.java:81)

其中第 81 行是移动代码出现的行。

我将非常感谢我能得到的任何帮助,因为我可以通过对所有内容使用单独的变量来使所有内容“工作”,但如果我知道为什么我的 getter 不工作,那将会变得更加容易和干净。

先感谢您!

编辑:我改变了

spd = this.speed;

this.speed = spd;

但是,我仍然收到空指针异常错误。事实上,我尝试使用的任何变量都会引发相同的错误。

任何人都可以看到任何重大错误吗? 感谢迄今为止提供帮助的所有人!我非常感激!

包主;

导入java.awt.image.BufferedImage;

public class Player extends Character {

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 实例时,我把

玩家玩家=...

而不是:

玩家 = ....

I started java this summer and am designing a small game in my free time. The problem I have right now is with a getter. In a Player class I have a getter for an integer "speed".

Here is the code:

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

This integer "speed" is set in a constructor:

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;
}

When I try to use the "speed" variable in a movement code:

if (w) {
    p_y -= player.getSpeed();
}

I get this error at runtime (thank you Martijn Courteaux):

Exception in thread "Thread-3" java.lang.NullPointerException
    at main.gameMain.update(gameMain.java:81)

Where line 81 is the line that the movement code appears on.

I would greatly appreciate any help that I could get as I could make everything "work" by using individual variables for everything, but it would be 10x easier and cleaner if I could know why my getter's aren't working.

Thank you in advance!

EDIT: I changed

spd = this.speed;

to

this.speed = spd;

but, I am still getting the Null Pointer Exception Error. In fact any variable that I am trying to use from the throws the same error.

Can anyone see any major errors?
And thanks to everyone that has helped so far! I greatly appreciate it!

package main;

import java.awt.image.BufferedImage;

public class Player extends Character {

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;
}

}

EDIT: I am such an idiot. WHen I was declaring a new instance of Player, I put

Player player =...

Instead of:

player = ....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

茶花眉 2024-12-29 12:59:54

更改:

spd = this.speed;

与:

this.speed = spd;

Change:

spd = this.speed;

With:

this.speed = spd;
忘东忘西忘不掉你 2024-12-29 12:59:54

NullPointerException 位于您的 gameMain 类中,因此我们需要查看它以了解问题可能是什么。

正如您现在在问题中指出的那样,问题是由您对玩家变量的分配引起的。您没有分配给实例变量,而是创建了一个新的局部变量并分配给它,因此将来访问该实例变量会导致 NullPointerException。

The NullPointerException is in your gameMain class, so we need to see that to see what the problem might be.

As you have now pointed out in your question the problem was caused by your assignment to the player variable. Instead of assigning to the instance variable you have created a new local variable and assigned to that, thus future access to the instance variable results in a NullPointerException.

旧时光的容颜 2024-12-29 12:59:54

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

确保将不更改的字段(基本类型的值和所有其他类型的引用)设置为 Final,并且当您在构造过程中忘记设置它们时,您将得到一个明显的编译器错误。如果您只有“final”字段,您的类将是不可变的(有关使用不可变类的原因,请参阅 Joshua Bloch 的《Effective 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
}

You can also make your arguments final. In that case, you cannot assign other values (or, again, references) to the arguments by mistake. Again, you will get a clear compiler error if you mess up.

Make sure that your the fields that do not change (the values for base types and the references for all others) are set to final, and you will get a clear compiler error when you forget to set them during construction. If you have only "final" fields, your class will be immutable (see Effective Java by Joshua Bloch for reasons to use immutable classes). But the speed probably changes when you run the application, so that will not work for this particular field. I'll demo it for the name.

So that would be:

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
}
内心旳酸楚 2024-12-29 12:59:54

这是你的问题:

spd = this.speed;

你希望

this.speed = spd;

在Java(以及大多数编程语言)中被分配的变量需要位于左侧而不是右侧。

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

Here is your problem:

spd = this.speed;

You want

this.speed = spd;

In Java (and most programming languages for that matter) the variable being assigned to needs to be on the left hand side not the right.

What you were doing is assigning the existing value of speed to the local variable in the constructor.

鹤舞 2024-12-29 12:59:54

更改:

`spd = this.speed;`

至:

`speed = spd;`

change:

`spd = this.speed;`

to:

`speed = spd;`
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文