|
It throws a NullPointerException I think it is because , the application may not have an access on hard drives, but it works if I search in folders which are located in those drives. Can you try it please?
|
|
|
|
|
...you know, you need to mask the double-slash ? it's not interpreted right otherwise. the Backslash is in Java a sign for masking and special String-values like the tab "\t". To set a "\" you always need to use 2 "\\".
It's a good solution to set up a String and use this as a argument for the following commands:
FileSearchRecursive x=new FileSearchRecursive();
String strSearchFolder = "C:\\\\someFolder";
String strSearchFile = "try.txt"´;
System.out.println("Searching in folder:\t" + strSearchFolder + " for file:\t" + strSearchFile);
if(false == (x.searchForFile(strSearchFolder,strSearchFile ))){
System.out.println("File not found");
}
regards
Torsten
I never finish anyth...
|
|
|
|
|
TorstenH. wrote: ...you know, you need to mask the double-slash ?
And do you know that the OP code doesn't need to do that correct?
The code has "C:\\" which is exactly what it should have to access the C drive root.
TorstenH. wrote: String strSearchFolder = "C:\\\\someFolder";
Which resolves to "C:\\someFolder" which although it will work on windows is not any better or more correct than "C:\someFolder".
|
|
|
|
|
CoderForEver wrote: It throws a NullPointerException
The stack trace tells you where. So which line is it?
CoderForEver wrote: the application may not have an access on hard
Application? You are running exactly the code you posted from a console window correct?
Thus the only way it wouldn't have access is if you you, the user, doesn't have permission to access the C drive root. And if that was the case then you should see an exception that indicates exactly that - a permission problem.
|
|
|
|
|
Judging by your comments and code extract I would suggest the following line be changed as indicated:
if(!(x.searchForFile("C:\\someFolder","try.txt")))
if(!(x.searchForFile("C:\\someFolder\\","try.txt")))
The best things in life are not things.
|
|
|
|
|
This would show the original problem if C:\\someFolder was empty.
Panic, Chaos, Destruction.
My work here is done.
or "Drink. Get drunk. Fall over." - P O'H
OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre
Have a bit more patience with newbies. Of course some of them act dumb -- they're often *students*, for heaven's sake. -- (Terry Pratchett, alt.fan.pratchett)
|
|
|
|
|
Exactly how I found the problem.
The best things in life are not things.
|
|
|
|
|
Interesting. When I run it on an empty directory, I get a zero-length array.
Also, it doesn't make any difference if I put "C:\\someFolder" or "C:\\someFolder\\". It behaves the same in both cases.
I only get null back if there is some kind of problem (e.g. Access denied).
|
|
|
|
|
I'll try it again sometime and check the results more closely.
The best things in life are not things.
|
|
|
|
|
Come on guys. My question is how can I find a file in "C", not in "C:\someFolder"? ... on the latter case it works easily. If you have time try to run my code, it really works. But try to make the searching folder "C:\" not "C:\someFolder". Then it will through an exception. Though thank you for your help
|
|
|
|
|
Consider these lines:
File[] list=file1.listFiles();
for(File x: list)
if file1.listfiles() does not return any items then list will be null and the runtime will throw an exception. Took me less than 5 minutes to find it with the debugger.
Lesson: Check return values and statuses, and catch exceptions: there is no alternative.
The best things in life are not things.
|
|
|
|
|
Hi,
some comments:
1.
you should implement error handling in your code; have a try-catch and log the exception.
2.
when an exception occurs, it gives the number of the offending line; that is where the problem shows itself, the actual cause may be somewhat sooner.
3.
when the exception occurs in a method call, check the documentation for that method to see what it has to say about the specific exception. That should tell you exactly what is wrong.
4.
Your searchForFile() method is a bit silly, it tells whether a file exists, but when it does, it does not tell you where it is. A better definition of it would return the path to the first occurence it finds as a string.
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
What is the result of running the following? If it is exception then post the entire stack trace.
File file1=new File("C:\\");
System.out.println("exists=" + file1.exists());
|
|
|
|
|
This works for me.
As someone else has pointed out, file1.listFiles() may return null so you need to allow for that.
|
|
|
|
|
I am reduced to "someone else".
The best things in life are not things.
|
|
|
|
|
Hi all,
I am having one small doubt. If string is a immutable class then how come it is allow us change ? ex: String s1="abc"; s1="def"; Sop(s1); it gives op as :- def. does it mean we have made changes on string object ? please tell me.
Thanks
************ S G KORE *******************
|
|
|
|
|
"abc" and "def" are strings.
s1 is not a string, it is a variable holding a reference to a string. at first it refers to "abc", later on it refers to "def".
The string "abc" never got modified, it may still be alive (kept alive by another reference, as in the example below), or it is dead and garbage collectible.
string s1="abc";
string s2=s1;
s1="def";
Sop(s1);
Sop(s2);
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
Thank you Luc Pattyn. Now I got clear picture .
************ S G KORE *******************
|
|
|
|
|
In addition to the above comment, what I suggest you is that try to draw a picture in a paper while you work on Object types & references so that you will always know what you are doing and have the right picture in your mind. It is very helpful, even in Arrays and Memory Allocations
|
|
|
|
|
I want to develope a desktop application in java which has TCP client on desktop.It connect to the server.And whenever TCP server sends message to this client,an alert(like balloon tip in windows) should appeared on my desktop where my application is running.This alert is timed alert.It should disappear after 3 seconds.
I am new to java so any help will be appreciated.
|
|
|
|
|
1. Learn basics of java
2. Learn GUI programming
3. Learn threads, including but not limited to timers.
4. Learn sockets programming
5. Create server
6. Create client.
|
|
|
|
|
Good answer.
The best things in life are not things.
|
|
|
|
|
yes, a fine, and precise answer.
But I'd like to give him a hint too: How to Make Dialogs @ oracle.com[^]. Combine this with the threads[^] that jschell talked about and you'll have your "Desktop notification".
...because we all started somehow & somewhere too.
regards
Torsten
I never finish anyth...
|
|
|
|
|
Hello guys,
I have to write an in-place version of Merge-Sort which works on doubly linked lists for university.
This wouldn't be the problem, but there are a few conditions to meet.
- the algo must work in-place (additional memory usage in O(log n))
- I cannot create new list elements or entire lists, just copies (ListElement left = new List Element() isn't possible but ListElement left = first is possible)
- the algo should be recursive, but this is not necessary
- each call of the algo must return a head pointer to the first element of the sub-list
- the merge part must also return a head pointer
public DoublyLinkedList mergesort(DoublyLinkedList in, int numOfElements) {
if (numOfElements > 1) {
DoublyLinkedList firstLeft = in;
DoublyLinkedList firstRight = in;
for ( int i = 0; i < numOfElements / 2; i++ ) {
firstRight.first = firstRight.first.next;
}
DoublyLinkedList left = mergesort ( firstLeft, (int)Math.floor(numOfElements / 2 ) );
DoublyLinkedList right = mergesort ( firstRight, (int)Math.ceil(numOfElements / 2 ) );
return merge ( left, right );
} else {
return in;
}
}
My problem is that I'm somehow stuck right at the beginning. The algo itself shouldn't be the problem, but the additional conditions make the whole thing quite difficult for me.
Of course I've already searched for useful algos or something, but I didn't find anything.
Thanks in advance for your help and best wishes,
Manfred
|
|
|
|
|
Coincidentally, I have to do the exact same thing. I believe recursion might be the most efficient, good thing you've already started it that way. The code you have looks simple/efficient. HOWEVER, it looks like you followed the algorithm given to you in the "Skriptum".
Through some searching without using the keyword "pseudocode", I came across this very descriptive page which I have yet to take the time to fully comprehend (http://www.chiark.greenend.org.uk/~sgtatham/algorithms/listsort.html)
|
|
|
|