Android Array String - android

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.

Related

Handling jsonarray with null value

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);
...
}
}

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 - How to set value of ArrayList to TextView

i have a problem here.
I've ArrayList from the result of my parser class. then i wanna put that value (value from ArrayList) to TextView.
here is the work i've done till now.
I create TextView on my main.xml
<TextView
android:id="#+id/deskripsi"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello"
/>
and here at my onCreate Method, i initialized the Text View
desk = (TextView)findViewById(R.id.deskripsi);
and then i tried to parser the KML document from google map and at the Node Placemark i put its value to ArrayList, here my code
ArrayList<String> pathName = new ArrayList<String>();
Object[] objPlace;
//Parser Node Placemark
NodeList nlPlace = doc.getElementsByTagName("Placemark");
for(int p = 0; p < nlPlace.getLength(); p++){
Node rootPlace = nlPlace.item(p);
NodeList itemPlace = rootPlace.getChildNodes();
for(int y = 0; y < itemPlace.getLength(); y++){
Node placeStringNode = itemPlace.item(y);
NodeList place = placeStringNode.getChildNodes();
//valueName = nameList.item(0).getNodeValue().toString() + "+";
pathName.add(place.item(0).getNodeValue());
}
}
objPlace = pathName.toArray();
desk.setText("");
for (int j = 0; j < objPlace.length; j++){
desk.append("Deskripsi:\n" + objPlace[j].toString() + "\n");
}
but when itried to run its to my emulator and real device, i get an error. here's my LogCat
please help me, and sorry for my english >_<
You can do this instead; use pathName directly in the for loop...
desk.setText("");
for (int j = 0; j < pathName.size(); j++){
desk.append("Deskripsi:\n" + pathName.get(j) + "\n");
}
you can put the line
objPlace[] = new Object[pathName.size()]
before
objPlace = pathName.toArray();
and check output otherwise do this way
desk.setText("");
for (int j = 0; j < pathName.size(); j++){
desk.append("Deskripsi:\n" + pathName.get(j) + "\n");
}
You never initialize your array... objPlace[] is a null array.
You need to do something like:
objPlace[] = new Object[arraysizenumber]
Ii is caused by NullPointerException, that means some needs parameters and you have passed a null value to it.
Based on your code it might be around desk.setText("");. Try inserting a space or some value into it and run.
Thanks my fren. im still not really well on using Object[]. then i used this code for solving my problem
String strPlace[] = (String[])pathName.toArray(new String[pathName.size()]);
desk.setText("");
for (int j = 0; j < strPlace.length; j++){
desk.append("Deskripsi:\n" + strPlace[j] + "\n");
}
Thanks for userSeven7s, flameaddict and khan for trying to help me.
nice to know u all :)

Matrix initialization in Android?

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;
}
}

Dom parsing in Android

If I parse the tag that contains <p>Some Text</p> tag, I get a null pointer exception.
My RSS feed is as follows:
<quaddeals_conditions><p>Limit one QuadDeal</p></quaddeals_conditions>
My code is:
if (name.equalsIgnoreCase("quaddeals_conditions")) {
property.normalize();
conditions = property.getFirstChild().getNodeValue();
}
You have an element inside an element .
Therefore retrieve all quaddeals and then iterate each one and retrieve from it the p element:
DocumentBuilder builder = factory.newDocumentBuilder();
Document dom = builder.parse(this.inputStream);
Element root = dom.getDocumentElement();
// snip
NodeList items = root.getElementsByTagName("quaddeals_conditions");
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("p")) {
property.getFirstChild().getNodeValue(); // Your paragraph data
}
}
}
Hope this helps.
is "name" not NULL? I dont see you check for that.
It's good coding practice to compare the other way if possible:
if ("quaddeals_conditions".equalsIgnoreCase(name))...
So even if "name" is NULL, you don't get a NullPointerException.
Always check for not null before accessing some object member.

Categories

Resources