Results 1 to 7 of 7

Thread: Thread Error

  1. #1
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default Thread Error

    Something is causing this error and I don't know why:
    Java Code:
    Exception in thread "Thread-2" java.lang.IllegalArgumentException: im == null!
    	at javax.imageio.ImageIO.write(Unknown Source)
    	at javax.imageio.ImageIO.write(Unknown Source)
    	at RecordThread.run(RecordThread.java:44)
    	at java.lang.Thread.run(Unknown Source)
    here is RecordThread class:
    Java Code:
    import java.awt.AWTException;
    import java.awt.Dimension;
    import java.awt.Image;
    import java.awt.MouseInfo;
    import java.awt.Rectangle;
    import java.awt.Robot;
    import java.awt.Toolkit;
    import java.awt.event.MouseAdapter;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    
    import javax.imageio.ImageIO;
    
    
    public class RecordThread implements Runnable{
    	
    	public int PIC_ID;
    	
    	public RecordThread(int id){
    		setPIC_ID(id);
    	}
    
    	public int getPIC_ID() {
    		return PIC_ID;
    	}
    
    	public void setPIC_ID(int pIC_ID) {
    		PIC_ID = pIC_ID;
    	}
    
    	@Override
    	public void run() {
    		Core.log("Starting thread for PIC_ID" + getPIC_ID());
    		
    		try {
    			Robot robot = new Robot();
    			DrawImageThread dit = new DrawImageThread();
    			
    			BufferedImage image = robot.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
    			Thread t = new Thread(new DrawImageThread(image, getPIC_ID()));
    			t.start();
    			Thread.sleep(1000);
                ImageIO.write(dit.returnedImage(), "png", new File(System.getProperty("user.home") + "\\SCapture\\.temp\\img"+getPIC_ID()+".png"));
    			
    		} catch (AWTException e) {
    			// TODO Auto-generated catch block
    			Core.log("Exception #3");
    			e.printStackTrace();
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} catch (InterruptedException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		
    		
    		Core.log("PIC_ID" + getPIC_ID());
    		
    	}
    
    }
    How can I fix this error?

  2. #2
    couling is offline Member
    Join Date
    Nov 2010
    Posts
    54
    Rep Power
    0

    Default

    It's not entirely clear because the DrawImageThread isn't a class I'm aware of. I'm guessing its another part of your code, so if my answer isnt helpful you may wish to post this class also (or atleast the relavent parts to this code).

    The exception being thrown (the error message) is reasonably clear in meaning:
    The stack trace tells you that your code is breaking at the following line because the first arguement of ImageIO.write is null:
    Java Code:
    ImageIO.write(dit.returnedImage(), "png", new File(System.getProperty("user.home") + "\\SCapture\\.temp\\img"+getPIC_ID()+".png"));
    You can check the API (as linked) to check which arguement "im" is.

    Which leads to the question why is dit.returnedImage() guving a null result.

    My guess is that its because you're making two DrawImageThread objects. You're telling the second to do its thing using the Thread. But the first is simply created and ignored until you call dit.returnedImage() .

    I'd suggest you ment to create one object:
    Java Code:
    Robot robot = new Robot();
    DrawImageThread dit = new DrawImageThread([B]image, getPIC_ID()[/B]);
    			
    BufferedImage image = robot.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
    Thread t = new Thread([B]dit[/B]);
    t.start();
    Thread.sleep(1000);
    ImageIO.write(dit.returnedImage(), "png", new File(System.getProperty("user.home") + "\\SCapture\\.temp\\img"+getPIC_ID()+".png"));
    As a general comment it's not safe to assume that a sleep() will be long enough. For interacting between threads like this you would normally use wait() and notify() instead of sleep.

    Hope this helps
    ----Signature ----
    Please use [CODE] tags and indent correctly. It really helps when reading your code.

  3. #3
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default

    Ok, this is my DrawImageThread class:
    Java Code:
    import java.awt.MouseInfo;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    
    import javax.imageio.ImageIO;
    
    
    public class DrawImageThread implements Runnable{
    	
    	BufferedImage imageToDrawOn;
    	int pictureID;
    	
    	public DrawImageThread(BufferedImage bi, int id){
    		imageToDrawOn = bi;
    		pictureID = id;
    	}
    	
    	public DrawImageThread(){}
    	
    	@Override
    	public void run() {
    		BufferedImage img = null;
    		try {
    			img = ImageIO.read(new File("C:\\Users\\DEIVIDAS\\Desktop\\windows7_arrow.png"));
    			imageToDrawOn.getGraphics().drawImage(img,
    					MouseInfo.getPointerInfo().getLocation().x,
    					MouseInfo.getPointerInfo().getLocation().y,
    					null);
    			
    		} catch (IOException e) {
    			Core.log("Exception #4");
    			e.printStackTrace();
    		}
    		
    	}
    	
    	public BufferedImage returnedImage(){
    		return imageToDrawOn;
    	}
    	
    	
    
    }
    to make it work properly should I make returnedImage static?

  4. #4
    couling is offline Member
    Join Date
    Nov 2010
    Posts
    54
    Rep Power
    0

    Default

    Quote Originally Posted by PhQ View Post
    to make it work properly should I make returnedImage static?
    No, see the code change I suggested and don't create two DrawImageThread objects, you've really got no reason to.

    And you also need to check the result of dit.returnedImage() isn't null before trying to use it.
    ----Signature ----
    Please use [CODE] tags and indent correctly. It really helps when reading your code.

  5. #5
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default

    Quote Originally Posted by couling View Post
    No, see the code change I suggested and don't create two DrawImageThread objects, you've really got no reason to.

    And you also need to check the result of dit.returnedImage() isn't null before trying to use it.
    I have fixed it, thank you.
    Last question, I need my program to shoot 35 pictures per second, but it goes laggy. Is it possible to make it not laggy?

  6. #6
    couling is offline Member
    Join Date
    Nov 2010
    Posts
    54
    Rep Power
    0

    Default

    The code you've given hasn't got a loop and given this is multi-threaded it is not clear where and why your code will be lagging between images. But as a hint:

    Thread.sleep(1000) will make your code sit for 1000ms (1 second) doing nothing. If you make your DrawImageThread write the file as well as draw it, RecordThread.run need never wait at all.

    But I can't see why you've done this in 3 seperate threads. You have your main thread, your RecordThread and your DrawImageThread. Starting and stopping threads is very time consuming. But your RecordThread is doing nothing while your DrawImageThread is running.
    ----Signature ----
    Please use [CODE] tags and indent correctly. It really helps when reading your code.

  7. #7
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default

    Quote Originally Posted by couling View Post
    The code you've given hasn't got a loop and given this is multi-threaded it is not clear where and why your code will be lagging between images. But as a hint:

    Thread.sleep(1000) will make your code sit for 1000ms (1 second) doing nothing. If you make your DrawImageThread write the file as well as draw it, RecordThread.run need never wait at all.

    But I can't see why you've done this in 3 seperate threads. You have your main thread, your RecordThread and your DrawImageThread. Starting and stopping threads is very time consuming. But your RecordThread is doing nothing while your DrawImageThread is running.
    Ok, thanks !

Similar Threads

  1. Applet Main Thread Error
    By pinkdreammsss in forum Java Applets
    Replies: 6
    Last Post: 09-14-2010, 08:12 PM
  2. Replies: 1
    Last Post: 12-05-2009, 08:28 AM
  3. Replies: 4
    Last Post: 06-07-2009, 01:59 AM
  4. Replies: 1
    Last Post: 03-24-2009, 01:52 PM
  5. Replies: 1
    Last Post: 07-25-2007, 11:55 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
  •