Null Pointer Exception with none of my code in logcat - java

I am getting a NPE, but when I check the logcat there are no points at which it is using my code.
The program crashes after attempting to show an AlertDialog containing a RadioButtonGroup. It previously worked and the only changes I remember are after the dialog.
Now I recieve the following:
EDIT:
06-19 17:15:19.430: E/AndroidRuntime(898): java.lang.NullPointerException
06-19 17:15:19.430: E/AndroidRuntime(898): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.widget.AbsListView.obtainView(AbsListView.java:2143)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.widget.ListView.measureHeightOfChildren(ListView.java:1246)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.widget.ListView.onMeasure(ListView.java:1158)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.view.View.measure(View.java:15518)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.view.View.measure(View.java:15518)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.view.View.measure(View.java:15518)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.view.View.measure(View.java:15518)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.view.View.measure(View.java:15518)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
06-19 17:15:19.430: E/AndroidRuntime(898): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2176)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.view.View.measure(View.java:15518)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.view.Choreographer.doFrame(Choreographer.java:532)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.os.Handler.handleCallback(Handler.java:725)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.os.Handler.dispatchMessage(Handler.java:92)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.os.Looper.loop(Looper.java:137)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.app.ActivityThread.main(ActivityThread.java:5041)
06-19 17:15:19.430: E/AndroidRuntime(898): at java.lang.reflect.Method.invokeNative(Native Method)
06-19 17:15:19.430: E/AndroidRuntime(898): at java.lang.reflect.Method.invoke(Method.java:511)
06-19 17:15:19.430: E/AndroidRuntime(898): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-19 17:15:19.430: E/AndroidRuntime(898): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-19 17:15:19.430: E/AndroidRuntime(898): at dalvik.system.NativeStart.main(Native Method)
The last code block that is run, when I step through, is:
public void addTeam (View view) {
//open select race dialog
AlertDialog alertDialog;
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
String[] items = new String[races.size()];
int i = 0;
for (Race race : races) {
//add radio button for each race
items[i] = race.raceName;
}
builder.setTitle(getString(R.string.select_race));
builder.setSingleChoiceItems(items, 0, null);
builder.setPositiveButton(R.string.done, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int whichButton) {
dialogInterface.dismiss();
Team team = new Team();
team.raceIndex = ((AlertDialog) dialogInterface).getListView().getCheckedItemPosition();
team.raceName = races.get(team.raceIndex).raceName;
setContentView(R.layout.edit_team);
((TextView) findViewById(R.id.raceTV)).setText(team.raceName);
}
});
alertDialog = builder.create();
alertDialog.show();
}
Without a line reference to my code, I do not even know where to start. Help is appreciated.

Here is an error in your code:
int i = 0;
for (Race race : races) {
//add radio button for each race
items[i] = race.raceName;
}
You don't increment i in the for loop.
May be this causes the crash.

You have two dialog instances. 1) AlertDialog dialog; and 2) DialogInterface dialog. So most probably AlertDialog dialog; throwing NPE as it was never initilized so defaluts to null

Replace this
((TextView) findViewById(R.id.raceTV)).setText(team.raceName);
with
((TextView) dialog.findViewById(R.id.raceTV)).setText(team.raceName);

Related

networkInfo.isConnected() error

I'm receiving an error when executing this code.
When connected to wifi, the function should display MAC address, otherwise "---"
private void DisplayWifiStatus() {
ConnectivityManager conMgr = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = conMgr.getActiveNetworkInfo();
wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo info = wifi.getConnectionInfo();
if (networkInfo.isConnected()) {
textMac.setText(info.getMacAddress());
}else {
textMac.setText("-----");
}
}
Logcat/Stacktrace:
03-11 21:32:07.532 3348-3348/com.light87sakurahimegmail.wifiscanner E/dalvikvm﹕ >>>>> Normal User
03-11 21:32:07.532 3348-3348/com.light87sakurahimegmail.wifiscanner E/dalvikvm﹕ >>>>> com.light87sakurahimegmail.wifiscanner [ userId:0 | appId:10337 ]
03-11 21:32:07.782 3348-3348/com.light87sakurahimegmail.wifiscanner E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.light87sakurahimegmail.wifiscanner, PID: 3348
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.light87sakurahimegmail.wifiscanner/com.light87sakurahimegmail.wifiscanner.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2350)
at android.app.ActivityThread.access$800(ActivityThread.java:163)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1257)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5335)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.light87sakurahimegmail.wifiscanner.MainActivity.DisplayWifiStatus(MainActivity.java:79)
at com.light87sakurahimegmail.wifiscanner.MainActivity.onCreate(MainActivity.java:43)
at android.app.Activity.performCreate(Activity.java:5389)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2256)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2350)
            at android.app.ActivityThread.access$800(ActivityThread.java:163)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1257)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:157)
            at android.app.ActivityThread.main(ActivityThread.java:5335)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
            at dalvik.system.NativeStart.main(Native Method)

How to store a single row data into a string array of sqlite database in android

I have called a function returnCurrentNameById() by passing the id of the particular row. But it gives an error.
String[] s1;
public String[] returnCurrentNameById(int k) throws SQLException{
String[] columns = new String[]{ KEY_ID1, KEY_NAME, Key_DOB, KEY_AGE, KEY_PHONE_NO, Key_EXPERIENCE, KEY_EMAIL, KEY_STATUS, KEY_STATUS};
SQLiteDatabase db3 = this.getReadableDatabase();
Cursor c = db3.query(DATA_BASE_TABLE_NAME1, columns, KEY_ID1 + "=" + k, null, null, null, null);
if (c != null){
c.moveToFirst();
for(int i = 0; i<=8; i++){
s1[i] = c.getString(i);
}
return s1;
}
return null;
}
The Log cat error report is:
02-15 16:38:13.424 1545-1545/world.com.my`enter code here`progect09 D/AndroidRuntime﹕ Shutting down VM
02-15 16:38:13.464 1545-1545/world.com.myprogect09 W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x2bc9a300)
02-15 16:38:13.533 1545-1545/world.com.myprogect09 E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:3591)
at android.view.View.performClick(View.java:4084)
at android.view.View$PerformClick.run(View.java:16966)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at android.view.View$1.onClick(View.java:3586)
            at android.view.View.performClick(View.java:4084)
            at android.view.View$PerformClick.run(View.java:16966)
            at android.os.Handler.handleCallback(Handler.java:615)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4745)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at world.com.myprogect09.DataBaseSQL.returnCurrentNameById1(DataBaseSQL.java:209)
at world.com.myprogect09.ThirdActivity.isClicked(ThirdActivity.java:74)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at android.view.View$1.onClick(View.java:3586)
            at android.view.View.performClick(View.java:4084)
            at android.view.View$PerformClick.run(View.java:16966)
            at android.os.Handler.handleCallback(Handler.java:615)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4745)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)
02-15 16:38:34.491 1545-1545/world.com.myprogect09 I/Process﹕ Sending signal. PID: 1545 SIG: 9
You have not initialized your s1 array.
Use
s1 = new String[9];
to allocate an array of 9 elements.
Also check the return value of moveToFirst() so you don't get an exception in case the query returns no rows.
Replace
Cursor c = db3.query(DATA_BASE_TABLE_NAME1, columns, KEY_ID1 + "=" + k, null, null, null, null);
with
Cursor c = db3.query(DATA_BASE_TABLE_NAME1, columns, KEY_ID1 + " = ?", new String[]{String.valueOf(k)}, null, null, null);

Populate Listview with filenames

Im writing an application in which i need to list all the pdf files present in a particular forlder.
However for now im trying just to list all the files present in the raw folder and populate a ListView .
This is what i have so far:
Fields[] fields;
ListView list;
String[] sample;
fields=R.raw.class.getFields();int count=0;
for(Field f : fields)
{
sample[count]=f.getName();count++;
}
list=getListView();
list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_checked, sample));
However the application keeps crashing when i start it.
I cant figure where im going wrong.
Can anyone help me?
the log :
06-19 08:52:20.352: E/AndroidRuntime(4955): FATAL EXCEPTION: main
06-19 08:52:20.352: E/AndroidRuntime(4955): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.magazine/com.example.magazine.MainActivity}: java.lang.NullPointerException: storage == null
06-19 08:52:20.352: E/AndroidRuntime(4955): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
06-19 08:52:20.352: E/AndroidRuntime(4955): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
06-19 08:52:20.352: E/AndroidRuntime(4955): at android.app.ActivityThread.access$600(ActivityThread.java:141)
06-19 08:52:20.352: E/AndroidRuntime(4955): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
06-19 08:52:20.352: E/AndroidRuntime(4955): at android.os.Handler.dispatchMessage(Handler.java:99)
06-19 08:52:20.352: E/AndroidRuntime(4955): at android.os.Looper.loop(Looper.java:137)
06-19 08:52:20.352: E/AndroidRuntime(4955): at android.app.ActivityThread.main(ActivityThread.java:5041)
06-19 08:52:20.352: E/AndroidRuntime(4955): at java.lang.reflect.Method.invokeNative(Native Method)
06-19 08:52:20.352: E/AndroidRuntime(4955): at java.lang.reflect.Method.invoke(Method.java:511)
06-19 08:52:20.352: E/AndroidRuntime(4955): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-19 08:52:20.352: E/AndroidRuntime(4955): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-19 08:52:20.352: E/AndroidRuntime(4955): at dalvik.system.NativeStart.main(Native Method)
06-19 08:52:20.352: E/AndroidRuntime(4955): Caused by: java.lang.NullPointerException: storage == null
06-19 08:52:20.352: E/AndroidRuntime(4955): at java.util.Arrays$ArrayList.<init>(Arrays.java:38)
06-19 08:52:20.352: E/AndroidRuntime(4955): at java.util.Arrays.asList(Arrays.java:154)
06-19 08:52:20.352: E/AndroidRuntime(4955): at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:128)
06-19 08:52:20.352: E/AndroidRuntime(4955): at com.example.magazine.MainActivity.onCreate(MainActivity.java:44)
06-19 08:52:20.352: E/AndroidRuntime(4955): at android.app.Activity.performCreate(Activity.java:5104)
06-19 08:52:20.352: E/AndroidRuntime(4955): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
06-19 08:52:20.352: E/AndroidRuntime(4955): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
06-19 08:52:20.352: E/AndroidRuntime(4955): ... 11 more
You should initialize array of Strings before:
Field[] fields = R.raw.class.getFields();
ListView list;
String sample[] = new String[R.raw.class.getFields().length];
int count=0;
for(Field f : fields)
{
sample[count]=f.getName();
count++;
}
list=getListView();
list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_checked, sample));

App crashing on Android when clicking a button

I am getting the following Logcat information when I click a button on my app:
06-19 11:24:45.308: E/AndroidRuntime(11498): FATAL EXCEPTION: main
06-19 11:24:45.308: E/AndroidRuntime(11498): java.lang.IllegalStateException: Could not execute method of the activity
06-19 11:24:45.308: E/AndroidRuntime(11498): at android.view.View$1.onClick(View.java:3674)
06-19 11:24:45.308: E/AndroidRuntime(11498): at android.view.View.performClick(View.java:4198)
06-19 11:24:45.308: E/AndroidRuntime(11498): at android.view.View$PerformClick.run(View.java:17164)
06-19 11:24:45.308: E/AndroidRuntime(11498): at android.os.Handler.handleCallback(Handler.java:615)
06-19 11:24:45.308: E/AndroidRuntime(11498): at android.os.Handler.dispatchMessage(Handler.java:92)
06-19 11:24:45.308: E/AndroidRuntime(11498): at android.os.Looper.loop(Looper.java:137)
06-19 11:24:45.308: E/AndroidRuntime(11498): at android.app.ActivityThread.main(ActivityThread.java:4918)
06-19 11:24:45.308: E/AndroidRuntime(11498): at java.lang.reflect.Method.invokeNative(Native Method)
06-19 11:24:45.308: E/AndroidRuntime(11498): at java.lang.reflect.Method.invoke(Method.java:511)
06-19 11:24:45.308: E/AndroidRuntime(11498): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
06-19 11:24:45.308: E/AndroidRuntime(11498): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
06-19 11:24:45.308: E/AndroidRuntime(11498): at dalvik.system.NativeStart.main(Native Method)
06-19 11:24:45.308: E/AndroidRuntime(11498): Caused by: java.lang.reflect.InvocationTargetException
06-19 11:24:45.308: E/AndroidRuntime(11498): at java.lang.reflect.Method.invokeNative(Native Method)
06-19 11:24:45.308: E/AndroidRuntime(11498): at java.lang.reflect.Method.invoke(Method.java:511)
06-19 11:24:45.308: E/AndroidRuntime(11498): at android.view.View$1.onClick(View.java:3669)
06-19 11:24:45.308: E/AndroidRuntime(11498): ... 11 more
06-19 11:24:45.308: E/AndroidRuntime(11498): Caused by: java.lang.NoClassDefFoundError: com.facebook.android.MainActivity$UpdateStatusListener
06-19 11:24:45.308: E/AndroidRuntime(11498): at com.facebook.android.MainActivity.triggerDialog(MainActivity.java:55)
06-19 11:24:45.308: E/AndroidRuntime(11498): at com.facebook.android.MainActivity.firstClicked(MainActivity.java:36)
06-19 11:24:45.308: E/AndroidRuntime(11498): ... 14 more
Here is how I have my classes set up following an example using the Facebook SDK for Android, with only the relevant methods and info displayed here:
public class MainActivity extends Activity {
//triggered when the button is clicked
public void firstClicked(View view)
{
triggerDialog();
}
public void triggerDialog()
{
//assume for the sake of this post that this string array has 4 strings
String[] offer_details = postOffer.getDetails();
Bundle params = new Bundle();
params.putString("caption", getString(R.string.app_name)); //Hackbook for Android
Utility.mFacebook.dialog(MainActivity.this, "feed", params, new UpdateStatusListener());
}
public class UpdateStatusListener extends BaseDialogListener {
#Override
public void onComplete(Bundle values) {
Log.i("wbbug","Status post complete.");
final String postId = values.getString("post_id");
if (postId != null) {
Toast toast = Toast.makeText(getApplicationContext(), "Facebook status update successful", Toast.LENGTH_LONG);
toast.show();
} else {
Toast toast = Toast.makeText(getApplicationContext(), "No wall post made",
Toast.LENGTH_LONG);
toast.show();
}
}
}
}
This is all directly following an example from the Facebook SDK, and the example is working with this basic code. Why is it that it cannot find the class UpdateStatusListener()? Thanks!

null into array from loop

this answer comes from here: 2Darray to 1darray with for loop
I don´t know if I can reopen it once it was answered.. my apologies if that's the case.
I modified a little that function to end like this:
public String[] initarray() {
xFINAL = new String [data_array.length];
int i = 0;
int j=0;
int UMB1 = 100;
int l = data_array.length;
for (i=0 ; i < l ; i++) {
LATIT = Double.parseDouble(data_array [i][2]);
LONGIT = Double.parseDouble(data_array [i][3]);
dist = calculateDistanceByHaversineFormula(Localizacion.LONGITUD_D, Localizacion.data_arrayUD_D,LONGIT, LATIT);
if (dist < UMB1){
xFINAL[j++] = data_array [i][0];
System.out.println(Arrays.toString(xFINAL));
System.out.println("j++" + data_array[i][0]);
}
}
return xFINAL;
}
dist = the difference in meters between 2 locations, returning an int.
data_array is already defined before.
It's "kind of working". What I get from that code is an array like this(data1 and data3 are inside the condition, data2 would be false)
[data1 , data3 , null]
And should be like this instead I think:
[data1, data3]
The error comes when I am trying to use that xFINAL array with an spinner:
public final void rel() {
String[] array_spinner = MYSQL.xFINAL;
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_dropdown_item, array_spinner);
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp_1.setAdapter(spinnerArrayAdapter);
}
When I try to load the spinner it gives me the following:
03-25 13:25:26.367: E/AndroidRuntime(30843): FATAL EXCEPTION: main
03-25 13:25:26.367: E/AndroidRuntime(30843): java.lang.NullPointerException
03-25 13:25:26.367: E/AndroidRuntime(30843): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394)
03-25 13:25:26.367: E/AndroidRuntime(30843): at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
03-25 13:25:26.367: E/AndroidRuntime(30843): at android.widget.Spinner.measureContentWidth(Spinner.java:681)
03-25 13:25:26.367: E/AndroidRuntime(30843): at android.widget.Spinner.onMeasure(Spinner.java:442)
03-25 13:25:26.367: E/AndroidRuntime(30843): at android.view.View.measure(View.java:15518)
03-25 13:25:26.367: E/AndroidRuntime(30843): at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:681)
03-25 13:25:26.367: E/AndroidRuntime(30843): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
03-25 13:25:26.367: E/AndroidRuntime(30843): at android.view.View.measure(View.java:15518)
03-25 13:25:26.367: E/AndroidRuntime(30843): at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:681)
03-25 13:25:26.367: E/AndroidRuntime(30843): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
03-25 13:25:26.367: E/AndroidRuntime(30843): at android.view.View.measure(View.java:15518)
03-25 13:25:26.367: E/AndroidRuntime(30843): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
03-25 13:25:26.367: E/AndroidRuntime(30843): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
03-25 13:25:26.367: E/AndroidRuntime(30843): at android.view.View.measure(View.java:15518)
03-25 13:25:26.367: E/AndroidRuntime(30843): at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
03-25 13:25:26.367: E/AndroidRuntime(30843): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
03-25 13:25:26.367: E/AndroidRuntime(30843): at android.view.View.measure(View.java:15518)
03-25 13:25:26.367: E/AndroidRuntime(30843): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
03-25 13:25:26.367: E/AndroidRuntime(30843): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
03-25 13:25:26.367: E/AndroidRuntime(30843): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2176)
03-25 13:25:26.367: E/AndroidRuntime(30843): at android.view.View.measure(View.java:15518)
03-25 13:25:26.367: E/AndroidRuntime(30843): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874)
03-25 13:25:26.367: E/AndroidRuntime(30843): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089)
03-25 13:25:26.367: E/AndroidRuntime(30843): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265)
03-25 13:25:26.367: E/AndroidRuntime(30843): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
03-25 13:25:26.367: E/AndroidRuntime(30843): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
03-25 13:25:26.367: E/AndroidRuntime(30843): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
03-25 13:25:26.367: E/AndroidRuntime(30843): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
03-25 13:25:26.367: E/AndroidRuntime(30843): at android.view.Choreographer.doFrame(Choreographer.java:532)
03-25 13:25:26.367: E/AndroidRuntime(30843): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
03-25 13:25:26.367: E/AndroidRuntime(30843): at android.os.Handler.handleCallback(Handler.java:725)
03-25 13:25:26.367: E/AndroidRuntime(30843): at android.os.Handler.dispatchMessage(Handler.java:92)
03-25 13:25:26.367: E/AndroidRuntime(30843): at android.os.Looper.loop(Looper.java:137)
03-25 13:25:26.367: E/AndroidRuntime(30843): at android.app.ActivityThread.main(ActivityThread.java:5041)
03-25 13:25:26.367: E/AndroidRuntime(30843): at java.lang.reflect.Method.invokeNative(Native Method)
03-25 13:25:26.367: E/AndroidRuntime(30843): at java.lang.reflect.Method.invoke(Method.java:511)
03-25 13:25:26.367: E/AndroidRuntime(30843): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-25 13:25:26.367: E/AndroidRuntime(30843): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-25 13:25:26.367: E/AndroidRuntime(30843): at dalvik.system.NativeStart.main(Native Method)
If I just get the 3 data into the spinner it works.. so I am lost.
Any help please? :)
Thanks in advance.
[edit]
03-25 19:39:23.081: I/System.out(3501): [00000001]
03-25 19:39:23.081: I/System.out(3501): 00000001
03-25 19:39:23.091: I/System.out(3501): [00000001, 00000003]
03-25 19:39:23.091: D/AndroidRuntime(3501): Shutting down VM
03-25 19:39:23.091: W/dalvikvm(3501): threadid=1: thread exiting with uncaught exception (group=0x40bac930)
03-25 19:39:23.091: E/AndroidRuntime(3501): FATAL EXCEPTION: main
03-25 19:39:23.091: E/AndroidRuntime(3501): java.lang.IndexOutOfBoundsException: Invalid index 2, size is 2
03-25 19:39:23.091: E/AndroidRuntime(3501): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
03-25 19:39:23.091: E/AndroidRuntime(3501): at java.util.ArrayList.get(ArrayList.java:304)
03-25 19:39:23.091: E/AndroidRuntime(3501): at com.example.mvlpres.c_sql.recorre_array2(c_sql.java:635)
03-25 19:39:23.091: E/AndroidRuntime(3501): at com.example.mvlpres.SincronizacionFragment$1.onClick(SincronizacionFragment.java:179)
03-25 19:39:23.091: E/AndroidRuntime(3501): at android.view.View.performClick(View.java:4204)
03-25 19:39:23.091: E/AndroidRuntime(3501): at android.view.View$PerformClick.run(View.java:17355)
03-25 19:39:23.091: E/AndroidRuntime(3501): at android.os.Handler.handleCallback(Handler.java:725)
03-25 19:39:23.091: E/AndroidRuntime(3501): at android.os.Handler.dispatchMessage(Handler.java:92)
03-25 19:39:23.091: E/AndroidRuntime(3501): at android.os.Looper.loop(Looper.java:137)
03-25 19:39:23.091: E/AndroidRuntime(3501): at android.app.ActivityThread.main(ActivityThread.java:5041)
03-25 19:39:23.091: E/AndroidRuntime(3501): at java.lang.reflect.Method.invokeNative(Native Method)
03-25 19:39:23.091: E/AndroidRuntime(3501): at java.lang.reflect.Method.invoke(Method.java:511)
03-25 19:39:23.091: E/AndroidRuntime(3501): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-25 19:39:23.091: E/AndroidRuntime(3501): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-25 19:39:23.091: E/AndroidRuntime(3501): at dalvik.system.NativeStart.main(Native Method)
System.out.println(Arrays.toString(xFINAL));
This line should be after for loop completes.
For references (anything that holds an object) that is null.
For int/short/byte that is a 0.
For float/double that is a 0.0
For booleans that is a false.
When you create an array of String, all entries are null initially. Your array size is 3 I think so initially it is initialized with [null,null, null]. Two null get replaced with the values and one remains as it is.
EDIT: Use ArrayList instead Array
List<String> list = new ArrayList<String>();
for (i=0 ; i < l ; i++) {
LATIT = Double.parseDouble(data_array [i][2]);
LONGIT = Double.parseDouble(data_array [i][3]);
dist = calculateDistanceByHaversineFormula(Localizacion.LONGITUD_D, Localizacion.data_arrayUD_D,LONGIT, LATIT);
if (dist < UMB1){
list.add(data_array [i][0]);
System.out.println(list);
System.out.println(list.get(i));
}
}

Categories