Is there a way to avoid this error java.lang.NullPointerException
in array?
I create an array with allocated size. But sometimes it doesn't fill up. And I get the error. How to avoid it?
The code:
public static produktas[] surasti (produktas G[], int n){
try{
int x = 0;
BufferedReader in = new BufferedReader (new InputStreamReader (System.in));
produktas A[] = new produktas[5]; // size of array, but sometimes it may be 1 element, or two, or etc.
for (int j=0; j<5; j++){
System.out.println("Kokio produkto ieskosime?");
String found = in.readLine();
for (int i=0; i<n; i++){
if (found.equals(G[i].gautiPav())){
A[x] = G[i];
x = x + 1;
}
}
}
return A;
} catch(IOException ie){
ie.printStackTrace();
}
return null;
}
And I'm asking about how to make dynamical array or smth, so it's not related to that topic.