Getting error with ImageIO.read(getClass().getResourceAsStream (input==NULL)? - java

I keep getting this error when I use the following code:
try
{
image=ImageIO.read(getClass().getResourceAsStream("build/classes/javaproject/Space.gif"));
}
catch (IOException ex)
{
lastException=ex;
}
It gives keeps giving me the "input==null" error.
I have already tried to find a solution to this (several answered questions already had the solution actually, but those didn't work for me) but I haven't had any luck. This one for example was very similar to my problem, but I can't seem to find out exactly where to put my image file in.
I noticed most of them were solved by simple file placement, and so it's all the more confusing when some people have 'bin' while I have 'build' and 'src' on NetBeans.
Also, I realize the path is probably erroneous, but I already tried it many different ways and it hasn't seemed to work with any of them. I would really appreciate any help...

I am assuming, from the name, that build/classes/ is a folder on your classpath; so what you probably need to write is:
image=ImageIO.read(getClass().getResourceAsStream("/javaproject/Space.gif"));
Edit for comment below: Since javaproject/ is not actually inside build/classes/, I guess you actually need:
image=ImageIO.read(getClass().getResourceAsStream("/Space.gif"));
(I know the context here is a bit different, but it should be clear that this system is modeled somewhat on a filesystem. If your build/classes/ directory doesn't contain a javaproject/ directory, then why would it ever occur to you to write build/classes/javaproject/?)

The promblom is that its A system file so you got too do this:
ClassLoader.getSystemResourceAsStream

Related

While debugging in eclipse, eclipse hides some variables

I am debugging my program in eclipse and facing some strange problem. I have a list fileList1, which is a list of objects files, which has 2 properties:
fileName
versionList
Please have a look on this picture, you will understand my problem.
While debugging, I noticed that when I add object files in fileList1 using a for loop, it works fine and the object files is added. But when I extract the FileList1, it doesn't shows all nodes information, for example, here you can see:
When I extracted FileList1[0], only filename is shown. The next filed, which should show versionList is blank.
Again, When I extracted FileList1[2],nothing is shown, neither filename or versionList.
If I mistakenly doesn't add anything in a particular file, then atleast the variable name , like fileName and versionList should be shown with null properties. But its not the case here. Also, sometimes, the fileName variable in debugger variables view shown one value and if I click on it to extract, it shows another value.
I think the problem is with debugger. Its not updating properly. My output is also coming weird. It seems new entries are replacing old entries, although I did not used any replace function for list, just used fileList1.add(file). I am not understanding, whats the problem here.
I already checked internet and stackoverflow for a solution. But did not found similar topic. Your help will be highly appreciated.

java file.renameTo() does rename file but returns false. Why?

The problem is that I need the file to move before the rest of my logic will work so when the method returns false I stop execution.
However, when I check on the file in windows explorer it has a new name and it moved.
Just curious why this is happening.
here is some sample code I just tried to recreate the issue. It's pretty much the same thing and it's working fine.
File testfile = new File("TestFile");
if(!testfile.exists()){
testfile.mkdirs();
}
File sample = new File("sample.txt");
if(sample.exists()){
boolean success = sample.renameTo(new File(testfile.getPath() + "\\" + sample.getName()));
if(success){
System.out.println("Moved");
}
else{
System.out.println("Failed");
}
}
Edit: Solved it. I'm sorry for wasting everyone's time with something so silly. However, I really dont think I would have tracked this down if not for making this post.
The solution was that I was actually looping through several files to move. When the output said it failed then the program stopped and when I looked in explorer only the first of the files was actually moved so I assumed it was moving and then returning false. However, the issue was that I was using the wrong variable as an index and so what was happeneing was that it did successfully move the file in index 0 and then when the loop repeated the index didnt increment so it tried to move index 0 again and therefore failed.
Like I said, very stupid but thanks for bearing with me.
Java's File.renameTo() is problematic, especially on Windows, it seems. As the API documentation says:
Many aspects of the behavior of this method are inherently
platform-dependent: The rename operation might not be able to move a
file from one filesystem to another, it might not be atomic, and it
might not succeed if a file with the destination abstract pathname
already exists. The return value should always be checked to make sure
that the rename operation was successful.
You can use apache.commons.io library, which includes FileUtils.moveFile() or also the Files.move() method in JDK 7.
Isn't it possible that you file has a Inputstream open somewhere but has not been closed and so the rename is not working. Try closing all open streams relevant to the file object before closing.
This one worked for me
File file = new File("E:/Javadocs/" , "new.txt");
File file1 = new File("E:/Javadocs/" , "myDoc.txt");
file1.createNewFile();
if (file1.exists()){
System.out.println(file1.renameTo(file));
}
This will create a file myDoc.txt and rename it to new.txt and will print true
I've also tried with File(URI) constructor it worked fine

Only some users reporting "Resource Not Found" error. Does this make sense?

I am seeing a a couple of errors coming up on Crittercism (Crash reporting service) for my published Android app. The trace is the following:
0 java.io.IOException: Resource not found: "org/joda/time/tz/data/ZoneInfoMap" ClassLoader: dalvik.system.PathClassLoader#45908320
1 at org.joda.time.tz.ZoneInfoProvider.openResource(ZoneInfoProvider.java:211)
2 at org.joda.time.tz.ZoneInfoProvider.<init>(ZoneInfoProvider.java:123)
3 at org.joda.time.tz.ZoneInfoProvider.<init>(ZoneInfoProvider.java:82)
4 at org.joda.time.DateTimeZone.getDefaultProvider(DateTimeZone.java:462)
5 at org.joda.time.DateTimeZone.setProvider0(DateTimeZone.java:416)
6 at org.joda.time.DateTimeZone.<clinit>(DateTimeZone.java:115)
7 at org.joda.time.chrono.GregorianChronology.<clinit>(GregorianChronology.java:71)
8 at org.joda.time.chrono.ISOChronology.<clinit>(ISOChronology.java:66)
9 at org.joda.time.base.BaseDateTime.<init>(BaseDateTime.java:61)
10 at org.joda.time.DateTime.<init>(DateTime.java:155)
Searching shows this this is usually a compiling problem (Usually that the Joda Time Library was not added to the build path or something), but then why would only about 4 users out of a couple thousand see this error?
My only guess is that someone is trying to decompile the app to pirate it (Its a fairly popular paid app), and sees this error when they incorrectly re-compiled. In that case I am glad they are seeing errors and I dont need to worry about this.
The other weird thing is that the code causing the problem was surrounded by a try/catch, which didnt seem to catch it:
try {
DateTime dt = new DateTime();
DateTimeFormatter fmt = ISODateTimeFormat.dateTime();
return fmt.print(dt);
} catch (Exception e) {
//Report issue to Analytics service
}
Which to me, makes it seem even more unlikely that this error would show up on Crittercism, since it should've been caught. Can anyone explain this?
This can happen in the case you miss initialization:
JodaTimeAndroid.init(this);
Not crash but in Unit test, I see the same error message
java.io.IOException: Resource not found: "org/joda/time/tz/data/ZoneInfoMap"
when call to DateTimeFormatter class with joda-time-android
Add joda-time java dependency to test source set will fix the error in Unit test
testImplementation "joda-time:joda-time:$latestVersion"
I see similar, infrequent reports from a popular free app. I haven't reproduced it so far. There are various references to this problem sprinkled around the web, not all involving Android.
One clue: the ZoneInfoMap is a resource file in the original jar, not a class. So if any of the various app markets was stripping out non-class information (e.g. unjar then re-jar), ZoneInfoMap would be a candidate. Likewise, if any classloader utility had an issue, for security or stupidity, with non-class resources, ZoneInfoMap would be a candidate.
Another angle: could the problematic runtime environment have its own joda-time jar earlier in the classpath, one that lacks a /data/ segment?
Bottom line: seems like this is another example of the Wild West of Android, one that I've learned to ignore until I can find a repro.
I have had the same problem and after a good deal of digging searching on google I figured it out. The ZoneInfoMap file is created by running jodaTime (ZoneInfoCompiler) with the arguments:
-dst src\org\joda\time\tz\data src\org\joda\time\tz\src\africa
src\org\joda\time\tz\src\antarctica src\org\joda\time\tz\src\asia
src\org\joda\time\tz\src\australasia src\org\joda\time\tz\src\backward
src\org\joda\time\tz\src\etcetera src\org\joda\time\tz\src\europe
src\org\joda\time\tz\src\northamerica src\org\joda\time\tz\src\pacificnew
src\org\joda\time\tz\src\southamerica src\org\joda\time\tz\src\systemv
This will add all the timezone to the jodaTimer, you can create more time Zone areas I believe if you know how as well, and you do not have to include all of them if you don't need all of them.
why it crashes with that error is because it is looking for a file that is not there. Not sure exactly why try catch is not working but I suspect it has something to do with the fact that the jodaTimer is a JAR library (mine was at least)?
Not sure if this is how it is meant to be done or if there is a better solution but that's what i did and it works for me perfectly now.
references that helped m come to this conclusion:
http://joda-interest.219941.n2.nabble.com/How-to-run-Joda-time-s-test-Resource-Not-Found-exception-td5913668.html
http://sourceforge.net/p/joda-time/discussion/337835/thread/2798a578/
I hope this helps.

Source not found exception

In my application i have a JSP file named "Transferdatanew.jsp". When I am going to use my application, I am getting an exception as numberformatexception, but when I clicked there I am getting a message as "source not found for org.apache.jsp._Transferdatanew".
What does this mean? Please help me. I am new to Java.
Its pretty clear that there is a problem in the 'Transferdatanew.jsp'. I'd venture so far as to say that the jsp is receiving an input that is not in the numerical format it expects.
You should not be worried about 'Source not found exception' unless you want to see the source .... which will be in one of the $TOMCAT_HOME/work subfolders.

Don't understand why I am getting this - 'Error: Value storage corrupted: negative offset'

I just started using IntelliJ-IDEA, and I don't know if the problem resides with the IDE or not. I'm assuming not but I have no idea. I've spent quite a lot of time googling this error to no avail.
The error comes about at compile time. The weird thing is that I returned my code to the way it was before the error was showing up and it is still being thrown. Here is a little bit of my code that I believe is causing the problem. But I think it might be something besides my code.
import org.joda.xpath.XPath;
private XPath componentXPath;
private List list;
this.componentXPath = XPath.newInstance("(//rss/channel/item)");
Document doc = jiraAdapter.fetch("path to XML file");
list = componentXPath.selectNodes(doc);
The componentXPath.selctNodes(doc) is somehow causing the problem. If i remove this line, then it compiles fine, but I need to get a list of all the nodes to work with.
The error shows up in the messages pane: Error: Value storage corrupted: negative offset
Any help would be greatly appreciated.
What IDEA version do you use? Try File | Invalidate Caches and then rebuild the project.

Categories

Resources