I am working on application that needs to get data from online MySQL database and show it as list in activity layout.
Problem is that my application gets error " Attempt to invoke interface method 'int java.util.List.size()' on a null object reference"
In onCreate method I am calling asyncTask class to get the data from server and than below that line of code I set that data through adapter to listview which is my layout. When starting that activity, I get the error.
Should I get the data from server before starting Activity? Or the problem is something else? My php file is working okay when I check it in browser.
Here is onCreate Method:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.invoice_list);
final ListView listView1 = (ListView) findViewById(R.id.invoicelist);
InvoiceDownloader task = new InvoiceDownloader();
task.execute(new String[] { "http://10.0.2.2/company/getinvoices.php" });
ArrayAdapter<Invoice> adapter = new ArrayAdapter<Invoice>(this, android.R.layout.simple_list_item_1, invoices);
listView1.setAdapter(adapter);
}
EDIT:
I fixed it as You all suggested, now there is no error, activity opens, but it still doesn't show any data from online database.. Here is edited onCreate() method:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.invoice_list);
adapter = new ArrayAdapter<Invoice>(this, android.R.layout.simple_list_item_1);
listView1 = (ListView) findViewById(R.id.invoicelist);
InvoiceDownloader task = new InvoiceDownloader();
task.execute(new String[] { "http://10.0.2.2/company/getinvoices.php" });
adapter.notifyDataSetChanged();
}
and here is AsyncTask class:
public class InvoiceDownloader extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
String response = "";
try {
response += getHttpContent(params[0]);
} catch (IOException e) {
Log.e("error", e.toString());
}
return response;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
adapter.addAll(invoices);
listView1.setAdapter(adapter);
Toast.makeText(getApplicationContext(), s, Toast.LENGTH_SHORT).show();
}
public String getHttpContent(String urlStr) throws IOException {
String response = "";
try {
URL url = new URL(urlStr);
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
StringBuilder sb = new StringBuilder();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpConn.getInputStream()));
String json;
while ((json = bufferedReader.readLine()) != null) {
sb.append(json + "\n");
}
JSONArray jsonArray = new JSONArray(sb.toString().trim());
for (int i = 0; i < jsonArray.length(); i++) {
Invoice invoice = new Invoice();
JSONObject inv = jsonArray.getJSONObject(i);
String companyNameOne = inv.getString("companyNameOne");
String companyNameTwo = inv.getString("companyNameTwo");
String address = inv.getString("address");
String postalCode = inv.getString("postalCode");
String city = inv.getString("city");
String oib = inv.getString("oib");
String email = inv.getString("email");
int comple = inv.getInt("completed");
String completed = String.valueOf(comple);
String workOrderNumber = inv.getString("workOrderNumber");
String price = inv.getString("price");
System.out.println("Company Name 1: " + companyNameOne + " Company Name 2: " + companyNameTwo + " address: " + address + " postalCode: " + postalCode + " city: " + city + " oib: " + oib + " email: " + email + " completed: " + completed + " work order num: " + workOrderNumber + " price: " + price);
invoice.setCompanyNameOne(companyNameOne);
invoice.setCompanyNameTwo(companyNameTwo);
invoice.setAddress(address);
invoice.setPostalCode(postalCode);
invoice.setCity(city);
invoice.setOib(oib);
invoice.setEmail(email);
invoice.setCompleted(completed);
invoice.setWorkOrderNumber(workOrderNumber);
invoice.setPrice(price);
invoices.add(invoice);
}
return response;
} catch (Exception e) {
return null;
}
}
}
2nd EDIT:
I got it. Sorry, this issue not showing data after fixing error was my mistake. In this part:
JSONObject inv = jsonArray.getJSONObject(i);
String companyNameOne = inv.getString("companyNameOne");
String companyNameTwo = inv.getString("companyNameTwo");
String address = inv.getString("address");
String postalCode = inv.getString("postalCode");
String city = inv.getString("city");
String oib = inv.getString("oib");
String email = inv.getString("email");
int comple = inv.getInt("completed");
String completed = String.valueOf(comple);
String workOrderNumber = inv.getString("workOrderNumber");
String price = inv.getString("price");
those strings in (" ") ALL had to be small letters, because I wrote them that way in my PHP.
Thank You all for Your help.