Discussions
Categories
- 196.7K All Categories
- 2.2K Data
- 235 Big Data Appliance
- 1.9K Data Science
- 449.8K Databases
- 221.6K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 31 Multilingual Engine
- 549 MySQL Community Space
- 477 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3K ORDS, SODA & JSON in the Database
- 532 SQLcl
- 4K SQL Developer Data Modeler
- 186.9K SQL & PL/SQL
- 21.3K SQL Developer
- 295.4K Development
- 17 Developer Projects
- 138 Programming Languages
- 292.1K Development Tools
- 104 DevOps
- 3.1K QA/Testing
- 645.9K Java
- 28 Java Learning Subscription
- 37K Database Connectivity
- 153 Java Community Process
- 105 Java 25
- 22.1K Java APIs
- 138.1K Java Development Tools
- 165.3K Java EE (Java Enterprise Edition)
- 17 Java Essentials
- 158 Java 8 Questions
- 85.9K Java Programming
- 79 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.2K Java SE
- 13.8K Java Security
- 203 Java User Groups
- 24 JavaScript - Nashorn
- Programs
- 395 LiveLabs
- 37 Workshops
- 10.2K Software
- 6.7K Berkeley DB Family
- 3.5K JHeadstart
- 5.6K Other Languages
- 2.3K Chinese
- 170 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 230 Portuguese
GetGraphics() in Java Application

Hey Guys,
I am trying to better myself in Java. So i basically porgrammed the snake game as an applet and transfered it to an application just to understand the differences between applets and applications. I manage now to do the change from applet to application but now i have a nullpointerexception at the line where I call getGraphics();
So you can see and understand better what i mean i have copied my come code here.
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
public class SnakeGame extends Frame implements WindowListener, Runnable, KeyListener {
/**
*
*/
private static final long serialVersionUID = 1L;
Graphics gfx;
Image img;
Thread thread;
Snake snake;
Token token;
boolean gameOver;
public static void main(String[] args){
SnakeGame sg = new SnakeGame();
sg.setSize(400, 400);
sg.setVisible(true);
sg.setLayout(new FlowLayout());
sg.setResizable(false);
}
public SnakeGame() {
this.resize(400, 400);
gameOver = false;
img = createImage(400, 400);
gfx = img.getGraphics();
this.addKeyListener(this);
this.addWindowListener(this);
snake = new Snake();
token = new Token(snake);
thread = new Thread(this);
thread.start();
}
public void paint(Graphics g){
gfx.setColor(Color.black);
gfx.fillRect(0, 0, 400, 400);
if(!gameOver){
snake.draw(gfx);
token.draw(gfx);
gfx.drawString("Score " + token.getScore(), 2, 12);
}
else{
gfx.setColor(Color.red);
gfx.drawString("Game Over", 180, 150);
gfx.drawString("Score: " + token.getScore(), 180, 170);
}
g.drawImage(img, 0, 0, null);
}
public void update(Graphics g) {
paint(g);
}
public void repaint(Graphics g) {
paint(g);
}
public void run() {
for (;;) {
if (!gameOver) {
snake.move();
this.checkGameOver();
token.snakeCollision();
}
this.repaint();
try {
Thread.sleep(40);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void checkGameOver() {
if (snake.getX() < 0 || snake.getX() > 396) {
gameOver = true;
}
if (snake.getY() < 0 || snake.getY() > 396) {
gameOver = true;
}
if (snake.snakeCollision()) {
gameOver = true;
}
}
public void keyTyped(KeyEvent e) {
}
public void keyPressed(KeyEvent e) {
if (!snake.isMoving()) {
if (e.getKeyCode() == KeyEvent.VK_UP || e.getKeyCode() == KeyEvent.VK_RIGHT
|| e.getKeyCode() == KeyEvent.VK_DOWN) {
snake.setIsMoving(true);
}
}
if (e.getKeyCode() == KeyEvent.VK_UP) {
if (snake.getYDir() != 1) {
snake.setYDir(-1);
snake.setXDir(0);
}
}
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
if (snake.getYDir() != -1) {
snake.setYDir(1);
snake.setXDir(0);
}
}
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
if (snake.getXDir() != 1) {
snake.setXDir(-1);
snake.setYDir(0);
}
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
if (snake.getXDir() != -1) {
snake.setXDir(1);
snake.setYDir(0);
}
}
}
public void keyReleased(KeyEvent e) {
}
public void windowOpened(WindowEvent e) {
// TODO Auto-generated method stub
}
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
public void windowClosed(WindowEvent e) {
// TODO Auto-generated method stub
}
public void windowIconified(WindowEvent e) {
// TODO Auto-generated method stub
}
public void windowDeiconified(WindowEvent e) {
// TODO Auto-generated method stub
}
public void windowActivated(WindowEvent e) {
// TODO Auto-generated method stub
}
public void windowDeactivated(WindowEvent e) {
// TODO Auto-generated method stub
}
}
I hope you guys can help me and tell me where my error is.
Thanx in Advance,
Ricci
Answers
-
Welcome to the forums!
I am trying to better myself in Java.
Good - that is going to be a lifelong process.
The place to start learning Java is by taking a tutorial or finding a good book.
The Java Tutorials has trails on ALL of the basic Java functionality including graphics.
https://docs.oracle.com/javase/tutorial/2d/index.html
So i basically porgrammed the snake game as an applet and transfered it to an application just to understand the differences between applets and applications. I manage now to do the change from applet to application but now i have a nullpointerexception at the line where I call getGraphics();
Well - thanks for posting the code but if you look at what you posted and you will see that you did NOT POST ANYTHING that shows an exception occurred or post the actual stacktrace.
When you post you need to SHOW US, not just tell us:
1. WHAT you do
2. HOW you do it
3. WHAT results you get
The stacktrace will show you where the exception occurred and other useful info
Did you look at the lines where you said the exception occurred?
img = createImage(400, 400);gfx = img.getGraphics();
If 'img.getGraphics' throws the NPE then 'img' is likely NULL. So look at the line above where it says 'createImage'. That means you need to RTFM to see what 'createImage' does:
https://docs.oracle.com/javase/7/docs/api/java/awt/Component.html#createImage(int,%20int)
- Returns:
- an off-screen drawable image, which can be used for double buffering. The return value may be
null
if the component is not displayable. This will always happen ifGraphicsEnvironment.isHeadless()
returnstrue
.
- See where it says 'return value may be null'? See where your exception says you are getting 'null'.
- Put the two together and what do you have?
- Use an IDE such as NetBeans when you test. Step through the code ONE LINE AT A TIME so you can see what every line of code does and the results you get.
- What do you see after you execute that 'img = createImage(400,400)?