Khalid Bakry Khalid Bakry - 5 months ago 30
Android Question

ArrayAdapter and listview Error

I tried to setadapter for ther list view in the following code.

public class MainActivity extends AppCompatActivity {

JSONArray jsonObject;
ArrayList<String> arrayList= new ArrayList<String>();
static ArrayList<String> articles= new ArrayList<String>();
static ArrayAdapter<String> arrayAdapter;
public class Dwnl extends AsyncTask<String,Void,String> {

@Override
protected String doInBackground(String... strings) {
try {
URL url = new URL(strings[0]);

URLConnection conn=null;
conn = (HttpURLConnection)url.openConnection();
String contents="";

InputStream in = conn.getInputStream();
InputStreamReader reader = new InputStreamReader(in);
int data =reader.read();
int counter = 0;
while (data!=-1) {
contents+=(char)data;
data=reader.read();
//ounter++;
}
//contents = in.toString();
jsonObject= new JSONArray(contents);
//Log.i("Sire",String.valueOf(jsonObject.length()));
arrayList = new ArrayList<>();
//arrayList.addAll((Collection<? extends String>) jsonObject);
for(int i = 0 ; i < 3 ; i++) {

try {
//Log.i("URL", "https://hacker-news.firebaseio.com/v0/item/" + jsonObject.get(i) + ".json?print=pretty");

String site = "";
site = "https://hacker-news.firebaseio.com/v0/item/" + jsonObject.get(i) + ".json?print=pretty";
URL Second = new URL(site);
URLConnection newURlCon = (HttpURLConnection) Second.openConnection();
InputStream input = newURlCon.getInputStream();
InputStreamReader secondreader = new InputStreamReader(input);
int pos = secondreader.read();
String all = "";
while (pos != -1) {
all += (char) pos;
pos = secondreader.read();
}
//Log.i("JSON",all);
JSONObject second = new JSONObject(all);
all = "";

articles.add(second.getString("title").toString());
Log.i("JSONSTR",articles.get(i));

//arrayAdapter.notifyDataSetChanged();
}
catch (Exception e) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
Log.i("Exp",sw.toString());
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}

return null;
}
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

Dwnl dwnl = new Dwnl();
try {

dwnl.execute("https://hacker-news.firebaseio.com/v0/topstories.json?print=pretty").get();
ListView listView = (ListView) findViewById(R.id.listView1);
arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,articles);
listView.setAdapter(arrayAdapter);
}
catch (Exception e) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
Log.i("Exp",sw.toString());
}
}
}


but this error always appears


java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference


I dont know what is null ther arraylist is of size 3 and there is nothing null at all

Thanks in advance

Answer

The NullPointerException is thrown by the ListView, not by the array.
In onCreate method you should call:

setContentView(R.layout.your_layout);

Replace your_layout with your MainActivity xml.