List<String>- Null - android

I am trying to iterate List from JSON. And everything works fine when List is not null. When is null, i ve got this: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
StringBuilder allAlsoAvailable = new StringBuilder();
List<String> alsoAvailableList = example.getResult().getAlsoAvailableOn();
for (int i = 0; i < alsoAvailableList.size(); i++) {
allAlsoAvailable.append(alsoAvailableList.get(i));
if (i < alsoAvailableList.size() - 1) allAlsoAvailable.append("\n");
}
and LogCat shows that the problem is in this line: for (int i = 0; i < alsoAvailableList.size(); i++)

This is because, as the error states, your alsoAvailableList is null and you are trying to access it's method size().
You should first check if the list is null and then iterate through it.
For example:
Builder allAlsoAvailable = new StringBuilder();
List<String> alsoAvailableList = example.getResult().getAlsoAvailableOn();
if (alsoAvailableList != null) {
for (int i = 0; i < alsoAvailableList.size(); i++) {
allAlsoAvailable.append(alsoAvailableList.get(i));
if (i < alsoAvailableList.size() - 1) allAlsoAvailable.append("\n");
}
}

Builder allAlsoAvailable = new StringBuilder();
List<String> alsoAvailableList = example.getResult().getAlsoAvailableOn();
if (alsoAvailableList != null) {
for (int i = 0; i < alsoAvailableList.size(); i++) {
allAlsoAvailable.append(alsoAvailableList.get(i));
if (i < alsoAvailableList.size() - 1) allAlsoAvailable.append("\n");
}
}
As your error explains, you can't call methods on a null object, in this case your alsoAvailableList is null, so make sure it's not null first when trying to use it.

Related

Derefrencing of 'binding' produces NullPointer Exception

Declare an array of Croller(https://android-arsenal.com/details/1/5079) -
Croller[] croller = new Croller[]{binding.crollerThirtyOnetwentyHertz,binding.crollerOnetwentyFoursixtyHertz,binding.crollerFoursixtyOnekiloHertz,
binding.crollerOnekiloSevenkiloHertz,binding.crollerSevenkiloHertz};
Use it: On setting the label of Croller using data binding shows Null Pointer Exception
for (int i = 0; i < numBands; i++)
{
freqRange = eq.getBandFreqRange((short)i);
croller[i].setLabel ("Anything Printed Here"));
}

Add custom object in Android

I am new to Android. I am trying to add a Custom object in a list. Below is my code.
GridItem items[];
if (motorList.length > 0){
for (int item:motorList) {
GridItem aItem = new GridItem(item,"no_image");
items.add(aItem);
}
}
How to achieve this?
There are a two big problems with your code:
You didn't initialize items but you are trying to use it
You can't call .add(...) on an array
-> You can either initialize an array with the size of motorlist and add the items via the index:
if (motorlist != null && motorlist.size() > 0) {
GridItem[] items = new GridItem[motorlist.size()];
for (int i = 0; i < motorlist.size(); i++) {
items[i] = new GridItem(motorlist.get(i), "no_image");
}
}
Or you could create a List instead of an array:
if (motorlist != null && motorlist.size() > 0) {
List<GridItem> items = new ArrayList<>();
for (int item : motrolist) {
GridItem aItem = new GridItem(item,"no_image");
items.add(aItem);
}
}
I'd recommend the second option.
Please note that both options assume that motorlist is a List

Adding a value to string array in android

i am trying enter link description here
as to add values to single as well as 2dim array dynamically,
but while adding values it shows null pointer ,
here is my code
Arr points1[];
points1 = new Arr[listItemList.size()];
for(int i=0;i<listItemList.size();i++)
{
ListItemReminderSummary listItem = listItemList.get(i);
Log.i("listItem.Car_Id", listItem.Car_Type);
points1[i].Car_Id = listItem.Car_Id;
points1[i].Car_Type = listItem.Car_Type;
}
for(int i=0;i<listItemList.size();i++)
{
System.out.println( points1[i].Car_Id + points1[i].Car_Type);
}
Null pointer at points1[i].Car_Id = listItem.Car_Id;
any suggestion,
thnx in advance.
initialize the items in Array...
for (int i = 0; i < listItemList.size(); i++) {
ListItemReminderSummary listItem = listItemList.get(i);
Log.i("listItem.Car_Id", listItem.Car_Type);
points[i] = new Arr();
points1[i].Car_Id = listItem.Car_Id;
points1[i].Car_Type = listItem.Car_Type;
}
You have to initialize cells of array.
for(int i=0;i<listItemList.size();i++){
points[i] = new Arr();
}
You have not Allocated memory to the Arr that is why you're trying to dereference an uninitialised pointer (i.e. writing to a random chunk of memory), which is undefined behaviour.
Change ur starting 2 lines
Arr points1[] = new Arr[listItemList.size()];
for (int i = 0; i < listItemList.size(); i++)
{
ListItemReminderSummary listItem = listItemList.get(i);
Log.i("listItem.Car_Id", listItem.Car_Type);
points1[i].Car_Id = listItem.Car_Id;
points1[i].Car_Type = listItem.Car_Type;
}
Did you make sure that listItemList.get(i) returns a value? Perhaps there is nothing returned from this.

Android shuffle Questions and Answers (string arrays)

I am making an app in which there are list of questions and respective answers.
Questions are in one string array, while answers are in another string array.
I have implemented the following in a wish to shuffle the questions. (Of course the answers need to be linked to that question, else meaningless)
Code:
selected_Q = new String[totalnoofQ];
selected_A = new String[totalnoofQ];
int[] random_code = new int[totalnoofQ];
for (int i = 0; i < totalnoofQ; i++)
{
random_code[i] = i;
}
Collections.shuffle(Arrays.asList(random_code));
for (int j = 0; j < totalnoofQ; j++)
{
int k = random_code[j];
selected_Q [j] = databank_Q [k];
selected_A[j] = databank_A [k];
}
The code reports no fatal error, but the selected_Q is still in sequential order. Why?
Could you please show me how can I amend the codes? Thanks!!!
You shuffle a list created using random_code, but random_code is not modified.
You need to create a temporary list based on random_code. Shuffle this list and then use it to fill the selected_X arrays.
Something like this should work :
int[] random_code = new int[totalnoofQ];
for (int i = 0 ; i < totalnoofQ ; i++) {
random_code[i] = i;
}
List<Integer> random_code_list = new ArrayList<Integer>(); // Create an arraylist (arraylist is used here because it has indexes)
for (int idx = 0 ; idx < random_code.length ; idx++) {
random_code_list.add(random_code[idx]); // Fill it
}
Collections.shuffle(random_code_list); // Shuffle it
for (int j = 0 ; j < totalnoofQ ; j++) {
int k = random_code_list.get(j); // Get the value
selected_Q[j] = databank_Q[k];
selected_A[j] = databank_A[k];
}

Android 4.0 ice cream sandwich Parser Errors

I have made a application which reads information from an Api.
Link : http://api.amp.active.com/camping/campground/details?contractCode=CO&parkId=50032&api_key=2chxq68efd4azrpygt5hh2qu
Following is my code :
NodeList list = element.getElementsByTagName("detailDescription");
Log.i("ZealDeveloper","I M In detail "+list.getLength());
if(list != null && list.getLength() > 0){
for(int i = 0; i < list.getLength(); i++){
entry = (Element) list.item(i);
description = entry.getAttribute("description");
drivingDirection = entry.getAttribute("drivingDirection");
latitude=entry.getAttribute("latitude");
longitude=entry.getAttribute("longitude");
}
}
NodeList list1 = element.getElementsByTagName("amenity");
Log.i("ZealDeveloper","I M In 2 "+list1.getLength());
if(list1 != null && list1.getLength() > 0){
for(int i = 0; i < list1.getLength(); i++){
entry = (Element) list1.item(i);
nameAmenity = entry.getAttribute("name");
listAmenity.add(nameAmenity);
}
arrAmenity = listAmenity.toArray(new String[listAmenity.size()]);
StringBuilder builder = new StringBuilder();
for ( int i = 0; i < arrAmenity.length; i++ ){
builder.append(arrAmenity[i]+"\n");
}
txtAmenity.setText(builder);
}
#
I am Getting list.getLength() as 0(was getting 1 in pervious versions of android) , so the parser this condition. For amenity tag i m getting the desired list size.
The only reason I can think of is that "detailDescription" is already root of the document so probably element is the tag you are seeking for. It doesn't have any children with the name "detailDescription" so getElementsByTagName("detailDescription") returns empty list. So change first half of your code as follows:
Log.i("ZealDeveloper","I M In detail " + element.getTagName());
if(element.getTagName().equalsIgnoreCase("detailDescription")) {
description = element.getAttribute("description");
drivingDirection = element.getAttribute("drivingDirection");
latitude = element.getAttribute("latitude");
longitude = element.getAttribute("longitude");
}
/* rest of your code...*/
I had the same issue. I switch from DOM to XPath. More here

Categories

Resources