Copied code from this tutorial- BufferStrategy
Java Tutorial - Making a 2D Game Part 6 - YouTube
However, the code compiled perfectly but when it runs it errors
Exception in thread "Thread-3" java.lang.NullPointerException
at GameCanvas.Render(GameCanvas.java:72)
at GameCanvas.run(GameCanvas.java:50)
at java.lang.Thread.run(Thread.java:680)
Code:
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
@SuppressWarnings("serial")
public class GameCanvas extends Canvas implements Runnable
{
int FWidth = 800;
int FHeight = 600;
public long period = 10;
public BufferStrategy Buffer;
public Graphics graphics;
public AppleEntity apple;
private Thread t;
public GameCanvas()
{
this.setIgnoreRepaint(true);
this.setBounds(0,0,FWidth,FHeight);
this.setBackground(Color.white);
this.setVisible(true);
apple = new AppleEntity("Apple.png",200,200);
}
public void addNotify()
{
if(t == null)
{
t = new Thread(this);
t.start();
}
}
@SuppressWarnings("static-access")
public void run()
{
while(true)
{
long beginTime = System.currentTimeMillis();
Update();
Render();
Draw();
long timeTaken = System.currentTimeMillis() - beginTime;
long sleepTime = period - timeTaken;
try
{
t.sleep(sleepTime);
}
catch(Exception e) { }
}
}
public void Update()
{
apple.Fall();
}
public void Render()
{
graphics = Buffer.getDrawGraphics();
graphics.setColor(Color.white);
graphics.fillRect(0,0,FWidth,FHeight);
//Paint stuff
apple.Draw(graphics);
}
public void Draw()
{
if(!Buffer.contentsLost())
{
Buffer.show();
if(graphics != null)
{
graphics.dispose();
}
}
}
}
I have never done BufferStrategy before, what is wrong?
Thanks
Re: Copied code from this tutorial- BufferStrategy
Quote:
Exception in thread "Thread-3" java.lang.NullPointerException
at GameCanvas.Render(GameCanvas.java:72)
Look at the statement at line 72 and see which variable has a null value. If you can not tell which one, add a println just before line 72 that prints out the values of all the variable used on line 72.
When you get the variable that has a null value, back track in your code to see why it does not have a valid value.
Re: Copied code from this tutorial- BufferStrategy
Code:
public void Render()
{
System.out.println("1 "+graphics);
System.out.println("2 "+Buffer);
graphics = Buffer.getDrawGraphics();
graphics.setColor(Color.white);
graphics.fillRect(0,0,FWidth,FHeight);
//Paint stuff
apple.Draw(graphics);
}
1 null
Exception in thread "Thread-3" 2 null
java.lang.NullPointerException
at GameCanvas.Render(GameCanvas.java:73)
at GameCanvas.run(GameCanvas.java:49)
at java.lang.Thread.run(Thread.java:680)
Re: Copied code from this tutorial- BufferStrategy
Where is the variable Buffer given a valid value? Check back through your code and make sure graphics is getting a valid value.
Why print out the value of graphics before the assignment statement that gives it a value???
Re: Copied code from this tutorial- BufferStrategy
It doesn't have one, what value do I need to give it?
Re: Copied code from this tutorial- BufferStrategy
Quote:
what value do I need to give it?
That depends.
What is the method Render() supposed to do? Why do you call it?
What objects does it need to do it job?
Normally drawing methods are called from a paintComponent method and are passed the Graphics object that they receive as argument.
Re: Copied code from this tutorial- BufferStrategy
A lot of unneeded code but I posted it all to find this problem.
Code:
import java.awt.Graphics;
import java.awt.Rectangle;
public abstract class Entity
{
protected double x;
protected double y;
protected Sprite sprite;
protected double Dy;
protected double Dx;
private Rectangle me = new Rectangle();
private Rectangle him = new Rectangle();
public Entity(String ref,int x, int y)
{
this.sprite = ImageLoader.get().getSprite(ref);
this.x = x;
this.y = y;
}
public void move(long delta)
{
x += (delta * Dx)/1000;
y += (delta * Dy)/1000;
}
public void setHorizontalMovement(double dx)
{
this.Dx = dx;
}
public void setVerticalMovement(double dy)
{
this.Dy = dy;
}
public double getHorizontalMovement()
{
return Dx;
}
public double getVerticalMovement()
{
return Dy;
}
public void Draw(Graphics g)
{
sprite.Draw(g,(int) x,(int) y);
}
public int getX()
{
return (int) x;
}
public int getY()
{
return (int) y;
}
public boolean collidesWidth(Entity e)
{
me.setBounds((int) x, (int) y,sprite.getWidth(),sprite.getHeight());
him.setBounds((int) e.x,(int) e.y, e.sprite.getWidth(),e.sprite.getHeight());
return me.intersects(him);
}
//public abstract void collidesWidth(Entity e);
}
Code:
import java.awt.Graphics;
import java.awt.Image;
public class Sprite
{
private Image image;
public Sprite(Image image)
{
this.image = image;
}
public int getWidth()
{
return image.getWidth(null);
}
public int getHeight()
{
return image.getHeight(null);
}
public void Draw(Graphics g, int x, int y)
{
g.drawImage(image,x,y,null);
}
}
Code:
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import javax.imageio.ImageIO;
public class ImageLoader
{
private static ImageLoader single = new ImageLoader();
public static ImageLoader get()
{
return single;
}
private HashMap<String,Sprite> Images = new HashMap<String,Sprite>();
public Sprite getSprite(String ref)
{
if(Images.get(ref) != null)
{
return (Sprite) Images.get(ref);
}
BufferedImage sourceImage = null;
try
{
sourceImage = ImageIO.read(new File (ref));
}
catch(IOException e)
{
System.out.println("Unable to load: " + ref);
}
GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
Image image = gc.createCompatibleImage(sourceImage.getWidth(),sourceImage.getHeight(),Transparency.BITMASK);
image.getGraphics().drawImage(sourceImage,0,0,null);
Sprite sprite = new Sprite(image);
Images.put(ref,sprite);
return sprite;
}
}
Re: Copied code from this tutorial- BufferStrategy
What is the problem now? What is all the code for?
Re: Copied code from this tutorial- BufferStrategy
It's all part of render I guess :) The apple extends entity which extends sprite which uses ImageLoader to load the image. However, the error is being called before apple.Draw(graphics) gets called.
This line graphics = Buffer.getDrawGraphics(); specifically.
Re: Copied code from this tutorial- BufferStrategy
If the variable: Buffer is null when you try to use it, then Where is the variable Buffer given a value?
Re: Copied code from this tutorial- BufferStrategy
It isn't. It is only declared Code:
public BufferStrategy Buffer;
What would give it? CreateBufferStrategy?
Re: Copied code from this tutorial- BufferStrategy
If its only declared then it will be null.
Where did you get this code? Does it have examples on how to make it work?
Have you looked in the API doc for the BufferStrategy class to see if there are any clues there?
Re: Copied code from this tutorial- BufferStrategy
YouTube link at the top. The source isn't provided which is quite annoying. graphics will be null because it is based off Buffer. What value do I give Buffer?
The code works fine if i comment out render and draw, so there are no errors elsewhere. Doesn't draw anything though.
Re: Copied code from this tutorial- BufferStrategy
Have you looked in the API doc for the BufferStrategy class to see if there are any clues there?
Re: Copied code from this tutorial- BufferStrategy
The docs say this. Code:
// Check the capabilities of the GraphicsConfiguration
...
// Create our component
Window w = new Window(gc);
// Show our window
w.setVisible(true);
// Create a general double-buffering strategy
w.createBufferStrategy(2);
BufferStrategy strategy = w.getBufferStrategy();
// Render loop
while (!done) {
Graphics g = strategy.getDrawGraphics();
// Draw to graphics
...
strategy.show();
}
// Dispose the window
w.setVisible(false);
w.dispose();
So I added this to my GameCanvas() function.
Code:
public GameCanvas()
{
this.setIgnoreRepaint(true);
this.setBounds(0,0,FWidth,FHeight);
this.setBackground(Color.white);
this.createBufferStrategy(2);
Buffer = getBufferStrategy();
this.setVisible(true);
apple = new AppleEntity("Apple.png",200,200);
}
New error, yay!
Exception in thread "main" java.lang.IllegalStateException: Component must have a valid peer
at java.awt.Component$FlipBufferStrategy.createBuffer s(Component.java:3843)
at java.awt.Component$FlipBufferStrategy.<init>(Compo nent.java:3817)
at java.awt.Component$FlipSubRegionBufferStrategy.<in it>(Component.java:4358)
at java.awt.Component.createBufferStrategy(Component. java:3699)
at java.awt.Canvas.createBufferStrategy(Canvas.java:1 66)
at java.awt.Component.createBufferStrategy(Component. java:3623)
at java.awt.Canvas.createBufferStrategy(Canvas.java:1 41)
at GameCanvas.<init>(GameCanvas.java:27)
at GameFrame.main(GameFrame.java:16)
Re: Copied code from this tutorial- BufferStrategy
Quote:
java.lang.IllegalStateException: Component must have a valid peer
Does the component you are working with have a valid peer? The JVM says not.
You need to research when a GUI component gets a peer. I've never used this class so I have no links to pass on.
Is the component a GUI item and is it visible?
Re: Copied code from this tutorial- BufferStrategy
Code:
import javax.swing.JFrame;
public class GameFrame
{
public static void main(String args[])
{
int FWidth = 800;
int FHeight = 600;
JFrame frame = new JFrame("Game Name");
frame.setIgnoreRepaint(true);
frame.setBounds(0,0,FWidth,FHeight);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GameCanvas game = new GameCanvas();
frame.add(game);
frame.setVisible(true);
frame.createBufferStrategy(2);
game.Buffer = frame.getBufferStrategy();
}
}
I've put them here now instead. both logs output null again with
Exception in thread "Thread-3" java.lang.NullPointerException
at GameCanvas.Render(GameCanvas.java:76)
at GameCanvas.run(GameCanvas.java:52)
at java.lang.Thread.run(Thread.java:680)
No idea what is wrong and they shouldn't be null.
Re: Copied code from this tutorial- BufferStrategy
What variable at line 76 is null? When/where do you give that variable a value?
Do you give it a value BEFORE your code tries to use it?
Re: Copied code from this tutorial- BufferStrategy
Cracked it! Moved frame.add(game); below the buffer declaration. Fantastic! Thanks
Re: Copied code from this tutorial- BufferStrategy