Ruben Renzema
Ruben Renzema

Reputation: 11

FATAL EXCEPTION: main, but eclipse does not show any error

here is my problem, I putted some log.i in my code, even at the end, and they are all printed in the logCat, meaning it goes untill the end, but some errors keep comming...

thanks for your help!

the log:

08-02 13:14:11.030: I/Process(1817): Sending signal. PID: 1817 SIG: 9
08-02 13:15:36.670: D/dalvikvm(1854): GC_FOR_ALLOC freed 36K, 4% free 6347K/6595K, paused 60ms
08-02 13:15:36.790: I/dalvikvm-heap(1854): Grow heap (frag case) to 12.983MB for 7056016-byte allocation
08-02 13:15:36.880: D/dalvikvm(1854): GC_FOR_ALLOC freed <1K, 3% free 13237K/13511K, paused 60ms
08-02 13:15:37.020: D/dalvikvm(1854): GC_CONCURRENT freed <1K, 3% free 13237K/13511K, paused 4ms+3ms
08-02 13:15:38.940: D/dalvikvm(1854): GC_CONCURRENT freed 3K, 1% free 15246K/15367K, paused 3ms+3ms
08-02 13:15:41.521: D/dalvikvm(1854): GC_FOR_ALLOC freed 28K, 1% free 15555K/15687K, paused 59ms
08-02 13:15:41.551: I/dalvikvm-heap(1854): Grow heap (frag case) to 17.630MB for 2499884-byte allocation
08-02 13:15:41.701: D/dalvikvm(1854): GC_FOR_ALLOC freed <1K, 2% free 17996K/18183K, paused 59ms
08-02 13:15:41.851: D/dalvikvm(1854): GC_CONCURRENT freed <1K, 2% free 17996K/18183K, paused 6ms+3ms
08-02 13:15:42.712: I/log_tag(1854): ID_ville: 3, Nom_ville: Lyon
08-02 13:15:42.712: I/log_tag(1854): ID_ville: 10, Nom_ville: Lille
08-02 13:15:42.712: I/test tableau(1854): Lyon
08-02 13:15:42.720: I/test 2 tableau(1854): Lille
08-02 13:15:42.750: D/AndroidRuntime(1854): Shutting down VM
08-02 13:15:42.750: W/dalvikvm(1854): threadid=1: thread exiting with uncaught exception (group=0x40014760)
08-02 13:15:42.781: E/AndroidRuntime(1854): FATAL EXCEPTION: main
08-02 13:15:42.781: E/AndroidRuntime(1854): java.lang.NullPointerException
08-02 13:15:42.781: E/AndroidRuntime(1854):     at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:398)
08-02 13:15:42.781: E/AndroidRuntime(1854):     at android.widget.ArrayAdapter.getView(ArrayAdapter.java:366)
08-02 13:15:42.781: E/AndroidRuntime(1854):     at android.widget.AbsListView.obtainView(AbsListView.java:1949)
08-02 13:15:42.781: E/AndroidRuntime(1854):     at android.widget.ListView.measureHeightOfChildren(ListView.java:1228)
08-02 13:15:42.781: E/AndroidRuntime(1854):     at android.widget.ListView.onMeasure(ListView.java:1139)
08-02 13:15:42.781: E/AndroidRuntime(1854):     at android.view.View.measure(View.java:10828)
08-02 13:15:42.781: E/AndroidRuntime(1854):     at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:581)
08-02 13:15:42.781: E/AndroidRuntime(1854):     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:365)
08-02 13:15:42.781: E/AndroidRuntime(1854):     at android.view.View.measure(View.java:10828)
08-02 13:15:42.781: E/AndroidRuntime(1854):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4351)
08-02 13:15:42.781: E/AndroidRuntime(1854):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:267)
08-02 13:15:42.781: E/AndroidRuntime(1854):     at android.view.View.measure(View.java:10828)
08-02 13:15:42.781: E/AndroidRuntime(1854):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:764)
08-02 13:15:42.781: E/AndroidRuntime(1854):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:519)
08-02 13:15:42.781: E/AndroidRuntime(1854):     at android.view.View.measure(View.java:10828)
08-02 13:15:42.781: E/AndroidRuntime(1854):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4351)
08-02 13:15:42.781: E/AndroidRuntime(1854):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:267)
08-02 13:15:42.781: E/AndroidRuntime(1854):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:1889)
08-02 13:15:42.781: E/AndroidRuntime(1854):     at android.view.View.measure(View.java:10828)
08-02 13:15:42.781: E/AndroidRuntime(1854):     at android.view.ViewRoot.performTraversals(ViewRoot.java:938)
08-02 13:15:42.781: E/AndroidRuntime(1854):     at android.view.ViewRoot.handleMessage(ViewRoot.java:2040)
08-02 13:15:42.781: E/AndroidRuntime(1854):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-02 13:15:42.781: E/AndroidRuntime(1854):     at android.os.Looper.loop(Looper.java:132)
08-02 13:15:42.781: E/AndroidRuntime(1854):     at android.app.ActivityThread.main(ActivityThread.java:4123)
08-02 13:15:42.781: E/AndroidRuntime(1854):     at java.lang.reflect.Method.invokeNative(Native Method)
08-02 13:15:42.781: E/AndroidRuntime(1854):     at java.lang.reflect.Method.invoke(Method.java:491)
08-02 13:15:42.781: E/AndroidRuntime(1854):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
08-02 13:15:42.781: E/AndroidRuntime(1854):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
08-02 13:15:42.781: E/AndroidRuntime(1854):     at dalvik.system.NativeStart.main(Native Method)
08-02 13:16:04.090: I/Process(1854): Sending signal. PID: 1854 SIG: 9

the code:

import android.os.Bundle;
import android.app.Activity;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;  
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MainClient extends Activity {
TextView txt;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_client);
    String[] listeStrings = new String[999];
    listeStrings = getServerData(strURL);
    ListView lvListe = (ListView)findViewById(R.id.ClientList);
    Log.i("test tableau", listeStrings[0]);
    lvListe.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,listeStrings)); 
    Log.i("test 2 tableau", listeStrings[1]);
}

public static final String strURL = "http://www.kogitus.com/test/ville.php";

private String[] getServerData(String returnString) {
    InputStream is = null;
    String result = "";
    String[] listeStrings = new String[999];
    // Envoyer la requête au script PHP.
    // Script PHP : $sql=mysql_query("select * from tblVille where Nom_ville like '".$_REQUEST['ville']."%'");
    // $_REQUEST['ville'] sera remplacé par L dans notre exemple.
    // Ce qui veut dire que la requête enverra les villes commençant par la lettre L
    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    nameValuePairs.add(new BasicNameValuePair("ville","L"));

    try{
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(strURL);
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        is = entity.getContent();

    }catch(Exception e){
        Log.e("log_tag", "Error in http connection " + e.toString());
    }

    try{
        BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        result=sb.toString();
    }catch(Exception e){
        Log.e("log_tag", "Error converting result " + e.toString());
    }

    try{
        JSONArray jArray = new JSONArray(result);
        returnString = "";
        for(int i=0;i<jArray.length();i++){
            JSONObject json_data = jArray.getJSONObject(i);
            // Affichage ID_ville et Nom_ville dans le LogCat
            Log.i("log_tag","ID_ville: "+json_data.getInt("ID_ville")+
                    ", Nom_ville: "+json_data.getString("Nom_ville"));
            listeStrings[i] = json_data.getString("Nom_ville");
            // Résultats de la requête
            //if (returnString == "")
                //returnString += json_data.getString("Nom_ville"); 
            //else
                //returnString += "\n" + jArray.getJSONObject(i);
        }
    }catch(JSONException e){
        Log.e("log_tag", "Error parsing data " + e.toString());
    }
    return listeStrings;
}

}

and he XML file:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/wallpaper" >

<ImageView
    android:id="@+id/BottomBar"
    android:layout_width="fill_parent"
    android:layout_height="80dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:scaleType="fitXY"
    android:src="@drawable/bottombar" />

<ImageView
    android:id="@+id/MenuClient"
    android:layout_width="453dp"
    android:layout_height="620dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:scaleType="fitXY"
    android:src="@drawable/menuclient" />

<ImageView
    android:id="@+id/RightBar"
    android:layout_width="160dp"
    android:layout_height="620dp"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:scaleType="fitXY"
    android:src="@drawable/rightbar" />

<Button
    android:id="@+id/BoutonBills"
    style="@android:style/Animation.Activity"
    android:layout_width="152dp"
    android:layout_height="129dp"
    android:layout_marginTop="480dp"
    android:layout_alignParentRight="true"
    android:background="@drawable/bills" />

<Button
    android:id="@+id/BoutonProviders"
    style="@android:style/Animation.Activity"
    android:layout_width="152dp"
    android:layout_height="129dp"
    android:layout_marginTop="360dp"
    android:layout_alignParentRight="true"
    android:background="@drawable/providers" />

<Button
    android:id="@+id/BoutonPieces"
    style="@android:style/Animation.Activity"
    android:layout_width="152dp"
    android:layout_height="129dp"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="120dp"
    android:background="@drawable/pieces" />

<Button
    android:id="@+id/BoutonTrucks"
    style="@android:style/Animation.Activity"
    android:layout_width="152dp"
    android:layout_height="129dp"
    android:layout_alignParentRight="true"
    android:layout_marginTop="240dp"       
    android:background="@drawable/trucks" />

<ListView
    android:id="@+id/ClientList"
    android:layout_width="453dp"
    android:layout_height="600dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true" >
</ListView>

</RelativeLayout>

Upvotes: 0

Views: 523

Answers (2)

Andy Res
Andy Res

Reputation: 16043

You declare your list array like this:

String[] listeStrings = new String[999];

Then you retrieve the server data like this:

listeStrings = getServerData(strURL);

My question is: Are you sure that after retrieving the server data the list contains exactly 999 elements?

I'm assuming that the getServerData() returns less elements than 999. In this case when you pass the listeString to the ArrayAdapter, the ArrayAdapter will try to fetch one by one all the records from the array, and at some time it will reach a location where the element will be null, resulting in a NullPointerException.

The solution would be to allocate for your string array as many locations as many elements are returned by your JSON array.
However, I would recommend use ArrayList instead of String[] arrays, they are allocated dinamically.

Upvotes: 0

glethien
glethien

Reputation: 2471

You've got an java.lang.NullPointerException in your code. Make sure you do not access any objects which are not initialized.

Means:

Object myObj; 
myObj.doSomething();

is will raise java.lang.NullPointerException

You need this order:

Object myObj;
myObj = new Object(); 
myObj.doSomething();

Without code I can't tell where this happens

Upvotes: 1

Related Questions