0
Hello! I’m trying to make the player walk but always when I press a button appears huge error I’m trying to make the player walk but this error appears
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at com.Cauã.main.Game.keyPressed(Game.java:136)
at java.desktop/java.awt.Component.processKeyEvent(Component.java:6594)
at java.desktop/java.awt.Component.processEvent(Component.java:6413)
at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:5012)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4844)
at java.desktop/java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1950)
at java.desktop/java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:870)
at java.desktop/java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:1139)
at java.desktop/java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:1009)
at java.desktop/java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:835)
at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:4893)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4844)
at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:772)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:95)
at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:745)
at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:743)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:742)
at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at com.Cauã.main.Game.keyReleased(Game.java:155)
at java.desktop/java.awt.Component.processKeyEvent(Component.java:6597)
at java.desktop/java.awt.Component.processEvent(Component.java:6413)
at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:5012)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4844)
at java.desktop/java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1950)
at java.desktop/java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:870)
at java.desktop/java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:1139)
at java.desktop/java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:1009)
at java.desktop/java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:835)
at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:4893)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4844)
at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:772)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:95)
at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:745)
at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:743)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:742)
at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)'
my code is:
<package com.Cauã.main;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import com.Cauã.Entity.Entity;
import com.Cauã.Entity.Player;
import com.Cauã.graficos.Spritesheet;
public class Game extends Canvas implements Runnable,KeyListener{
private static final long serialVersionUID = 1L;
public static JFrame frame;
private Thread thread;
private boolean isRunning = true;
private final int WIDTH = 240;
private final int HEIGHT = 160;
private final int SCALE = 3;
private BufferedImage image;
public List<Entity> entities;
public Spritesheet spritesheet;
public Player player;
public Game() {
addKeyListener(this);
setPreferredSize(new Dimension(WIDTH*SCALE,HEIGHT*SCALE));
initFlame();
image = new BufferedImage(WIDTH,HEIGHT,BufferedImage.TYPE_INT_ARGB);
entities = new ArrayList<Entity>();
spritesheet = new Spritesheet("/spritesheet.png");
Player player = new Player(0,0,19,21,spritesheet.getSprite(39, 0, 19, 21));
entities.add(player);
}
public void initFlame() {
frame = new JFrame();
frame.add(this);
frame.setResizable(false);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);}
public synchronized void start() {
thread = new Thread (this);
isRunning = true;
thread.start();
}
public synchronized void stop () {
}
public static void main(String[] args) {
Game game = new Game();
game.start();
}
public void tick() {
for(int i = 0; i < entities.size(); i++) {
Entity e = entities.get(i);
e.tick();
}
}
public void render() {
BufferStrategy bs = this.getBufferStrategy();
if(bs == null) {
this.createBufferStrategy(3);
return;
}
Graphics g = image.getGraphics();
g.setColor(new Color(0,255,0));
g.fillRect(0, 0, WIDTH, HEIGHT);
for(int i = 0; i < entities.size(); i++) {
Entity e = entities.get(i);
e.render(g);
}
g.dispose();
g = bs.getDrawGraphics();
g.drawImage(image, 0, 0,WIDTH*SCALE,HEIGHT*SCALE,null );
bs.show();
}
public void run() {
long lastTime = System.nanoTime();
double amontOfTicks = 60.0;
double ns = 1000000000 / amontOfTicks;
double delta = 0;
int frames = 0;
double timer = System.currentTimeMillis();
while(isRunning) {
long now = System.nanoTime();
delta+= (now - lastTime) / ns;
lastTime = now;
if(delta >= 1) {
tick();
render();
frames++;
delta--;
}
if(System.currentTimeMillis() - timer >= 1000) {
System.out.println("FPS: " + frames );
frames = 0;
timer+=1000;
}
}
}
public void keyTyped(KeyEvent e) {
}
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_RIGHT ||
e.getKeyCode() == KeyEvent.VK_D) {
System.out.println(player);
player.right = true;
}
else if(e.getKeyCode() == KeyEvent.VK_LEFT ||
e.getKeyCode() == KeyEvent.VK_A) {
player.left = true;
}
if(e.getKeyCode() == KeyEvent.VK_UP ||
e.getKeyCode() == KeyEvent.VK_W) {
player.up = true;
}
else if(e.getKeyCode() == KeyEvent.VK_DOWN ||
e.getKeyCode() == KeyEvent.VK_S) {
player.down = true;
}
}
public void keyReleased(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_RIGHT ||
e.getKeyCode() == KeyEvent.VK_D) {
player.right = false;
}
else if(e.getKeyCode() == KeyEvent.VK_LEFT ||
e.getKeyCode() == KeyEvent.VK_A) {
player.left = false;
}
if(e.getKeyCode() == KeyEvent.VK_UP ||
e.getKeyCode() == KeyEvent.VK_W) {
player.up = false;
}
else if(e.getKeyCode() == KeyEvent.VK_DOWN ||
e.getKeyCode() == KeyEvent.VK_S) {
player.down = false;
}
}
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
}}
>
<
package com.Cauã.Entity;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
public class Entity {
protected double x;
protected double y;
protected int width;
protected int height;
private BufferedImage sprite;
public Entity(int x, int y, int width, int height,BufferedImage sprite) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.sprite = sprite;
}
public int getX() {
return (int)this.x;
}
public int getY() {
return (int)this.y;
}
public double getWidth() {
return this.width;
}
public double getHeight() {
return this.height;
}
public void tick() {
}public void render(Graphics g) {
g.drawImage(sprite, this.getX(), this.getY(), null);
}
}
>
<package com.Cauã.Entity;
import java.awt.image.BufferedImage;
public class Player extends Entity{
public boolean right,up,left,down;
public double speed = 1.4;
public Player(int x, int y, int width, int height, BufferedImage sprite) {
super(x, y, width, height, sprite);
}
public void tick() {
if(right)
x+=speed;
else if(left)
x-=speed;
if(up)
y+=speed;
else if(down)
y-=speed;
}
}
>
<package com.Cauã.graficos;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
public class Spritesheet {
private BufferedImage spritesheet;
public Spritesheet(String path) {
try {
spritesheet = ImageIO.read(getClass().getResource(path));
} catch (IOException e) {
e.printStackTrace();
}
}
public BufferedImage getSprite(int x, int y, int width, int height) {
return spritesheet.getSubimage( x, y, width, height);
}
}
>