X-Received: by 2002:a37:634c:: with SMTP id x73mr1039672qkb.17.1551390573892; Thu, 28 Feb 2019 13:49:33 -0800 (PST) X-Received: by 2002:a0d:e901:: with SMTP id s1mr954843ywe.97.1551390573667; Thu, 28 Feb 2019 13:49:33 -0800 (PST) Path: csiph.com!weretis.net!feeder6.news.weretis.net!feeder.usenetexpress.com!feeder-in1.iad1.usenetexpress.com!border1.nntp.dca1.giganews.com!nntp.giganews.com!m21no199235qta.0!news-out.google.com!o7ni57qta.1!nntp.google.com!m21no199225qta.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.java.programmer Date: Thu, 28 Feb 2019 13:49:33 -0800 (PST) In-Reply-To: <8617d379-1659-4582-94fe-6ffb95c975e4@googlegroups.com> Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=84.74.101.34; posting-account=UjEXBwoAAAAOk5fiB8WdHvZddFg9nJ9r NNTP-Posting-Host: 84.74.101.34 References: <8617d379-1659-4582-94fe-6ffb95c975e4@googlegroups.com> User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: <3167672b-6941-4802-acd7-3b7defe06348@googlegroups.com> Subject: Re: it's Closeable, but I don't want to close() it yet. From: bursejan@gmail.com Injection-Date: Thu, 28 Feb 2019 21:49:33 +0000 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Lines: 52 Xref: csiph.com comp.lang.java.programmer:38765 Hint: Don't use MyClass.class.getResourceAsStream. Use getResource which gives you an URL, and provide this to ImageIcon. Swing will create a background worker, and load the ImageIcon for you. getResource doesn't throw any error, but it might return null: URL url =3D clazz.getResource("relative path to icon"); if (url!=3Dnull) { ImageIcon icon=3Dnew ImageIcon(url); /* do something with the icon, it should already be loaded */ } else { /* your setup is wrong or programming error wrong class or wrong relative path */ } On Thursday, February 28, 2019 at 9:11:41 PM UTC+1, Eric Douglas wrote: > On Wednesday, February 27, 2019 at 10:52:01 AM UTC-5, Andreas Leitgeb wro= te: > > In my application there exists an entity that is Closeable. > > It is kept in some class, and other parts of my application > > request a ref to the entity and do actions on it, then drop > > their ref, leaving the entity intact. > >=20 > > Everything runs fine, except eclipse warns me about spots > > where the entity is requested, used, and then dropped. > > Eclipse thinks it might need to be close()d. > >=20 > > Apart from ignoring the warning per eclipse settings or > > adding @SuppressWarnings, is there maybe a way to tell > > the compiler that a certain ref is not meant to be close()d? > > Letting it know that - despite the entity's ultimate fate of > > being eventually closed - this is not yet the time&place for it? >=20 > Here's where it gets ugly. I've had the "Potential resource leak" error = just turned off, now I'm trying to turn it on and change code to make it go= away. I have a class with class level final public static ImageIcon varia= bles. I initialize each ImageIcon in a static block which calls MyClass.cl= ass.getResourceAsStream() to open an InputStream to an image file which is = packaged into the jar (so if this ever gets an error you've got serious pro= blems). I then pass that InputStream into another method in a utility clas= s which uses ImageIO to generate the ImageIcon. That method closes the str= eam. Eclipse complains the stream may be leaked in this class. I take the= close out of that class and try to change this to use the try-with-resourc= es. I previously needed no try statement in this static block. When I wro= te a try-with-resources for a SQL Statement it didn't ask for any exception= clause, but here it's telling me I need to catch IOException. Now of cour= se my assignment of the ImageIcon must be inside this try block, and if I a= dd a catch clause it's complaining it may not be initialized, so I have to = initialize it to null in the constructor and remove the final.