Results 1 to 20 of 20
- 11-13-2011, 06:19 PM #1
Member
- Join Date
- Jul 2011
- Posts
- 19
- Rep Power
- 0
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)
I have never done BufferStrategy before, what is wrong?Java 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(); } } } }
Thanks
- 11-13-2011, 06:39 PM #2
Re: Copied code from this tutorial- BufferStrategy
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.Exception in thread "Thread-3" java.lang.NullPointerException
at GameCanvas.Render(GameCanvas.java: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.
- 11-14-2011, 06:04 PM #3
Member
- Join Date
- Jul 2011
- Posts
- 19
- Rep Power
- 0
Re: Copied code from this tutorial- BufferStrategy
1 nullJava 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); }
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)
- 11-14-2011, 06:08 PM #4
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???Last edited by Norm; 11-14-2011 at 06:13 PM.
- 11-14-2011, 06:09 PM #5
Member
- Join Date
- Jul 2011
- Posts
- 19
- Rep Power
- 0
Re: Copied code from this tutorial- BufferStrategy
It doesn't have one, what value do I need to give it?
- 11-14-2011, 06:17 PM #6
Re: Copied code from this tutorial- BufferStrategy
That depends.what value do I need to give it?
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.
- 11-14-2011, 06:25 PM #7
Member
- Join Date
- Jul 2011
- Posts
- 19
- Rep Power
- 0
Re: Copied code from this tutorial- BufferStrategy
A lot of unneeded code but I posted it all to find this problem.
Java 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); }Java 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); } }Java 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; } }Last edited by JavaWizKid; 11-14-2011 at 06:27 PM.
- 11-14-2011, 06:32 PM #8
Re: Copied code from this tutorial- BufferStrategy
What is the problem now? What is all the code for?
- 11-14-2011, 06:34 PM #9
Member
- Join Date
- Jul 2011
- Posts
- 19
- Rep Power
- 0
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.
- 11-14-2011, 06:36 PM #10
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?
- 11-14-2011, 06:38 PM #11
Member
- Join Date
- Jul 2011
- Posts
- 19
- Rep Power
- 0
Re: Copied code from this tutorial- BufferStrategy
It isn't. It is only declared
What would give it? CreateBufferStrategy?Java Code:public BufferStrategy Buffer;
- 11-14-2011, 06:40 PM #12
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?
- 11-14-2011, 06:43 PM #13
Member
- Join Date
- Jul 2011
- Posts
- 19
- Rep Power
- 0
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.
- 11-14-2011, 06:43 PM #14
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?
- 11-14-2011, 06:53 PM #15
Member
- Join Date
- Jul 2011
- Posts
- 19
- Rep Power
- 0
Re: Copied code from this tutorial- BufferStrategy
The docs say this.
Java 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.
Java 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)
- 11-14-2011, 06:59 PM #16
Re: Copied code from this tutorial- BufferStrategy
Does the component you are working with have a valid peer? The JVM says not.java.lang.IllegalStateException: Component must have a valid peer
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?
- 11-14-2011, 07:00 PM #17
Member
- Join Date
- Jul 2011
- Posts
- 19
- Rep Power
- 0
Re: Copied code from this tutorial- BufferStrategy
I've put them here now instead. both logs output null again withJava 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(); } }
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.
- 11-14-2011, 07:02 PM #18
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?
- 11-14-2011, 07:04 PM #19
Member
- Join Date
- Jul 2011
- Posts
- 19
- Rep Power
- 0
Re: Copied code from this tutorial- BufferStrategy
Cracked it! Moved frame.add(game); below the buffer declaration. Fantastic! Thanks
- 11-14-2011, 07:06 PM #20
Similar Threads
-
I am getting a cannot find symbol error on this code from Oracle Java tutorial site
By bigsonny in forum New To JavaReplies: 8Last Post: 06-15-2011, 05:26 AM -
BufferStrategy in JFrame
By dbomb in forum AWT / SwingReplies: 3Last Post: 05-23-2011, 01:43 AM -
BufferStrategy
By Shadowdude04 in forum AWT / SwingReplies: 0Last Post: 03-13-2010, 02:59 PM -
BufferStrategy and white rows of pixels
By TobyLobster in forum Java 2DReplies: 2Last Post: 12-29-2008, 01:49 PM -
how can objects themselves be copied???
By ishakteyran in forum New To JavaReplies: 1Last Post: 12-29-2007, 10:04 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks