无法在空对象引用上处理错误java.lang.object.toString

时间:2018-04-06 18:55:16

标签: java android android-studio android-spinner dataadapter

我试图从我的服务器json数组响应中构建我的微调器数据。它' S 填充绝对正常,但是当我试图向下滚动时它会得到 坠毁了,我无法阻止它。除非微调器从服务器加载数据,否则它可以正常工作,但是一旦加载,如果我点击微调器就会发生错误。

请帮帮我。相当新的。这是我的代码

import android.app.DownloadManager;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ProgressBar;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

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

public class TeacherLand extends AppCompatActivity {

    String url ="https://www.myserver.com/Android/request.php?action=listTeacher&code=";
    String sCode;
    String sName;
    String[] name;
    String[] number;
    Spinner spinner;
    String url2;
    ProgressBar progressBar;
    JSONObject jsonObject;
    JSONArray jsonArray;
    ArrayAdapter<String> arrayAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_teacher_land);

        number = new String[100];
        name = new String[100];
        Intent intent = getIntent();
        try {
            sCode = intent.getExtras().getString("code");
            sName = intent.getExtras().getString("school");
            url2 = url + sCode;
        }
        catch (NullPointerException e)
        {
            new AlertDialog.Builder(TeacherLand.this)
                    .setTitle("ERROR !!")
                    .setMessage("Error Code = "+e.toString())
                    .setCancelable(false)
                    .setIcon(R.drawable.ic_logo)
                    .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.cancel();
                            //finishAffinity();
                            //System.exit(0);
                        }
                    }).show();
        }
        progressBar = (ProgressBar)findViewById(R.id.prog1);
        name[0] = "Select One";
        callNet(url2);
        spinner = (Spinner)findViewById(R.id.spinner);
        arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,name);
        arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_item);

        spinner.setAdapter(arrayAdapter);


      spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                                              @Override
                                              public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {

                                                  Object item = adapterView.getItemAtPosition(i);
                                                  if (item != null) {
                                                      try {
                                                          Toast.makeText(TeacherLand.this, item.toString() + " selected.",
                                                                  Toast.LENGTH_SHORT).show();
                                                      }
                                                      catch (NullPointerException e)
                                                      {
                                                          Toast.makeText(TeacherLand.this,"Error Block",Toast.LENGTH_SHORT).show();
                                                      }
                                                      }
                                              }

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

                                              }
                                          }

        );

    }

    public void callNet(String url2)
    {

        progressBar.setVisibility(View.VISIBLE);
        StringRequest stringRequest = new StringRequest(Request.Method.GET, url2,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {

                        progressBar.setVisibility(View.GONE);
                        try
                        {
                            jsonArray = new JSONArray(response);
                            for(int i=0;i<jsonArray.length();i++)
                            {

                                jsonObject = jsonArray.getJSONObject(i);
                                name[i] = jsonObject.getString("StaffName");
                                number[i] = jsonObject.getString("StaffContact");
                            }
                            //jsonObject = new JSONObject(response);

                        }
                        catch (JSONException e)
                        {


                            new AlertDialog.Builder(TeacherLand.this)
                                    .setTitle("Fatal Error")
                                    .setMessage("Bad luck.Please call us.And mention error code = 901")
                                    .setCancelable(false)
                                    .setIcon(R.drawable.ic_logo)
                                    .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {
                                            dialog.cancel();
                                            //finishAffinity();
                                            //System.exit(0);
                                        }
                                    }).show();
                        }



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

                new AlertDialog.Builder(TeacherLand.this)
                        .setTitle("Timeout Error")
                        .setMessage("Check your internet connection.")
                        .setCancelable(false)
                        .setIcon(R.drawable.ic_logo)
                        .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.cancel();
                                //finishAffinity();
                                //System.exit(0);
                            }
                        }).show();
            }
        }
        );


        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(stringRequest);
    }

}

这是错误截图enter image description here

0 个答案:

没有答案