Results 1 to 9 of 9
  1. #1
    Bojevnik is offline Member
    Join Date
    Aug 2007
    Posts
    13
    Rep Power
    0

    Default NullPointerException - createImage()

    In this prgram i get NullpoinerExceptiion. Program reports it in lin marked ***, but the problem one line above (marked **). After calling createImage mamImage is still null.
    What is the problem in how to fix it?

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class Panela extends JPanel implements Runnable{
        int korak=0;
        int zakasnitev=50;
        boolean running=false;
        
        Image memImage;
        Graphics memGraphics;
        Thread nit;
    
        Panela(){
            **memImage= createImage(500,500);
            ***memGraphics = memImage.getGraphics();
            setVisible(true);
            start();
        }
        public void run(){
            while(running){
                long cas=System.currentTimeMillis();
                korak++;
                repaint();
                cas=System.currentTimeMillis();
                try{
                    if(cas<zakasnitev)
                    Thread.sleep(zakasnitev-(int) cas);
                }
                catch(InterruptedException e){e.printStackTrace(); System.exit(-1);}
            }
        }
        public void start(){
            if(!running) {
                nit= new Thread(this);
                nit.start();
                running=true;
            }
        }
        public void paint(Graphics g){
            update(g);
        }
        public void update(Graphics g){
            memGraphics.setColor(getBackground());
            memGraphics.fillRect(0, 0, getWidth(), getHeight());
            memGraphics.setColor(Color.black);
            int odmik= korak % 20;
            for(int x=0; x<getWidth()+getHeight(); x+=20){
                memGraphics.drawLine(x, 0, 0, x);            
            }//for
            g.drawImage(memImage, 0, 0, this);
        }
        
    }
    I HATE SMURFS!!!!!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    I've never used Component.getImage, so I'm not that familiar with its use, or whether what you are trying to do (what are you trying to do, by the way) is the correct way to do things, but I'm going to guess that no image will be available until a component has been rendered either with a call to pack() or setVisible(true) on a root container that holds your panel.

    But again, what is your goal here? You're overriding paint which probably shouldn't be done. Instead you should probably override paintComponent.

    Also, you're using a while(true) construct, and I'll bet you want to use a Swing Timer instead.

    HTH.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default

    Have you tried the code without the memGraphics object? Are you trying to double buffer the output?
    For Swing components, override paintComponent() vs paint().

  4. #4
    Bojevnik is offline Member
    Join Date
    Aug 2007
    Posts
    13
    Rep Power
    0

    Default

    My goal is learn about graphics and animation.
    The whole program is from a book, but it was written for applets and im trying to conver it normal program.

    I have tried the code without memGraphics and i works
    and yes the point of it double buffering.
    I HATE SMURFS!!!!!

  5. #5
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class PanelaRx extends JPanel implements Runnable{
        int korak=0;
        int zakasnitev=50;
        boolean running=false;
        
        Image memImage;
        Graphics memGraphics;
        Thread nit;
    
        PanelaRx(){
            // See Method Detail for Component [i]createImage[/i] method.
            System.out.println("createImage = " + createImage(500,500));
    //        memImage= createImage(500,500);
    //        memGraphics = memImage.getGraphics();
    //        setVisible(true);
            startFirstTime();
        }
    
        public void run(){
            while(running){
                long cas=System.currentTimeMillis();
                korak++;
                update();
                repaint();
                cas=System.currentTimeMillis();
                try{
                    if(cas<zakasnitev)
                    Thread.sleep(zakasnitev-(int) cas);
                } catch(InterruptedException e) {
                    e.printStackTrace();
                    System.exit(-1);
                }
            }
        }
    
        public void start(){
            if(!running) {
                running=true;
                nit= new Thread(this);
                nit.setPriority(Thread.NORM_PRIORITY);
                nit.start();
            }
        }
    
        private void startFirstTime() {
            // Wait for gui to be realized.
            new Thread(new Runnable() {
                public void run() {
                    do {
                        try {
                            Thread.sleep(25);
                        } catch(InterruptedException e) {
                            System.out.println("wait interrupted");
                            break;
                        }
                    } while(!isDisplayable());
                    // Now it should be safe to make these calls.
                    memImage= createImage(500,500);
                    memGraphics = memImage.getGraphics();
                    start();
                }
            }).start();
        }
    
        protected void paintComponent(Graphics g){
            g.drawImage(memImage, 0, 0, this);
        }
    
        /**
         * Swing is double buffered by default. There are times when
         * it is useful in Swing. It is recommended that we do not
         * override the [i]update[/i] method in swing (see Method
         * Detail section in JComponent api). Your drawing code looks
         * like older AWT drawing code.
         */
        private void update(){
            memGraphics.setColor(getBackground());
            memGraphics.fillRect(0, 0, getWidth(), getHeight());
            memGraphics.setColor(Color.black);
            int odmik= korak % 20;
            for(int x=0; x<getWidth()+getHeight(); x+=20){
                memGraphics.drawLine(x, 0, 0, x);            
            }
        }
    
        public Dimension getPreferredSize() {
            return new Dimension(500,500);
        }
    
        public static void main(String[] nargs) {
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(new PanelaRx());
            f.pack();
            f.setLocation(200,200);
            f.setVisible(true);
        }
    }

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default

    I think Swing components are automatically double buffered.
    Look at using the BufferedImage class to build your image in before drawing it.

  7. #7
    Bojevnik is offline Member
    Join Date
    Aug 2007
    Posts
    13
    Rep Power
    0

    Default

    Thank you all for your help.
    It seem the problem was frame wasn created by the time image was created (if i understood it correctly).

    I also have a few other questions:
    Why is overrindind paintComponent better than overrinding paint?

    hardwired: thank you for the code, but it seems to be "blickering" (i hope i said that right), which was the thing i was trying to aviod.

    Also here is my new working code(althought not as good as it should be.
    It overides paintC instead of PaintComponent (i did try overriding painComponent, but I spotted no difference between the two, so i just left it paint).
    And i could include delay or some other failsefe that memImage wont initialize before frame.

    Here is my working code, any comment are welcome.
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class Panela extends JPanel implements Runnable{
        int korak=0;
        int zakasnitev=500;
        boolean running=false;
        
        Image memImage;
        Graphics memGraphics;
        Thread nit;
    
        Panela(){
        }
        public void run(){
            while(running){
                long cas=System.currentTimeMillis();
                korak++;
                repaint();
                cas=System.currentTimeMillis();
                try{
                    if(cas<zakasnitev)
                    Thread.sleep(zakasnitev-(int) cas);
                }
                catch(InterruptedException e){e.printStackTrace(); System.exit(-1);}
            }
        }
        public void start(){        
            if(!running) {
                nit= new Thread(this);
                nit.start();
                running=true;
            }
        }
        public void paintComponent(Graphics g){
            memImage= createImage(500,500);
            memGraphics = memImage.getGraphics();
            if(!running) start();
            update(g);
        }
        public void update(Graphics g){
            memGraphics.setColor(getBackground());
            memGraphics.fillRect(0, 0, getWidth(), getHeight());
            memGraphics.setColor(Color.black);
            int odmik= korak % 20;
            for(int x=0; x<getWidth()+getHeight(); x+=20){
                memGraphics.drawLine(x, 0, 0, x);            
            }//for
            g.drawImage(memImage, 0, 0, this);
        }
        
    }
    Last edited by Bojevnik; 06-29-2008 at 02:06 PM. Reason: just waqnted make some thing clearer
    I HATE SMURFS!!!!!

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Bojevnik View Post
    Thank you all for your help.
    It seem the problem was frame wasn created by the time image was created (if i understood it correctly).
    Ah, just as I suspected!

    I also have a few other questions:
    Why is overrindind paintComponent better than overrinding paint?
    paintComponent is responsible for just that: painting the component. paint on the other hand is responsible for more: painting the component, painting the border, and painting the component's children. If your paint override method is not attempting to affect the painting of the border or the child components, it's best not to mess with it and instead you should override the more specific method, paintComponent.

    Good luck.

  9. #9
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class PanelaRx extends JPanel implements Runnable{
        Thread nit;
        boolean running=false;
        int korak = 0;
    
        PanelaRx(){
            start();
        }
    
        public void run(){
            while(running){
                korak++;
                repaint();
                try{
                    Thread.sleep(100);
                } catch(InterruptedException e) {
                    break;
                }
            }
        }
    
        public void start(){
            if(!running) {
                running=true;
                nit= new Thread(this);
                nit.setPriority(Thread.NORM_PRIORITY);
                nit.start();
            }
        }
    
        protected void paintComponent(Graphics g){
            super.paintComponent(g);
            int odmik= korak % 20;
            for(int x=odmik; x<getWidth()+getHeight(); x+=20){
                g.drawLine(x, 0, 0, x);            
            }
        }
    
        public static void main(String[] nargs) {
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(new PanelaRx());
            f.setSize(500,500);
            f.setLocation(200,200);
            f.setVisible(true);
        }
    }

Similar Threads

  1. NullPointerException
    By adeeb in forum AWT / Swing
    Replies: 3
    Last Post: 06-11-2008, 09:42 AM
  2. NullPointerException
    By mensa in forum Java 2D
    Replies: 5
    Last Post: 05-04-2008, 12:19 AM
  3. NullPointerException
    By ravian in forum New To Java
    Replies: 2
    Last Post: 12-07-2007, 05:20 PM
  4. NullPointerException
    By Feng in forum New To Java
    Replies: 5
    Last Post: 11-24-2007, 08:51 PM
  5. nullPointerException problem
    By conandor in forum Networking
    Replies: 1
    Last Post: 08-14-2007, 02:22 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
  •