Click here to Skip to main content
15,905,232 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I have made a JSON Parser class and trying to fetch data from northwind web services customers tables in my andriod app. but i am getting a null pointer exception in JSON array object. I have tried the same code for different url but it works fine..

Any help will be highly appreciable. My code goes below.

Thank you in advance.

What I have tried:

MainActivity.java
package com.example.gaurav.catalog;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

private TextView t1;
ProgressBar pg1;
List task;
List flowerlist;

List customerlist;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

t1 = (TextView) findViewById(R.id.textView);
t1.setMovementMethod(new ScrollingMovementMethod());

pg1 = (ProgressBar) findViewById(R.id.progressBar);
pg1.setVisibility(View.INVISIBLE);

task = new ArrayList<>();
// for (int i = 0; i {

@Override
protected void onPreExecute() {
// updateDisplay("Starting Task");
if (task.size() == 0) {
pg1.setVisibility(View.VISIBLE);
}
task.add(this);
}

@Override
protected String doInBackground(String... params) {
// for (int i = 0; i < params.length; i++) {
// publishProgress("Executing task" + params[i]);
// try {
// Thread.sleep(1000);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// }
String content = HttpManager.getData(params[0]);
return content;
}


@Override
protected void onPostExecute(String s) {
// flowerlist = FlowerJSONParser.parseFeed(s);
customerlist = CustomerJSONParser.parseFeed(s);
updateDisplay();
// updateValue();
task.remove(this);
if (task.size() == 0) {
pg1.setVisibility(View.INVISIBLE);
}
}

@Override
protected void onProgressUpdate(String... values) {
// updateDisplay(values[0]);
}
}
}
-----------------------------------------------------------
JSOPNPARSER.java

package com.example.gaurav.catalog;

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

import java.util.ArrayList;
import java.util.List;

public class CustomerJSONParser {
public static List parseFeed(String content) {
try {
JSONArray ar = new JSONArray(content);
List customers = new ArrayList<>();

for (int i = 0; i < ar.length(); i++) {
JSONObject obj = ar.getJSONObject(i);
Customer customer = new Customer();
customer.setCustomerid(obj.getString("customerId"));
customer.setCompanyname(obj.getString("companyname"));
customer.setContact(obj.getString("contact"));
customer.setAddress(obj.getString("address"));
customer.setCity(obj.getString("city"));
customer.setPostalcode(obj.getString("postalcode"));
customer.setCountry(obj.getString("country"));
customer.setPhone(obj.getString("phone"));
customer.setFax(obj.getString("fax"));
customer.setRegion(obj.getString("region"));
customers.add(customer);
}
return customers;
} catch (JSONException e) {
e.printStackTrace();
return null;
}
}
}
-----------------------------------------
Customer.java

package com.example.gaurav.catalog;

public class Customer {
    private String customerid;
    private String companyname;
    private String contact;
    private String contacttitle;
    private String address;
    private String city;
    private String postalcode;
    private String country;
    private String phone;
    private String fax;
    private String region;

    public String getCustomerid() {
        return customerid;
    }

    public void setCustomerid(String customerid) {
        this.customerid = customerid;
    }

    public String getCompanyname() {
        return companyname;
    }

    public void setCompanyname(String companyname) {
        this.companyname = companyname;
    }

    public String getContact() {
        return contact;
    }

    public void setContact(String contactname) {
        this.contact = contact;
    }

    public String getContacttitle() {
        return contacttitle;
    }

    public void setContacttitle(String contacttitle) {
        this.contacttitle = contacttitle;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getPostalcode() {
        return postalcode;
    }

    public void setPostalcode(String postalcode) {
        this.postalcode = postalcode;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }
    public String getFax() {
        return fax;
    }

    public void setFax(String fax) {
        this.fax = fax;
    }
    public String getRegion() {
        return region;
    }

    public void setRegion(String region) {
        this.region = region;
    }


}
Posted
Comments
Richard MacCutchan 21-Mar-17 5:24am    
Please format all your code properly, and also explain exactly where the error occurs.
David Crow 6-Jun-17 20:15pm    
In addition to Richard's comment, I would suggest removing all but the relevant code. Unnecessary code just gets in the way.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900