Jump to content



Photo

[Android] array list issue.


  • Please log in to reply
10 replies to this topic

#1 Original Poster

Original Poster

    Systems Developer

  • Tech Issues Solved: 1
  • Joined: 15-July 08
  • Location: my room
  • OS: windows 7/8, Kali, ubuntu, OSx 10.9
  • Phone: Android

Posted 12 May 2013 - 02:16

hello people! i really hope someone here can help me out a bit with this android code... I can get a list array working no problems but its when i introduce the loop and text messages that it becomes an issue ... I can get the text messages to appear no problem but not in this array list... any help?

it says one of the causes is a null pointer exception or something... but I dont see any issue ;( i am probs just blind ?

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.database.Cursor;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends Activity {

   
	 String[] sArray;
 
   
	 private ListView smsListView;
 
	 private ArrayAdapter arrayAdapter;
 
  
	 @Override
	 public void onCreate(Bundle savedInstanceState) {
		 super.onCreate(savedInstanceState);
		 setContentView(R.layout.activity_main);
 
   
		 smsListView = (ListView) findViewById(R.id.listview);
		 sms();
 
		 arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, sArray);
 
	   
		 smsListView.setAdapter(arrayAdapter);
	 }
	
	 private void sms() {
	   Uri uriSMSURI = Uri.parse("content://sms/inbox");
	    Cursor cur = getContentResolver().query(uriSMSURI, null, null, null,null);
	   
	    int i = 0;
	    while (cur.moveToNext() && i < cur.getColumnCount()) {
	    i++;
	   sArray[i] = "From :" + cur.getString(2);
	  
			
		  
	    }
	 }
}



#2 giantpotato

giantpotato

    Neowinian Senior

  • Joined: 27-January 04
  • Location: Montreal, Canada

Posted 12 May 2013 - 02:22

You never initialized sArray.

#3 OP Original Poster

Original Poster

    Systems Developer

  • Tech Issues Solved: 1
  • Joined: 15-July 08
  • Location: my room
  • OS: windows 7/8, Kali, ubuntu, OSx 10.9
  • Phone: Android

Posted 12 May 2013 - 02:25

You never initialized sArray.


even when i initialise it, String[] sArray = null;

it still comes up with the error

#4 giantpotato

giantpotato

    Neowinian Senior

  • Joined: 27-January 04
  • Location: Montreal, Canada

Posted 12 May 2013 - 02:33

even when i initialise it, String[] sArray = null;

it still comes up with the error


That's not initializing anything.

String[] sArray = new String[100]; would initialize the array.

#5 OP Original Poster

Original Poster

    Systems Developer

  • Tech Issues Solved: 1
  • Joined: 15-July 08
  • Location: my room
  • OS: windows 7/8, Kali, ubuntu, OSx 10.9
  • Phone: Android

Posted 12 May 2013 - 02:41

That's not initializing anything.

String[] sArray = new String[100]; would initialize the array.


yes i realised that shortly after and faced palmed my self... but it wont work with a count option i placed in ..


int cou = cur.getColumnCount();
String sArray[] = new String[cou];

any idea why ?? it works when i set it to a number but not when i set it to a varible ... this all works fine in actually java (the logic)

also yes I have moved it into an appropriate place for the count to work :)

#6 Andre S.

Andre S.

    Asik

  • Tech Issues Solved: 14
  • Joined: 26-October 05

Posted 12 May 2013 - 02:54

What do you mean by "it won't work"? What's the error you're getting?

#7 OP Original Poster

Original Poster

    Systems Developer

  • Tech Issues Solved: 1
  • Joined: 15-July 08
  • Location: my room
  • OS: windows 7/8, Kali, ubuntu, OSx 10.9
  • Phone: Android

Posted 12 May 2013 - 03:07

What do you mean by "it won't work"? What's the error you're getting?


I was getting a arrayIndexOutOfBoundsException ... but I fixed it by adding 1 to the count, I dont know why this fixed it it just did lol probably something to do with the array starting at 0 and the count matching the while loop means it had one less array set then needed :) thanks for all the help! even if i did derp a little bit!
working code for those who want to see.
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.database.Cursor;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends Activity {

   
	
 
   
	 private ListView smsListView;
 
	 private ArrayAdapter arrayAdapter;
 
  
	 @Override
	 public void onCreate(Bundle savedInstanceState) {
		 super.onCreate(savedInstanceState);
		 setContentView(R.layout.activity_main);
 
   
		 smsListView = (ListView) findViewById(R.id.listview);
		 sms();
 
	  
	 }
	
	 private void sms() {
	   Uri uriSMSURI = Uri.parse("content://sms/inbox");
	    Cursor cur = getContentResolver().query(uriSMSURI, null, null, null,null);
	   
	    int i = 0;
	    int cou = cur.getCount() +1;
	    String sArray[] = new String[cou];
	   
	    while (cur.moveToNext() && i < cur.getColumnCount()) {
	    i++;
	   
	   sArray[i] = "From :" + cur.getString(2);
	  
	   
		  
	    }
	    arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, sArray);
		
		  smsListView.setAdapter(arrayAdapter);
	 }
}


#8 Andre S.

Andre S.

    Asik

  • Tech Issues Solved: 14
  • Joined: 26-October 05

Posted 13 May 2013 - 16:58

I was getting a arrayIndexOutOfBoundsException ... but I fixed it by adding 1 to the count, I dont know why this fixed it it just did lol

Don't you run your code inside a debugger and doesn't it allow you to see exactly what's happening at the time the exception occurs? This looks like a typical off-by-one error and there's really no excuse to fix that by trial-and-error rather than understanding what's going on.

#9 OP Original Poster

Original Poster

    Systems Developer

  • Tech Issues Solved: 1
  • Joined: 15-July 08
  • Location: my room
  • OS: windows 7/8, Kali, ubuntu, OSx 10.9
  • Phone: Android

Posted 13 May 2013 - 21:19

Don't you run your code inside a debugger and doesn't it allow you to see exactly what's happening at the time the exception occurs? This looks like a typical off-by-one error and there's really no excuse to fix that by trial-and-error rather than understanding what's going on.


yes, I have a debugger within my IDE and i do use it, I understand now what the problem was but I did not know what the problem was (specifically) untill i had fixed it, I understand why it was an off by 1, I rewrote the whole code the next day and made it much better then what you see above without having to add the 1.

in all honesty I normally run here when its like 4 in the morning and im desperate to get my work done before i sleep, as soon as i woke up the next day I redid my code and made it far far better. my brain just does not function well after a certain point but I cant sleep untill i get to a checkpoint in my work

#10 +Majesticmerc

Majesticmerc

    Resident Idealist

  • Tech Issues Solved: 8
  • Joined: 24-August 05
  • Location: United Kingdom
  • OS: Arch Linux / Win 7
  • Phone: HTC One X

Posted 13 May 2013 - 22:19

in all honesty I normally run here when its like 4 in the morning and im desperate to get my work done before i sleep, as soon as i woke up the next day I redid my code and made it far far better. my brain just does not function well after a certain point but I cant sleep untill i get to a checkpoint in my work


Yeah, I wouldn't recommend doing that. My code quality tends to go something like this:

Productivity chart.png

#11 OP Original Poster

Original Poster

    Systems Developer

  • Tech Issues Solved: 1
  • Joined: 15-July 08
  • Location: my room
  • OS: windows 7/8, Kali, ubuntu, OSx 10.9
  • Phone: Android

Posted 14 May 2013 - 20:34

Yeah, I wouldn't recommend doing that. My code quality tends to go something like this:

Productivity chart.png


i skip straight to 2pm