I am new to Android making and app to get data from Oxford dictionary API, but every time when I hit the search button, the app crashes and an error popup is shown in the console window.
Here is the code
Fragment
public class Student4 extends Fragment {
String url;
Button getresponse;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.student4, container, false);
url = dictionaryEntries();
getresponse=(Button)view.findViewById(R.id.button);
getresponse.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
MyDictionaryRequest myDictionaryRequest = new
MyDictionaryRequest(getActivity());
myDictionaryRequest.execute(url);
}
});
return view;
}
private String dictionaryEntries() {
final String language = "en";
final String word = "Ace";
final String word_id = word.toLowerCase(); //word id is case sensitive and lowercase is required
return "https://od-api.oxforddictionaries.com:443/api/v1/entries/" + language + "/" + word_id;
}
}
Request class
public class MyDictionaryRequest extends AsyncTask<String,Integer,String> {
final String app_id = "091b8935";
final String app_key = "5decabd8eaef02bb10c7219c07481bb0";
String myUrl;
Context context;
public View view;
MyDictionaryRequest(Context context){
this.context = context;
}
@Override
protected String doInBackground(String... params) {
myUrl = params[0];
try {
URL url = new URL(myUrl);
HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();
urlConnection.setRequestProperty("Accept","application/json");
urlConnection.setRequestProperty("app_id",app_id);
urlConnection.setRequestProperty("app_key",app_key);
// read the output from the server
BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
StringBuilder stringBuilder = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
stringBuilder.append(line + "\n");
}
return stringBuilder.toString();
}
catch (Exception e) {
e.printStackTrace();
return e.toString();
}
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
String def;
try {
JSONObject jsonObject = new JSONObject(s);
JSONArray results = jsonObject.getJSONArray("results");
JSONObject lEnteries = results.getJSONObject(0);
JSONArray laArray = lEnteries.getJSONArray("lexicalEntries");
JSONObject entries = laArray.getJSONObject(0);
JSONArray entriesArray = entries.getJSONArray("entries");
JSONObject senses = entriesArray.getJSONObject(0);
JSONArray sensesArray = senses.getJSONArray("senses");
JSONObject definations = sensesArray.getJSONObject(0);
JSONArray definationsArray = definations.getJSONArray("definitions");
def = definationsArray.getString(0);
>> error TextView t1 = (TextView)view.findViewById(R.id.view);
t1.setText(def);
Toast.makeText(context, def, Toast.LENGTH_SHORT).show();
} catch (JSONException e) {
e.printStackTrace();
}
}
}
Errorr
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.getRootView()' on a null object reference
at netscope.mango.educy.MyDictionaryRequest.onPostExecute(MyDictionaryRequest.java:90)
at netscope.mango.educy.MyDictionaryRequest.onPostExecute(MyDictionaryRequest.java:23)
at android.os.AsyncTask.finish(AsyncTask.java:636)
at android.os.AsyncTask.access$500(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:653)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:210)
at android.app.ActivityThread.main(ActivityThread.java:5839)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1113)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:879)
In request class when I try to set my response to textview, I am getting view=null this is the problem. Please help me. Thank you.