Results 1 to 7 of 7
Thread: Thread Error
- 11-30-2010, 08:19 PM #1
Thread Error
Something is causing this error and I don't know why:
here is RecordThread class: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)
How can I fix this error?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()); } }
- 11-30-2010, 10:47 PM #2
Member
- Join Date
- Nov 2010
- Posts
- 54
- Rep Power
- 0
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:You can check the API (as linked) to check which arguement "im" is.Java Code:ImageIO.write(dit.returnedImage(), "png", new File(System.getProperty("user.home") + "\\SCapture\\.temp\\img"+getPIC_ID()+".png"));
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: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.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"));
Hope this helps----Signature ----
Please use [CODE] tags and indent correctly. It really helps when reading your code.
- 11-30-2010, 11:03 PM #3
Ok, this is my DrawImageThread class:
to make it work properly should I make returnedImage static?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; } }
- 11-30-2010, 11:13 PM #4
Member
- Join Date
- Nov 2010
- Posts
- 54
- Rep Power
- 0
----Signature ----
Please use [CODE] tags and indent correctly. It really helps when reading your code.
- 11-30-2010, 11:17 PM #5
- 12-01-2010, 08:18 AM #6
Member
- Join Date
- Nov 2010
- Posts
- 54
- Rep Power
- 0
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.
- 12-01-2010, 12:03 PM #7
Similar Threads
-
Applet Main Thread Error
By pinkdreammsss in forum Java AppletsReplies: 6Last Post: 09-14-2010, 07:12 PM -
Thread: Error 500--Internal Server Error java.lang.NullPointerException
By jackdear44 in forum New To JavaReplies: 1Last Post: 12-05-2009, 07:28 AM -
[newbie] Exception in thread "AWT-EventQueue-0" java.lang.Error
By jon80 in forum New To JavaReplies: 4Last Post: 06-07-2009, 12:59 AM -
getting following error org.eclipse.swt.SWTException: Invalid thread access
By Madhavee Latha in forum SWT / JFaceReplies: 1Last Post: 03-24-2009, 12:52 PM -
Error: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
By romina in forum New To JavaReplies: 1Last Post: 07-25-2007, 10:55 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks