Results 1 to 16 of 16
Like Tree7Likes
  • 1 Post By Tolls
  • 1 Post By jim829
  • 1 Post By jim829
  • 1 Post By jim829
  • 1 Post By jim829
  • 1 Post By jim829
  • 1 Post By jim829

Thread: ImageIcon.getImage()

  1. #1
    faraa is offline Member
    Join Date
    Mar 2013
    Posts
    70
    Rep Power
    0

    Default ImageIcon.getImage()

    PHP Code:
    g.drawImage(imgs[k].getImage(),.....);
    When this line causes this Exception?
    PHP Code:
    Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
    imgs[] is an array of ImageIcons....

    Thank's in advance

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: ImageIcon.getImage()

    Something is null somewhere.
    Without the stack trace it's hard to tell.
    If that is the line throwing it (and not some line inside drawImage() then either imgs is null, or imgs[k] is null, or something in the '...' is null.
    faraa likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,025
    Rep Power
    6

    Default Re: ImageIcon.getImage()

    You should provide a Short, Self Contained, Correct Example to demonstrate the problem. Is g null? Are you sure all the images from 0 thru k-1 are non-null?

    Regards,
    Jim
    faraa likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    faraa is offline Member
    Join Date
    Mar 2013
    Posts
    70
    Rep Power
    0

    Default Re: ImageIcon.getImage()

    I'm sure that it is non-null.
    Because if I use this statement:
    PHP Code:
     imgs[k].paintIcon(parent, g, x, y);
    The code will run without any Error or Exception...
    (parent is JApplet )

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,025
    Rep Power
    6

    Default Re: ImageIcon.getImage()

    You need to show the relevant code and the exception (which shows the line number where the exception occurred).

    Regards,
    Jim
    faraa likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    faraa is offline Member
    Join Date
    Mar 2013
    Posts
    70
    Rep Power
    0

    Default Re: ImageIcon.getImage()

    PHP Code:
    public class Bird2 extends ScreenObject implements Runnable{
         int x ;
         int y = 0;
        protected ImageIcon[] imgs = new ImageIcon[9];
      
      
        JApplet parent;
        int k=1;
        int sleep;
        Color clBg;
       ScreenManager sm;
    
        public Bird2(int x2,int y,int w,int h,Color cl,int sleep,ScreenManager sm,JApplet parent) {
            super(x2, y, w, h, cl);
            this.parent=parent;
            this.sleep=sleep;
            this.parent=parent;
            this.sm=sm;
           
                    
       //    x=parent.getWidth();
    for (int i = 1; i < imgs.length; i++) {
                imgs[i] = new ImageIcon(getClass().getResource(i + ".PNG"));
            
     
            }
    
        }
    
    
    
    
    
        @Override
        public void run() {
            while (true) {
                for (k = 1; k < imgs.length; k++) {
    
                      x += imgs[k].getIconWidth();
                    try {
                        Thread.sleep(300);
    
                    } catch (InterruptedException ex) {
                    }
                }
            }
        }
    
        @Override
        public void draw(Graphics g) {
            
      //      imgs[k].paintIcon(parent, g, x, y);
    g.drawImage(imgs[k].getImage(), x, y, w, h,parent.getBackground(), parent);
         
            if (k == imgs.length - 1) {
                k = 1;
            }
        }
        
    }

    I hope that it would be helpful...!!!!

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,025
    Rep Power
    6

    Default Re: ImageIcon.getImage()

    Well, I notice that in draw, parent.getBackground() is called. Is it a non-null value? Also, please show the complete Exception trace.

    Regards,
    Jim
    faraa likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: ImageIcon.getImage()

    ...and the full exception with stacktrace.
    Highlighting the line on which the exception is thrown.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    faraa is offline Member
    Join Date
    Mar 2013
    Posts
    70
    Rep Power
    0

    Default Re: ImageIcon.getImage()

    PHP Code:
    run:
    Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
    	at birdhunter3.Bird2.draw(Bird2.java:53)
    	at birdhunter3.ScreenManager.draw(ScreenManager.java:54)
    	at birdhunter3.BirdHunter.paint(BirdHunter.java:62)
    	at javax.swing.RepaintManager$3.run(RepaintManager.java:807)
    	at javax.swing.RepaintManager$3.run(RepaintManager.java:784)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    	at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:784)
    	at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:757)
    	at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:706)
    	at javax.swing.RepaintManager.access$1000(RepaintManager.java:62)
    	at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1651)
    	at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
    	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:727)
    	at java.awt.EventQueue.access$200(EventQueue.java:103)
    	at java.awt.EventQueue$3.run(EventQueue.java:688)
    	at java.awt.EventQueue$3.run(EventQueue.java:686)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    	at java.awt.EventQueue.dispatchEvent(EventQueue.java:697)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    	at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
    va:697)
    	
    BUILD SUCCESSFUL (total time: 3 seconds)

    It is the first time that I use .getBackground method.I don't know how it works exactly...
    Sorry
    Thank you again...

  10. #10
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,025
    Rep Power
    6

    Default Re: ImageIcon.getImage()

    I suspect that parent is null. In paintIcon, parent is used as an observer and in most cases like this, the observer may be null. But not if you are using it to say get the background color.

    Regards,
    Jim
    faraa likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  11. #11
    faraa is offline Member
    Join Date
    Mar 2013
    Posts
    70
    Rep Power
    0

    Default Re: ImageIcon.getImage()

    Thank's a lot.
    I used this.parent=parent in constructor.
    It means that it doesn't any work?

  12. #12
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,025
    Rep Power
    6

    Default Re: ImageIcon.getImage()

    When invoking constructors and painting there is a lot of asynchronous activity going on with different threads. It is possible that the draw method was invoked prior to parent getting set. I suggest you check the value of parent in the draw method. If it is null, simply return. This means you may need to do a repaint later on.

    Regards,
    Jim
    faraa likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  13. #13
    faraa is offline Member
    Join Date
    Mar 2013
    Posts
    70
    Rep Power
    0

    Default Re: ImageIcon.getImage()

    Ok.
    I learned many things
    Thank's a lot for your helping...

  14. #14
    faraa is offline Member
    Join Date
    Mar 2013
    Posts
    70
    Rep Power
    0

    Default Re: ImageIcon.getImage()

    You're right.
    parent is null
    Can you help me How to fix it?
    I really confused....!!!!
    Thank's

  15. #15
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,025
    Rep Power
    6

    Default Re: ImageIcon.getImage()

    Well, I am not certain how Bird2 is invoked. I don't do applets much but I believe there needs to be an init() somewhere. In any event I am assuming the Bird2 constructor is invoked. So you need to check the value of parent in the constructor just to ensure it is not null. If it is, then the problem lies elsewhere (in code not shown).

    If parent is non-null, then you might try the following:

    In draw(), check if parent is null and if it is, simply return.

    Then you need to put a repaint() statement somewhere in your code, probably the start() method.

    Regards,
    Jim
    faraa likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  16. #16
    faraa is offline Member
    Join Date
    Mar 2013
    Posts
    70
    Rep Power
    0

    Default Re: ImageIcon.getImage()

    Thank you very very very much...
    I finally fixed my code....
    Thank youuuuuuu

Similar Threads

  1. Replies: 2
    Last Post: 05-08-2013, 09:46 PM
  2. SWTResourceManager.getImage: Error getting image
    By shay2 in forum SWT / JFace
    Replies: 0
    Last Post: 06-26-2012, 12:38 PM
  3. Japplet.getImage(): What if image file does not exist?
    By CoderMan in forum Java Applets
    Replies: 0
    Last Post: 02-12-2010, 12:31 AM
  4. Replies: 0
    Last Post: 11-13-2009, 04:18 PM
  5. getImage - not fail when image doesn't exist
    By leonid.ge in forum Java Applets
    Replies: 0
    Last Post: 07-24-2009, 04:17 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
  •