IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Android Discussion :

Problème NullPointerException


Sujet :

Android

  1. #1
    Futur Membre du Club
    Femme Profil pro
    Étudiant
    Inscrit en
    Mars 2015
    Messages
    7
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Mars 2015
    Messages : 7
    Points : 8
    Points
    8
    Par défaut Problème NullPointerException
    Salut , j'essaye d'ajouter des données dans ma base de données a travers android mais j'ai un problème lors de l’envoi voila le code j'ai utiliser et le logcat
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
     
      protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_carte);
     
     
            nomt = (EditText) findViewById(R.id.editText8);
            prénomt = (EditText) findViewById(R.id.editText10);
            comptet = (EditText) findViewById(R.id.editText11);
            idt= (EditText)findViewById(R.id.editText12);
            // Create button
            Button btnCreateProduct = (Button) findViewById(R.id.button6);
     
            // button click event
            btnCreateProduct.setOnClickListener(new View.OnClickListener() {
     
                @Override
                public void onClick(View view) {
                    // creating new product in background thread
                    new CreateNewProduct().execute();
                }
            });
        }
     
        /**
         * Background Async Task to Create new product
         * */
        class CreateNewProduct extends AsyncTask<String, String, String> {
     
            /**
             * Before starting background thread Show Progress Dialog
             * */
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                pDialog = new ProgressDialog(Cartes.this);
                pDialog.setMessage("Creating Product..");
                pDialog.setIndeterminate(false);
                pDialog.setCancelable(true);
                pDialog.show();
            }
     
            /**
             * Creating product
             * */
            protected String doInBackground(String... args) {
                String nom = nomt.getText().toString();
                String prenom = prénomt.getText().toString();
                String compte = comptet.getText().toString();
                String id = idt.getText().toString();
     
     
                // Building Parameters
                List<NameValuePair> params = new ArrayList<NameValuePair>();
                params.add(new BasicNameValuePair("nom", nom));
                params.add(new BasicNameValuePair("prenom", prenom));
                params.add(new BasicNameValuePair("compte", compte));
                params.add(new BasicNameValuePair("id", id));
     
     
                // getting JSON Object
                // Note that create product url accepts POST method
                JSONObject json = jsonParser.makeHttpRequest(url_create_product,
                        "POST", params);
     
                // check log cat fro response
                Log.d("Create Response", json.toString());
     
                // check for success tag
     
     
                return null;
            }
     
            /**
             * After completing background task Dismiss the progress dialog
             * **/
            protected void onPostExecute(String file_url) {
                // dismiss the dialog once done
                pDialog.dismiss();
            }
     
        }
    }
    le log cat
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
     
    04-30 18:39:59.654    6155-6719/attijari.com.attijarimobileapp E/JSON Parser﹕ Error parsing data org.json.JSONException: Value OK of type java.lang.String cannot be converted to JSONObject
    04-30 18:39:59.694    6155-6719/attijari.com.attijarimobileapp E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #2
        java.lang.RuntimeException: An error occured while executing doInBackground()
                at android.os.AsyncTask$3.done(AsyncTask.java:299)
                at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
                at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
                at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
                at java.util.concurrent.FutureTask.run(FutureTask.java:137)
                at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
                at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
                at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
                at java.lang.Thread.run(Thread.java:856)
         Caused by: java.lang.NullPointerException
                at attijari.com.attijarimobileapp.Cartes$CreateNewProduct.doInBackground(Cartes.java:160)
                at attijari.com.attijarimobileapp.Cartes$CreateNewProduct.doInBackground(Cartes.java:121)
                at android.os.AsyncTask$2.call(AsyncTask.java:287)
                at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
    ************at java.util.concurrent.FutureTask.run(FutureTask.java:137)
    ************at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
    ************at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
    ************at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
    ************at java.lang.Thread.run(Thread.java:856)
    04-30 18:40:07.892    6155-6155/attijari.com.attijarimobileapp E/WindowManager﹕ Activity attijari.com.attijarimobileapp.Cartes has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@419fd918 that was originally added here
        android.view.WindowLeaked: Activity attijari.com.attijarimobileapp.Cartes has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@419fd918 that was originally added here
                at android.view.ViewRootImpl.<init>(ViewRootImpl.java:415)
                at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:322)
                at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:234)
                at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:153)
                at android.view.Window$LocalWindowManager.addView(Window.java:559)
                at android.app.Dialog.show(Dialog.java:301)
                at attijari.com.attijarimobileapp.Cartes$CreateNewProduct.onPreExecute(Cartes.java:133)
                at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
                at android.os.AsyncTask.execute(AsyncTask.java:534)
                at attijari.com.attijarimobileapp.Cartes$1.onClick(Cartes.java:113)
                at android.view.View.performClick(View.java:4262)
                at android.view.View$PerformClick.run(View.java:17421)
                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:4944)
                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:1038)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
                at dalvik.system.NativeStart.main(Native Method)

  2. #2
    Membre régulier
    Homme Profil pro
    Étudiant
    Inscrit en
    Juillet 2014
    Messages
    64
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 33
    Localisation : France, Ille et Vilaine (Bretagne)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Juillet 2014
    Messages : 64
    Points : 96
    Points
    96
    Par défaut
    Bonsoir,

    à priori ton problème vient soit de ta méthode "makeHttpRequest", soit de ta page php te renvoyant ok si tout va bien et autre chose si ça va mal...

  3. #3
    Expert éminent

    Homme Profil pro
    Ingénieur systèmes et réseaux
    Inscrit en
    Février 2007
    Messages
    4 253
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Ingénieur systèmes et réseaux
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Février 2007
    Messages : 4 253
    Points : 7 618
    Points
    7 618
    Billets dans le blog
    3
    Par défaut
    Allez... on va prendre le truc point par point:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
    Caused by: java.lang.NullPointerException
                at attijari.com.attijarimobileapp.Cartes$CreateNewProduct.doInBackground(Cartes.java:160)
    Donc, c'est ligne 160 de Cartes.java... dans la fonction doInBackground de la classe fille "CreateNewProduct" de la classe "Cartes"
    (premier conseil: utiliser des noms de classes qui permettent de savoir instantannément le type d'objet... par exemple: CreateNewProductTask.... CartesActivity ).

    On n'a pas tout le code, mais j'ai reçu des piles pour ma boule de cristal:

    Ce doit être ici:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    Log.d("Create Response", json.toString());
    La raison étant que "json" est null..... => json.toString() => NullPointerException.

    L'objet en question est reçu la ligne d'avant:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    JSONObject json = jsonParser.makeHttpRequest(url_create_product, "POST", params);
    Quels sont les post-conditions de cette fonction ? (makeHttpRequest).
    Vue le type de retour, je dirais que la post-condition est d'avoir un objet JSON valide, donc json ne devrait *jamais* être null...
    Mais dans ce cas... comment sont remontées les erreurs ? (pas de réseau, JSON mal formatté en retour, serveur innaccessible, timeout car réseau pourri ... ) ? A priori par une exception... mais aucune exception n'est gérée dans le code.

    Donc j'en arrive aux conclusions que:
    il y a un problème avec le JSON (cf la ligne de log juste avant l'exception).
    mais que la fonction makeHttpRequest ne remontent pas les exceptions nécessaires (IOException, JSONException pour ne parler que d'elles), ou ne vérifient pas les pré-conditions (l'url est correcte...).
    N'oubliez pas de cliquer sur mais aussi sur si un commentaire vous a été utile !
    Et surtout

Discussions similaires

  1. Problème NullPointerException
    Par Laurette8 dans le forum Débuter avec Java
    Réponses: 14
    Dernier message: 15/05/2015, 14h17
  2. Réponses: 15
    Dernier message: 13/02/2015, 17h04
  3. Problème NullPointerException en POST mais pas en GET
    Par Tonyo38 dans le forum Servlets/JSP
    Réponses: 15
    Dernier message: 03/07/2014, 09h35
  4. Comment résoudre le problème : NullPointerException
    Par ABS_AGH dans le forum Débuter avec Java
    Réponses: 2
    Dernier message: 09/01/2014, 15h56
  5. [JNI] Problème NullPointerException
    Par jabesq dans le forum Entrée/Sortie
    Réponses: 0
    Dernier message: 08/07/2009, 10h22

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo