FATAL EXCEPTION: main Process: nixerton.everemotequicktask, PID: 11611 java.lang.NullPointerException: Attempt to invoke interface method 'void com.android.okhttp.internal.http.HttpStream.writeRequestHeaders(com.android.okhttp.Request)' on a null object reference at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:593) at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:471) at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:407) at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponseMessage(HttpURLConnectionImpl.java:534) at com.android.tools.profiler.support.network.httpurl.TrackedHttpURLConnection.trackResponse(TrackedHttpURLConnection.java:86) at com.android.tools.profiler.support.network.httpurl.TrackedHttpURLConnection.tryTrackResponse(TrackedHttpURLConnection.java:101) at com.android.tools.profiler.support.network.httpurl.TrackedHttpURLConnection.getResponseCode(TrackedHttpURLConnection.java:308) at com.android.tools.profiler.support.network.httpurl.HttpURLConnection$.getResponseCode(HttpURLConnection$.java:92) at nixerton.everemotequicktask.EveAIORemote.PostRequest(EveAIORemote.java:30) at nixerton.everemotequicktask.EveAIORemote$1.onClick(EveAIORemote.java:54) at android.view.View.performClick(View.java:6294) at android.view.View$PerformClick.run(View.java:24770) at android.os.Handler.handleCallback(Handler.java:790) at android.os.Handler.dispatchMessage(Handler.java:99) 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)
I'm getting this Exception once my PostRequest is triggered by the button click. No idea of why this is occuring though. This is my code:
public class EveAIORemote extends AppCompatActivity {
TextView tv;
Button btn;
public void PostRequest(String url_from_text) throws IOException {
String url = url_from_text;
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("GET");
con.setRequestProperty("User-Agent", "Mozilla/5.0");
int responseCode = con.getResponseCode();
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
//print in String
System.out.println(response.toString());
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_eve_aioremote);
btn = (Button) findViewById(R.id.Button1);
tv = (TextView) findViewById(R.id.textView);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
PostRequest("apilink");
} catch (IOException e){
e.printStackTrace();
}
}
});
}
}
I didn't put my actual api link there as it contains several private informations, but I get the same error with other similar api links.