Getting a NullPointer Exception when running this code:
public class MainActivity extends AppCompatActivity {
TextView textView;
String JSONstr;
String title;
JSONObject weatherInfo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textView);
weatherThread weather = new weatherThread();
weather.execute();
Log.d("LOL1","TESTING");
try {
weatherInfo=new JSONObject(JSONstr);
title=weatherInfo.getJSONObject("query").getString("created");
} catch (JSONException e) {
e.printStackTrace();
}
}
public class weatherThread extends AsyncTask<Void, Void, Void>{
@Override
protected Void doInBackground(Void... params) {
URL url;
URLConnection urlConnection;
InputStream inputStream;
BufferedReader bufferedReader;
try {
url=new URL("https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text%3D%2208852%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys");
urlConnection = url.openConnection();
inputStream = urlConnection.getInputStream();
bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
JSONstr=bufferedReader.readLine();
bufferedReader.close();
Log.d("TEST",JSONstr);
} catch (IOException e) {Log.d("ERROR",e.toString());}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
}
}
This is the link to the JSONObject
https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text%3D%2208852%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys
I tried looking at other posts but they had completely different code than mine. The error is coming up around the try catch statement.
Thank you in advance
Edit: Why did you mark this as a duplicate to a question about "What is a null pointer exception". This question is why is it showing up under specific circumstances.