尝试在空对象引用(旋转器)上调用虚拟方法'java.lang.String java.lang.Object.toString()'

时间:2018-12-13 15:58:35

标签: java android

我有此代码

//get Info from Json 
    ic_Spe_categorie = findViewById(R.id.ic_Spe_categorief);

    StringRequest request = new StringRequest(Request.Method.GET, URL, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            final HashMap<String,Integer> mapList=new HashMap<>();

            try {
                JSONObject source = new JSONObject(response);
                JSONArray path = source.getJSONArray("hotel");

                Spe_categorie = new String[path.length()];

                for (int i = 0; i < path.length(); i++){
                    JSONObject object = path.getJSONObject(i);
                    if(object.getString("hotel_id").equals("100972")){

                        String categorie = object.getString("categorie_chambre_name");



                        Spe_categorie[i] = categorie ;

                        Log.d("categorie", object.getString("categorie_chambre_name"));

                        ArrayAdapter arrayAdapter_cat = new ArrayAdapter(SearchHotelActivity.this, android.R.layout.simple_spinner_item , Spe_categorie);
                        arrayAdapter_cat.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                        ic_Spe_categorie.setAdapter(arrayAdapter_cat);
                    }



                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }
    }) ;
    queue.add(request);

im尝试使用json REST API从数据库中的行(categorie_chambre_name)填充微调框(ic_Spe_categorie)

如果我不使用if语句,则

spinner实际上填满了所有行(categorie_chambre_name)

我想要的是链接到我的酒店ID(100972)的行(categorie_chambre_name)

当我尝试提出一个条件,如果hotel_id等于我的酒店ID(100972)已存在于数据库中

给我这个错误

E/AndroidRuntime: FATAL EXCEPTION: main
Process: ma.transatour.transatourmaroc, PID: 12734
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
    at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:453)
    at android.widget.ArrayAdapter.getView(ArrayAdapter.java:415)
    at android.widget.AbsSpinner.onMeasure(AbsSpinner.java:204)
    at android.widget.Spinner.onMeasure(Spinner.java:602)
    at android.support.v7.widget.AppCompatSpinner.onMeasure(AppCompatSpinner.java:426)
    at android.view.View.measure(View.java:23169)
    at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
    at android.view.View.measure(View.java:23169)
    at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:1308)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
    at android.widget.ScrollView.onMeasure(ScrollView.java:350)
    at android.view.View.measure(View.java:23169)
    at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
    at android.view.View.measure(View.java:23169)
    at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
    at android.view.View.measure(View.java:23169)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
    at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
    at android.view.View.measure(View.java:23169)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749)
    at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:400)
    at android.view.View.measure(View.java:23169)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
    at android.view.View.measure(View.java:23169)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749)
    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1535)
    at android.widget.LinearLayout.measureVertical(LinearLayout.java:825)
    at android.widget.LinearLayout.onMeasure(LinearLayout.java:704)
    at android.view.View.measure(View.java:23169)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
    at com.android.internal.policy.DecorView.onMeasure(DecorView.java:716)
    at android.view.View.measure(View.java:23169)
    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2718)
    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1572)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1855)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1460)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7183)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:949)
    at android.view.Choreographer.doCallbacks(Choreographer.java:761)
    at android.view.Choreographer.doFrame(Choreographer.java:696)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:935)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6669)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

1 个答案:

答案 0 :(得分:0)

您可能需要检查JSON对象是否具有“ hotel_id”键。只需放置调试点或使用has进行检查,即可使用它。