aspose file tools*
The moose likes Beginning Java and the fly likes Return array..urgent Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Return array..urgent" Watch "Return array..urgent" New topic
Author

Return array..urgent

yogi ta dev
Greenhorn

Joined: Nov 25, 2004
Posts: 9
Hi,
This is the piece of code.I am getting Null pointer exception when I return cf.When I tried to print the length of cf ,the value returned was 30 where as there are not so many elements stored.Please help..

public File[] f1= new File[30];
public File[] cf=new File[30];
int count=0;
for(int j=0;j<f1.length;j++)
{
cf[count]=f1[j];
count++;
}
return cf;
Thanks..
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
The posted code example is incomplete and leaves much for the imagination of those that might try to help you. I recommend posting complete code examples. I also recommend posting complete error messages.

Nothing in the code you've posted would cause a NullPointerException.

In plain english, could you describe what the posted code is supposed to do?


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
As Dirk noted, there is nothing in the code you posted that would cause a NullPointerException. You should post the exact error message, along with the code that it indicates causes the problem, in order for us to help you. However, I *do* see some possibilities that can cause your problem. As a wild guess, perhaps you are doing something like this with the code you gave us:

In fact, trying to do anything with any element of the array will cause a NullPointerException in fact. This is because none of the elements have been initialized. "But wait," you might say, "I initialized them in the for loop in someFunc()!" Unfortunately, this is not the case. The for loop copies the uninitialized references from one array to another. In otherwords, the elements of f1 are not initialized when the loop starts.

Unfortunately, your original post does not have enough information to suggest a solution to this problem. You need to find some way to initialize the elements of the array f1 before you try to copy the references to another array.

As a side note, f1.length and cf.length are both 30 since you used this as the size when you created these arrays. However, this doesn't mean there are actually 30 objects in the array. As you have seen, the elements are initially all null. You have to initialize each element in order to fill the array with the 30 elements.

Also notice that I modified your for loop slightly. There was no need for the count variable as j was already doing the exact same counting in the for loop.

Anyway, I hope this helps some. If not, please post more of your code and the exact error message that you get. (Copy and Paste is your friend!)

Keep Coding!

Layne


Java API Documentation
The Java Tutorial
yogi ta dev
Greenhorn

Joined: Nov 25, 2004
Posts: 9
thanks,
I got the solution.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Return array..urgent