尝试在空对象引用上调用接口方法<myinterfacemethord>

时间:2017-02-19 03:11:06

标签: java android json

我刚接触Android和Im开发一个试图连接服务器并获取Json响应的应用程序,将其解析为一个类并让它可用。

我已经为这个

提出了3个课程
  1. public class Customer {}
  2. 公共类ServerConnect {}
  3. public class JsonParser {}
  4. Serverconnect使用Volley库在Async线程上下载网页源。下载后,Server Connect类将激活onServerResponded(String responce)列表器。这个响应是JsonParser类的输入。在JsonParser类成功获取客户列表后,它会触发另一个名为onCustomersParsed(列出客户)的列表器。稍后处理在onCustomersParsed回调中完成。

    但我得到致命的异常......

      

    尝试调用接口方法'void   app.utilities.JsonParser $ OnCustomersParserListner.onCustomersParsed()”   在空对象引用上

    我尽了最大努力,但我失败了。这是我的代码

    package app.elements;
    
    /**
     * Created by Sangeeth Nandakumar on 13-02-2017.
     */
    
    public class Customer
    {
        private String id;
        private String firstname;
        private String lastname;
        private String device;
        private String gender;
    
        public Customer(String id, String firstname, String lastname, String device,String gender)
        {
            this.id = id;
            this.firstname = firstname;
            this.lastname = lastname;
            this.device = device;
            this.gender = gender;
        }
    
        public String getGender() {
            return gender;
        }
    
        public void setGender(String gender) {
            this.gender = gender;
        }
    
        public String getId() {
            return id;
        }
    
        public void setId(String id) {
            this.id = id;
        }
    
        public String getFirstname() {
            return firstname;
        }
    
        public void setFirstname(String firstname) {
            this.firstname = firstname;
        }
    
        public String getLastname() {
            return lastname;
        }
    
        public void setLastname(String lastname) {
            this.lastname = lastname;
        }
    
        public String getDevice() {
            return device;
        }
    
        public void setDevice(String device) {
            this.device = device;
        }
    }
    

    这是我的服务器连接器类

    package app.utilities;
    
    import android.content.Context;
    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;
    
    /**
     * Created by Sangeeth Nandakumar on 18-02-2017.
     * Highly optimised Google's Volley driven async webpage source downloader
     */
    
    public class ServerConnector
    {
        //Member variables
        private Context context;
    
        //Override interfaces
        public interface OnServerStatusListner
        {
            public void onServerResponded(String responce);
            public void onServerRevoked();
        }
    
        //New listner
        private OnServerStatusListner listner;
    
        //Listner function
        public void setOnServerStatusListner(OnServerStatusListner myststus)
        {
            listner=myststus;
        }
    
        //Constructor
        public ServerConnector(Context context)
        {
            this.context = context;
        }
    
        //Connect to server
        public void connectServer(String url)
        {
            //Create a request queue
            RequestQueue queue= Volley.newRequestQueue(context);
            //Tunnel the request
            StringRequest stringRequest=new StringRequest(Request.Method.GET, url,
                    //When request success
                    new Response.Listener<String>()
                    {
                        @Override
                        public void onResponse(String response)
                        {
                            listner.onServerResponded(response);
                        }
                    },
                    //When request fails
                    new Response.ErrorListener()
                    {
                        @Override
                        public void onErrorResponse(VolleyError error)
                        {
                            listner.onServerRevoked();
                        }
                    });
            //Add the request to queue
            queue.add(stringRequest);
        }
    
    }
    

    这是我的JSON-PARSER CLASS

    package app.utilities;
    
    import android.content.Context;
    import android.widget.Toast;
    import org.json.JSONArray;
    import org.json.JSONException;
    import org.json.JSONObject;
    import java.util.ArrayList;
    import java.util.List;
    import app.elements.Customer;
    
    /**
     * Created by Sangeeth Nandakumar on 18-02-2017.
     * Highly optimised Json parserclass with multiple listners
     */
    
    public class JsonParser
    {
        //Data members
        private Context context;
        private String jsontext;
        private List<Customer> customers=new ArrayList<>();
    
        //Listner object
        public OnCustomersParserListner listner;
    
        //Listners
        public interface OnCustomersParserListner
        {
            public void onCustomersParsed(List<Customer> customers);
        }
    
        //On Jsonparse listner
        public void setOnJsonParseListner(OnCustomersParserListner mylistner)
        {
            listner=mylistner;
        }
    
        //Constructor
        public JsonParser(Context context, String jsontext)
        {
            this.context = context;
            this.jsontext = jsontext;
        }
    
        //Json pre-parsing
        public void parseCustomers()
        {
            // If json is true
            if (jsontext != null)
            {
                try
                {
                    //Create a JSON object
                    JSONObject jsonObj = new JSONObject(jsontext);
                    // Getting JSON Array node
                    JSONArray json = jsonObj.getJSONArray("details");
                    // Looping through All Contacts
                    for (int i = 0; i < json.length(); i++)
                    {
                        JSONObject details = json.getJSONObject(i);
                        //Get all customer details from JSON file
                        String id = details.getString("id");
                        String firstname = details.getString("firstname");
                        String lastname = details.getString("lastname");
                        String device = details.getString("device");
                        String gender = details.getString("gender");
                        //Add to list
                        customers.add(new Customer(id,firstname,lastname,device,gender));
                    }
                    listner.onCustomersParsed(customers);
                }
                catch (final JSONException e)
                {
                    Toast.makeText(context, "Error in parsing JSON", Toast.LENGTH_SHORT).show();
                }
            }
            else
            {
                Toast.makeText(context, "JSON text is empty", Toast.LENGTH_SHORT).show();
            }
        }
    
    }
    

    这是主要活动的来电

    ServerConnector server=new ServerConnector(this);
    server.connectServer("http://amazinginside.esy.es/customerlist.php");
    
    server.setOnServerStatusListner(new ServerConnector.OnServerStatusListner()
    {
        @Override
        public void onServerResponded(String responce)
        {
            Toast.makeText(MainActivity.this, "Server responded",      Toast.LENGTH_SHORT).show();
            JsonParser parser=new JsonParser(getApplicationContext(),responce);
            parser.parseCustomers();
            parser.setOnJsonParseListner(new JsonParser.OnCustomersParserListner() {
                @Override
                public void onCustomersParsed(List<Customer> customers) {
                    Toast.makeText(MainActivity.this, customers.get(0).getFirstname(), Toast.LENGTH_SHORT).show();
                }
            });
    
        }
        @Override
        public void onServerRevoked()
        {
            Toast.makeText(MainActivity.this, "Connection to server broke", Toast.LENGTH_SHORT).show();
        }
    });
    

    任何人都请帮助我

1 个答案:

答案 0 :(得分:0)

您正在设置侦听器,然后呼叫parsing。尝试更改订单。

@Override
public void onServerResponded(String responce)
{
    Toast.makeText(MainActivity.this, "Server responded",      Toast.LENGTH_SHORT).show();
    JsonParser parser=new JsonParser(getApplicationContext(),responce);
    parser.setOnJsonParseListner(new JsonParser.OnCustomersParserListner() {
        @Override
        public void onCustomersParsed(List<Customer> customers) {
            Toast.makeText(MainActivity.this, customers.get(0).getFirstname(), Toast.LENGTH_SHORT).show();
        }
    });
    parser.parseCustomers();

}