I need that my ArrayAdapter will be loaded with data coming of HTTP Request.
My onCreate method:
private Spinner spiCities;
private ArrayAdapter<String> citiesAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new Thread() {
@Override
public void run() {
LoadCities lc = new LoadCities();
lc.execute();
}
}.start();
}
I have a inner class in Activity:
class LoadCities extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void... voids) {
BufferedReader br = null;
try {
URL url = new URL("http://10.0.2.2/inter/api/loadCities.php");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
if ( con.getResponseCode() != 200 ) {
throw new RuntimeException( "Error: " + con.getResponseMessage() );
}
br = new BufferedReader( new InputStreamReader( con.getInputStream() ) );
} catch ( MalformedURLException e ) {
e.printStackTrace();
} catch ( IOException e ) {
e.printStackTrace();
}
if ( null == br ) {
return "[]";
}
else {
StringBuilder sb = new StringBuilder();
try {
String linha;
while ( (linha = br.readLine()) != null ) {
sb.append( linha );
}
} catch (IOException e) {
e.printStackTrace();
}
return sb.toString();
}
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
s = s.substring(1);
s = s.substring(0, s.length()-1);
List<String> listCities = new ArrayList<>();
String s1;
for ( String aux : s.split(",") ) {
s1 = aux.substring(1);
s1 = s1.substring(0, s1.length()-1);
listaCidades.add(s1);
}
citiesAdapter = new ArrayAdapter<String>( context, android.R.layout.simple_spinner_dropdown_item, listCities);
citiesAdapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
spiCities.setAdapter( citiesAdapter );
}
}
The error says that the "citiesAdapter" object is null:
FATAL EXCEPTION: main Process: br.com.interativa, PID: 21386 java.lang.RuntimeException: Unable to start activity ComponentInfo{br.com.interativa/br.com.interativa.FiltrarEventoActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.Object.equals(java.lang.Object)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) at android.app.ActivityThread.-wrap11(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6494) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.Object.equals(java.lang.Object)' on a null object reference at br.com.inter.FilterEventActivity.loadCities(FilterEventActivity.java:183)
What am I doing wrong?
EDIT
I researched a lot here in the forum before posting my question and really did not find anything like it!
My problem was marked as duplicate of another post that asks ONLY about Null Pointer in basic and pure Java. My problem has nothing to do with simple Null Pointer and yes with JSON values coming through a Request / Respose within the Android Async.