Jump to content

android studio:java.lang.NullPointerException


fouzia

Recommended Posts

Bonjour à tous,

je voudrais faire un spinner , quand je sélectionne une option et je clique sur le boutton l 'application m'affiche un graphe.

package com.example.lenovo.volley;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.JsonArrayRequest;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class first extends AppCompatActivity implements AdapterView.OnItemSelectedListener {

    String[] reportings= new String[]{"nombre de vose par personne","nombre de cantacts chaque personne",
            "nombre d'audits par personne","nombre devistes de chaque personne","nombre de mission de chaque personne"};
    Spinner reporting;
    //Button rechercher;
    BarData theData;
    BarChart barChart;
    private String urlJsonArry;

    //private Button btnMakeArrayRequest;
    String record="";

    // Progress dialog
    private ProgressDialog pDialog;
    private String jsonResponse;
    ArrayList<String> employes=new ArrayList<>();
    ArrayList<BarEntry> barEntries = new ArrayList<>();
    private static String TAG1 = first.class.getSimpleName();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first);
        //rechercher=(Button)findViewById(R.id.button);
        barChart=(BarChart)findViewById(R.id.barChart);
//        YAxis rightAxis = barChart.getAxisRight();
  //      rightAxis.setDrawGridLines(false);
    //    YAxis leftAxis = barChart.getAxisLeft();
      //  leftAxis.setDrawGridLines(false);


        pDialog = new ProgressDialog(this);
        pDialog = new ProgressDialog(this);
        pDialog.setMessage("Please wait...");
        pDialog.setCancelable(false);
        reporting=(Spinner)findViewById(R.id.spinner);
        ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,reportings);
        reporting.setAdapter(adapter);


        reporting.setOnItemSelectedListener(this);

    }
    public void rechercher(View view){
        showpDialog();

        JsonArrayRequest req = new JsonArrayRequest(urlJsonArry,
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {
                        Log.d(TAG1, response.toString());

                        try {
                            // Parsing json array response
                            // loop through each json object


                            for (int i = 0; i < response.length(); i++) {


                                JSONObject person = (JSONObject) response.get(i);
                                int Val=person.getInt("Val");
                                String user_name=person.getString("user_name");

                                employes.add(user_name);
                                barEntries.add(new BarEntry(Val,i));
                            }
                            BarDataSet barDataSet=new BarDataSet(barEntries,record);
                            theData = new BarData(employes,barDataSet);
                            barChart.setData(theData);
                            barChart.notifyDataSetChanged();
                            barChart.invalidate();


                        } catch (JSONException e) {
                            e.printStackTrace();
                            Toast.makeText(getApplicationContext(),
                                    "Error: " + e.getMessage(),
                                    Toast.LENGTH_LONG).show();
                        }

                        hidepDialog();
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                VolleyLog.d(TAG1, "Error: " + error.getMessage());
                Toast.makeText(getApplicationContext(),
                        error.getMessage(), Toast.LENGTH_SHORT).show();
                hidepDialog();
            }
        });

        // Adding request to request queue
        AppController.getInstance().addToRequestQueue(req);

    }

    private void showpDialog() {
        if (!pDialog.isShowing())
            pDialog.show();
    }

    private void hidepDialog() {
        if (pDialog.isShowing())
            pDialog.dismiss();
    }
    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
        switch (position)

        {

            case 0:

                record = "nombre de vose par personne";
                urlJsonArry="http://13.81.62.0/mobileapi/reporting?reportid=1";

                break;

            case 1:

                record = "nombre de cantacts chaque personne";
                urlJsonArry="http://13.81.62.0/mobileapi/reporting?reportid=2";

                break;

            case 2:

                record = "nombre d'audits par personne";
                urlJsonArry="http://13.81.62.0/mobileapi/reporting?reportid=3";

                break;
        }
    }

    @Override
    public void onNothingSelected(AdapterView<?> adapterView) {


    }
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.lenovo.volley.first">

    <LinearLayout
        android:layout_width="368dp"
        android:layout_height="495dp"
        android:orientation="vertical"
        tools:layout_editor_absoluteY="8dp"
        tools:layout_editor_absoluteX="8dp">

        <TextView
            android:id="@+id/textView3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.10"
            android:text="TextView" />

        <Spinner
            android:id="@+id/spinner"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.55" />

        <Button
            android:id="@+id/button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.10"
            android:onClick="rechercher"
            android:text="Button" />

        <com.github.mikephil.charting.charts.BarChart
            android:layout_width="match_parent"
            android:layout_height="363dp"></com.github.mikephil.charting.charts.BarChart>
    </LinearLayout>
</android.support.constraint.ConstraintLayout>

et voila l 'erreur :08-09 09:58:49.356 32742-32742/com.example.lenovo.volley E/AndroidRuntime: FATAL EXCEPTION: main
                                                                           Process: com.example.lenovo.volley, PID: 32742
                                                                           java.lang.NullPointerException: Attempt to invoke virtual method 'void com.github.mikephil.charting.charts.BarChart.setData(com.github.mikephil.charting.data.ChartData)' on a null object reference
                                                                               at com.example.lenovo.volley.first$1.onResponse(first.java:99)
                                                                               at com.example.lenovo.volley.first$1.onResponse(first.java:77)
                                                                               at com.android.volley.toolbox.JsonRequest.deliverResponse(JsonRequest.java:65)
                                                                               at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
                                                                               at android.os.Handler.handleCallback(Handler.java:739)
                                                                               at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                               at android.os.Looper.loop(Looper.java:158)
                                                                               at android.app.ActivityThread.main(ActivityThread.java:7231)
                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...