Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6963447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:47:34+00:00 2026-05-27T15:47:34+00:00

I started java this summer and am designing a small game in my free

  • 0

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 = ….

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T15:47:34+00:00Added an answer on May 27, 2026 at 3:47 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just started with java development using Eclipse, when I was debugging this
Since I've started programming in Java I have been wondering this (about a year
I am extremely new to Java (just started this weekend) and have a background
I have just started learning java, and know only a small amount of code,
My Java application is started from within a native program through java.dll. This native
I've just started using Java's enums in my own projects (I have to use
greetings, today i have started to learn java using netbeans ide. i would like
I have recently started seeing user agents like Java/1.6.0_14 (and variations) on my site
First of all, this is my code (just started learning java): Queue<String> qe =
I've downloaded the JAVA Eclipse IDE and started learning Java. I have a good

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.