Can't i initialized empty String array and pass in the value afterwards in Java?
I can't seems to find out what what is wrong with my codes? i can't开发者_开发百科 pass in non-empty String value into an empty String array?
String[] profiles = wifiPositioningServices.GetAllProfileData();
ArrayList<String[]> macAddresses = new ArrayList<String[]>();
String[] macAddressTemp=null;
//store all profile data into a ArrayList of Profile objects
for(int i=0; i<profiles.length; i++){
String[] result = profiles[i].split(",");
Profile profile = new Profile();
profile.setProfileName(result[0]);
profile.setOwner(result[1]);
profile.setMap(result[2]);
profile.setVisible(result[3]);
boolean visible = Boolean.valueOf(result[3]);
if(visible){
//if visible is true, then add profile name and mac filters into arraylist
profileNames.add(result[0]);
int cnt=0;
for(int j=4; j<result.length; j++){
profile.setMacFiltersList(result[j]);
Log.e("Text:", result[j]);
macAddressTemp[cnt] = result[j];
++cnt;
}
macAddresses.add(macAddressTemp);
}
profileList.add(profile);
}
Java show a nullpointer exception at the line "macAddressTemp[cnt] = result[j];". I am sure that result[j] is not empty cause i was able to print it out via the log msg.
Use an ArrayList instead.
ArrayList<String> macAddressTemp = new ArrayList<String>(100); //check capacity
macAddressTemp.add(result[j]);
EDIT:
You changed your code wrong.
This ArrayList<String[]> macAddresses = new ArrayList<String[]>();
is creating a List that contains arrays. You have to create a List that contains String. Check the code above.
The array macAddressTemp
is null.
The NPE is thrown because macAddressTemp
is null
. You probably want to declare and initialize macAddressTemp
inside of the outer loop.
You explicitly set it to null:
String[] macAddressTemp=null;
You need to allocate some space for the array, or use a collection.
精彩评论