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"));
}
Related
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.
Following is a part of my json array
{
"format_file_name": [
"/client/compliance_format/payment_of_bonus_act_1965-ce32ec0ee2b94f819ffd2ffdb95ba439.pdf"
]
}
This is the part of my code while Im parsing it,
JSONArray format_file_name = innerJobj.getJSONArray("format_file_name");
if (format_file_name != null) {
for (int k = 0; k < format_file_name.length(); k++) {
JSONObject jsonObject1 = format_file_name.getJSONObject(i);
Iterator<String> keys = jsonObject1.keys();
while (keys.hasNext()) {
file = jsonObject1.getString(keys.next());
}
}
}
The value of format_file_name might be null some times.
In my code, i have already checked it it is null and in case if it is not null then Im parsing it and assigning it to a string called file
The problem is,
When Im trying to parse the json array of null value, Im getting Value null at format_file_name of type org.json.JSONObject$1 cannot be converted to JSONObject. How can I parse it only when its value is not equal to null?
Why my code doesnt work though I have checked for the condition of null and parsing it only if it is not null?
you can use isNull method of JSONObject.
if(!innerJobj.isNull("format_file_name"))
{
//your rest of codes
}
Try with array.length() method.
JSONArray format_file_name = innerJobj.getJSONArray("format_file_name");
if (format_file_name.length() >0) {
for (int k = 0; k < format_file_name.length(); k++) {
JSONObject jsonObject1 = format_file_name.getJSONObject(i);
Iterator<String> keys = jsonObject1.keys();
while (keys.hasNext()) {
file = jsonObject1.getString(keys.next());
}
}
}
If you check documentation for getJSONArray method:
Returns the value mapped by name if it exists and is a JSONArray, or
throws otherwise.
So if the array is null an exception will be thrown, Instead of using getJSONArray, use another version: optJSONArray
Returns the value mapped by name if it exists and is a JSONArray, or null
otherwise.
It returns null without no exception so you can handle it by null check, Try this:
JSONArray format_file_name = innerJobj.optJSONArray("format_file_name");
if (format_file_name != null) {
for (int k = 0; k < format_file_name.length(); k++) {
JSONObject jsonObject1 = format_file_name.optJSONObject(k);
...
}
}
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.
I'm tring to do a simple matrix initialization in android and I get Error: java.lang.ArrayIndexOutOfBoundsException. I'm trying this:
Integer id = Integer.valueOf(idcateg);
System.out.println("Id-ul e"+id);
vot = new Integer[sirid.length][id];
for (int i = 0; i < sirid.length; i++) {
vot[i][id] = 0;
}
where id is a value between 1 and 5,sirid.length is a number that reflects number of images from different categorys. For example,I want for category 1 to have something like this :
vot[0][1]=0;
vot[1][1]=0;
vot[2][1]=0;
...etc
Where is my mistache?
try this
Integer id = Integer.valueOf(idcateg);
System.out.println("Id-ul e"+id);
vot = new Integer[sirid.length][id];
for (int i = 0; i < sirid.length; i++) {
vot[i][id-1] = 0;
}
array index start from 0
you set the size of the vot array to sirid.lengh by id but the array start index value from 0 to (size)(size value not included) see the for loop
I think because you initialize the array on id.
After that you call to vot[i][id] which then will alway be 1 too high.
f.e.
if you create new int[3][3]
you can only call positions 0,1 and 2
good luck
That is because id has been considered as Length of the String and you r trying to access the same element... but the last element is vat[i][id-1] but u r trying to get vat[i][id]
So better use this..
for (int i = 0; i < sirid.length; i++) {
for(int j = 0; j<id ; j++){
vot[i][j] = 0;
}
}
Hi guys if i try to assign the string to string Array i am getting NULL POINTER EXCEPTION..
Pls give me a solution..
Here is my code and i am getting null pointer in this line thumbs[j]=title;
as code Follows...
for (int i = 0; i < items.getLength(); i++) {
Node item = items.item(i);
NodeList properties = item.getChildNodes();
for (int j = 0; j < properties.getLength(); j++) {
Node property = properties.item(j);
String name = property.getNodeName();
if (name.equalsIgnoreCase("title")) {
try{
property.normalize();
Spanned title2 = Html.fromHtml(property.getFirstChild().getNodeValue().replaceAll("\\<.*?>", ""));
title = title2.toString();
}catch(Exception e){
Log.v("Exception",""+e);
}
thumbs[j]=title;
}
}
}
My guess is that the thumbs[] hasn't been initialized doing something like:
String [] thumbs = new String [properties.getLength()];
Though it's hard to say - your code doesn't show your declaration of this array.
The only possibility is that thumbs is null. It isn't a compile error if you don't initialize it. Don't rely on the IDE to tell you if something initialized prove it to yourself by examining your code, or print out the value of thumbs before you use it every time.
The only other exception possibilities at that line are ArrayIndexOutOfBoundsException if j > thumbs.length-1. But again, that's not a NPE. If title was null, that's fine and not an exception.