• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Eckhart

NullPointerException

10 posts in this topic

Well, I've been trying to learn some Java for a while, but I'm having some trouble with the following code:
[CODE]
package net.devox.basic;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;



/*
* Title: Game
* Version: 1.0.0
* Desc: This class is used to control and pack together the
* methods required to run the games logic in a central
* location.
*/



public class Game extends Canvas
{
//buffer for off screen drawing
private BufferedImage buffer;

private static final long serialVersionUID = 1L;

private void init()
{
//turn paint off
this.setIgnoreRepaint(true);
//set size
this.setSize(GUI.WIDTH, GUI.HEIGHT);
//set Focusable
this.setFocusable(true);
//create buffer
buffer = new BufferedImage(GUI.WIDTH, GUI.HEIGHT, BufferedImage.TYPE_INT_ARGB);


}

public void start()
{
//initialize game
this.init();
//start loop

while(true){
try{
this.update();
this.drawbuffer();
this.drawscreen();
}catch(Exception e){e.printStackTrace(); Main.close();}
}

}

private void update()
{
return;
}

private void drawbuffer()
{
//set buffer
Graphics buff = buffer.createGraphics();

//do drawing logic
drawrect(buff, 10, 10, 10, 10);

//clear buffer
buff.dispose();
}

private void drawscreen()
{
Graphics2D g2d = (Graphics2D)getGraphics();

// Render Buffer to Screen
g2d.drawImage(buffer, 0, 0, this);

Toolkit.getDefaultToolkit().sync();

// Dispose of Graphics2D
g2d.dispose();
}


//Generic render method. Change this.
private void drawrect(Graphics buff, int x, int y, int width, int height)
{
buff.setColor(Color.BLACK);
buff.drawRect(x, y, width, height);
}
}
[/CODE]
( [url="http://pastebin.com/A8hEgMMf"]http://pastebin.com/A8hEgMMf[/url] )
I get the error:
[CODE]
java.lang.NullPointerException
at net.devox.basic.Game.drawscreen(Game.java:81)
at net.devox.basic.Game.start(Game.java:57)
at net.devox.basic.Main.main(Main.java:19)
[/CODE]
when I run it.

Does anyone have any advice or know how to fix the error? I'd greatly appreciate any feedback.

Thank you for your time.
0

Share this post


Link to post
Share on other sites
Time to get acquainted with a debugger.

Obviously the null pointer occurred at line 86. Which line is this exactly?
Then look at that line, which variable could be null there?

PasteBin says, line 86 is "[color="#000000"]g2d[/color][color="#666600"].[/color][color="#000000"]dispose[/color][color="#666600"]();[/color]". Which does not make much sense.
If g2d was null, it would have thrown at drawImage already. Unless the null pointer was an inner one in Graphics2d, which occurred, because sync cleaned something up.

Other possibility: The stack trace is wrong (would be weird) and the null pointer occurs at Toolkit.getDefaultToolkit().sync() (ie. Toolkit.getDefaultToolkit() returns null, which i personally don't believe).
0

Share this post


Link to post
Share on other sites
I think I messed up somewhere while posting this...
It highlights line 81,[color=#181818][font=monospace][size=3] g2d.[/size][/font][/color][color=#006633][font=monospace][size=3]drawImage[/size][/font][/color][color=#009900][font=monospace][size=3]([/size][/font][/color][color=#181818][font=monospace][size=3]buffer, [/size][/font][/color][color=#CC66CC][font=monospace][size=3]0[/size][/font][/color][color=#181818][font=monospace][size=3], [/size][/font][/color][color=#CC66CC][font=monospace][size=3]0[/size][/font][/color][color=#181818][font=monospace][size=3], [/size][/font][/color][color=#000000][font=monospace][size=3][b]this[/b][/size][/font][/color][color=#009900][font=monospace][size=3]);[/size][/font][/color]

I have no idea what could be null there. I mean, I guess it has to be buffer, but I clearly defined buffer in the init() method.
0

Share this post


Link to post
Share on other sites
[quote name='Eckhart' timestamp='1326008728' post='4900571']
I think I messed up somewhere while posting this...
It highlights line 81,[color=#181818][font=monospace][size=3] g2d.[/size][/font][/color][color=#006633][font=monospace][size=3]drawImage[/size][/font][/color][color=#009900][font=monospace][size=3]([/size][/font][/color][color=#181818][font=monospace][size=3]buffer, [/size][/font][/color][color=#CC66CC][font=monospace][size=3]0[/size][/font][/color][color=#181818][font=monospace][size=3], [/size][/font][/color][color=#CC66CC][font=monospace][size=3]0[/size][/font][/color][color=#181818][font=monospace][size=3], [/size][/font][/color][color=#000000][font=monospace][size=3][b]this[/b][/size][/font][/color][color=#009900][font=monospace][size=3]);[/size][/font][/color]

I have no idea what could be null there. I mean, I guess it has to be buffer, but I clearly defined buffer in the init() method.
[/quote]
Where do you call the init() method?
0

Share this post


Link to post
Share on other sites
[quote name='Eckhart' timestamp='1326008728' post='4900571']
I think I messed up somewhere while posting this...
It highlights line 81,[color=#181818][font=monospace][size=3] g2d.[/size][/font][/color][color=#006633][font=monospace][size=3]drawImage[/size][/font][/color][color=#009900][font=monospace][size=3]([/size][/font][/color][color=#181818][font=monospace][size=3]buffer, [/size][/font][/color][color=#CC66CC][font=monospace][size=3]0[/size][/font][/color][color=#181818][font=monospace][size=3], [/size][/font][/color][color=#CC66CC][font=monospace][size=3]0[/size][/font][/color][color=#181818][font=monospace][size=3], [/size][/font][/color][color=#000000][font=monospace][size=3][b]this[/b][/size][/font][/color][color=#009900][font=monospace][size=3]);[/size][/font][/color]

I have no idea what could be null there. I mean, I guess it has to be buffer, but I clearly defined buffer in the init() method.
[/quote]
No, it can only be the graphics context.
Take a look at getGraphics. You won't get a context if you don't have a parent component for the canvas.
0

Share this post


Link to post
Share on other sites
[quote name='RulerOfNothing' timestamp='1326009606' post='4900574']
[quote name='Eckhart' timestamp='1326008728' post='4900571']
I think I messed up somewhere while posting this...
It highlights line 81,[color=#181818][font=monospace][size=3] g2d.[/size][/font][/color][color=#006633][font=monospace][size=3]drawImage[/size][/font][/color][color=#009900][font=monospace][size=3]([/size][/font][/color][color=#181818][font=monospace][size=3]buffer, [/size][/font][/color][color=#CC66CC][font=monospace][size=3]0[/size][/font][/color][color=#181818][font=monospace][size=3], [/size][/font][/color][color=#CC66CC][font=monospace][size=3]0[/size][/font][/color][color=#181818][font=monospace][size=3], [/size][/font][/color][color=#000000][font=monospace][size=3][b]this[/b][/size][/font][/color][color=#009900][font=monospace][size=3]);[/size][/font][/color]

I have no idea what could be null there. I mean, I guess it has to be buffer, but I clearly defined buffer in the init() method.
[/quote]
Where do you call the init() method?
[/quote]

in the beginning of the start() method.


[quote name='6510' timestamp='1326023440' post='4900610']
No, it can only be the graphics context.
Take a look at getGraphics. You won't get a context if you don't have a parent component for the canvas.
[/quote]
I'm not sure what you're asking. The class extends canvas, if that's what you mean.
0

Share this post


Link to post
Share on other sites
The null pointer is g2d, I believe: in line 78, if (Graphics2D)getGraphics() doesn't return a valid pointer or fully instantiated object reference, then g2d is a bad pointer, and trying to dereference it with the dot operator (in line 81) generates your null pointer exception. You can check this by testing g2d directly after the instantiation call if you want. Look at the getGraphics() method and see what it relies on to work.
0

Share this post


Link to post
Share on other sites
Fixed the error :D turns out it was in my main function :/ I still don't understand it....
I was calling the Game.start() method in the main method.
I decided to change it to see what would happen.
I put the Game.start() method in the constructor of the GUI class.
GUI() is called in the main method.
Why does it work now, but didn't work earlier?
0

Share this post


Link to post
Share on other sites
Without seeing the source code in its entirety, I'd wager it has to do with the order of object initialization. Your code can be syntactically sound, but if the overall design makes assumptions on objects being initialized correctly (to include method calls that just assume their "this" pointer is already valid) you won't see the problem until runtime.
0

Share this post


Link to post
Share on other sites
Because if you call it in your main method the Graphics object from the GUI might not be initialized yet. You've created a race situation because your main method is in one thread and the GUI (i assume it's a JFrame or something) runs in the GUI thread.

You also shouldn't make that start method the way you did. You should have an object that Implements Runnable and use that to Initilized a Thread and you can start that. Also never do a while(true). Try creating a static volatile boolean variable called isRunning and make it equal true. That way when you need to stop the thread gracefully you just make isRunning = false so that on the next iteration of the loop it stops.
0

Share this post


Link to post
Share on other sites
[quote name='medv4380' timestamp='1326408996' post='4902138']
Because if you call it in your main method the Graphics object from the GUI might not be initialized yet. You've created a race situation because your main method is in one thread and the GUI (i assume it's a JFrame or something) runs in the GUI thread.
[/quote]
Thanks, I think that's why.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0