Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Return array..urgent

 
yogi ta dev
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
yogi ta dev
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks,
I got the solution.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic