Results 1 to 8 of 8
  1. #1
    luke24 is offline Member
    Join Date
    Feb 2011
    Posts
    4
    Rep Power
    0

    Default Graphics problem

    Hi trying to draw a circle on my frame.

    Getting the error

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at SuperStarTrekGame.paint(SuperStarTrekGame.java:44)

    Java Code:
    import java.awt.Graphics;
    import javax.swing.JFrame;
    
    
    public class SuperStarTrekGame extends JFrame{
    
        private int numberOfKlingons, numberOfRomulans;
        private boolean setSuperCommander;
        private int timeRemaining;
        private int numberOfStarBases, numberOfBlackHoles;
        private String difficulty;
        private Quadrant[] galaxy = new Quadrant[7];
        private int arrayPos;
    
       
    
        public void prepareGame(){
    
         
            arrayPos = 5;
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            setSize(800,600);
            setVisible(true);
    
        }
    
        public void adjustTime(){
    
        }
    
        
        public void paint(Graphics g){
    
            galaxy[arrayPos].paint(g);
    
        }
    
        public void checkGame(){
    
        }
    
        public static void main(String[] args){
    
            SuperStarTrekGame game = new SuperStarTrekGame();
            game.prepareGame();
            
            
    
        }
    
    }
    
    
    
    import java.awt.Color;
    import java.awt.Graphics;
    
    
    public class Quadrant{
    
        public Quadrant(){
    
        }
    
       
    
       
        public void paint(Graphics g){
    
            g.setColor(Color.green);
            g.drawOval(40, 40, 100, 100);
            g.fillOval(40, 40, 100, 100);
            
            
        }
    
        
    
    
    
    }
    The error is on
    galaxy[arrayPos].paint(g);

    Any ideas?

  2. #2
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    I see a declared and initialized array of Quadrants, but nowhere do I see the Quadrants being initialized, so the array galaxy contains null values. That's probably the error, but it's possible you're not showing us the whole code. Also, instance variables should be initialized in the constructor, not at declaration.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  3. #3
    luke24 is offline Member
    Join Date
    Feb 2011
    Posts
    4
    Rep Power
    0

    Default

    Ah ofcourse, I need a for loop to initialise each Quadrant in the array.

    thx !

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by m00nchile View Post
    Also, instance variables should be initialized in the constructor, not at declaration.
    Care to elaborate on that? because I think it doesn't matter (much) where those member variables are initialized.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    luke24 is offline Member
    Join Date
    Feb 2011
    Posts
    4
    Rep Power
    0

    Default

    Just a quick question, I am drawing my image fine, but as far as I know, I should be using paintComponent rather then paint since use Jframe. But when I use paintComponent, the circle is not drawn, it works fine with paint tho?

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by luke24 View Post
    Just a quick question, I am drawing my image fine, but as far as I know, I should be using paintComponent rather then paint since use Jframe. But when I use paintComponent, the circle is not drawn, it works fine with paint tho?
    A JFrame isn't a JComponent so it hasn't a paintComponent( ... ) method and you can't override it. Add a JPanel to your frame and paint on that.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    luke24 is offline Member
    Join Date
    Feb 2011
    Posts
    4
    Rep Power
    0

    Default

    So should I instead extend Jpanel and create a JFrame in the constructor, since paintComponent will need to override?

  8. #8
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Quote Originally Posted by JosAH View Post
    Care to elaborate on that? because I think it doesn't matter (much) where those member variables are initialized.

    kind regards,

    Jos
    It's more of a good practice thing in my eyes. Perhaps my wording was a bit too strong with the should.
    Ever seen a dog chase its tail? Now that's an infinite loop.

Similar Threads

  1. Drawing a graphics onto another Graphics ?
    By Ziden in forum Java Applets
    Replies: 0
    Last Post: 01-08-2011, 07:30 PM
  2. problem with jframe&jmenu&graphics
    By beni.vd in forum AWT / Swing
    Replies: 1
    Last Post: 12-31-2010, 07:07 PM
  3. i am having a problem with creating graphics
    By umpire43055 in forum AWT / Swing
    Replies: 3
    Last Post: 08-07-2009, 05:26 AM
  4. Problem in g.drawArc() api of Graphics class
    By narasimman in forum AWT / Swing
    Replies: 0
    Last Post: 07-30-2008, 08:19 AM
  5. Problem in printing java graphics
    By Mahendra in forum Java 2D
    Replies: 0
    Last Post: 01-23-2008, 12:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •