0

So we are working on this app in Android Studio where we want to make a get request to a website, and when we run this piece of code, we keep getting an error of "null" which I believe to be so because one of the variables in this piece of code is null. Can someone look it over and see any places where you may detect some variable is not being used correctly and therefore providing a null error?

public class SpotAlgo {
String vidLink;
int linkLoc;
String testString = "<title>";
String result;

public String gettheResult(String v) throws Exception{
    String sname = " ";
    vidLink = "https://open.spotify.com/track/43PuMrRfbyyuz4QpZ3oAwN";

    URL obj = new URL(vidLink);
    HttpURLConnection con = (HttpURLConnection) obj.openConnection();
    con.setRequestMethod("GET");
    BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
    String inputLine = "";
    StringBuffer response = new StringBuffer();

    while ((inputLine = in.readLine()) != null) {
        response.append(inputLine);
    }
    in.close();

    result = response.toString();


    linkLoc = result.indexOf(testString) + testString.length();
    for (int i = linkLoc; i < result.indexOf("on Spotify"); i++) {
        sname += result.charAt(i) + "";
    }
    return obj.toString();
}

}

Ken Y-N
  • 14,644
  • 21
  • 71
  • 114
  • 2
    Provice a stacktrace please. – Ezequiel Nov 24 '16 at 01:20
  • So, what happens if the returned text doesn't contain `"on Spotify"`, such as if the URL returns a 404 error? Please, as mentioned above, supply your LogCat so we can see what the error is. – Ken Y-N Nov 24 '16 at 01:34
  • The stacktrace is: Failed to set EGL_SWAP_BEHAVIOR on surface 0xa8d63f60, error=EGL_BAD_MATCH – Programmer87 Nov 24 '16 at 02:01
  • @enzokie are u referring to the website itself that ive provided? When I run it through postman I am getting a proper response – Programmer87 Nov 24 '16 at 02:03
  • @Enzokie HEAD request returns 200 for me... Anyways, Programmer87, Volley / OkHttp may be better HTTP libraries for your needs. – OneCricketeer Nov 24 '16 at 02:08
  • Yeah it is a 200 request for me now, probably I did use a wrong request. – Enzokie Nov 24 '16 at 02:09
  • @enzokie so do you see any other sources of problem? – Programmer87 Nov 24 '16 at 02:16
  • @cricket_007 point taken. But I would rather use this connection toolset now because I feel more familiar with it – Programmer87 Nov 24 '16 at 02:17
  • Do you have the proper permission in your manifest? – Enzokie Nov 24 '16 at 02:18
  • @enzokie what do you mean by that? Sorry I am a bit new to android programming – Programmer87 Nov 24 '16 at 02:19
  • @Programmer87 [Mainfest permission](http://stackoverflow.com/questions/2169294/how-to-add-manifest-permission-to-android-application#2169311). You also need an AsyncTask to even run this code off the UI Thread. – OneCricketeer Nov 24 '16 at 02:21
  • And you are parsing HTML? So, look into JSoup. `indexOf("")` does not seem reliable. – OneCricketeer Nov 24 '16 at 02:24
  • @cricket_007 Realized that internet permission wasnt there and I added that... Nothing is being returned now instead of just a null statement – Programmer87 Nov 24 '16 at 02:37
  • Okay... where is null returned? It is `"null"` (a string), `null` (no object), or NullPointerException, an issue with your exception catching? As Nobody pointed out below, the code seems to work with what you provided, so unless you provide the stacktrace and a [mcve], we cannot help anymore – OneCricketeer Nov 24 '16 at 02:40
  • @cricket_007 I think it may actually be an issue with the emulator itself. How would I enable the emulator to have internet access on Windows? – Programmer87 Nov 24 '16 at 03:24
  • Should have access by default. I use Genymotion, though, so not sure – OneCricketeer Nov 24 '16 at 03:35

2 Answers2

0

Nothing, it works fine!

I believe that if you have a null pointer exception, it is related to another part of your code.

However, here are some suggestions to improve it

In this snippet you add text to the sname variable in a loop. When you do that kind of operation a StringBuilder will be more efficient.

for (int i = linkLoc; i < result.indexOf("on Spotify"); i++) {
   sname += result.charAt(i) + "";
}
//Could be replaced b
for (int i = linkLoc; i < result.indexOf("on Spotify"); i++) {
    sb.append(result.charAt(i));
}

You can later use sb.toString() to get the result of the operation.

Also, more importantly, when you do network operation you never know what the result will be. There can be many variables that will inpact the result you will get, and often exceptions will be thrown. It is important that you wrap the network code in a try { } finally {}, and that you close the ressources you open in the finally block (a finally block always execute).

edit Fixed a typo

edit 2

Some people are saying it is throwing a 404 exception, I did not get one when I ran it on my machine, and a 404 would throw an IOException when you do con.getInputStream(), so you would likely see that instead

Samuel Yvon
  • 416
  • 5
  • 13
-1

You missed actually connecting

con.setRequestMethod("GET");
con.connect();

Your con.getInputStream(); is null since the connection is not made and nothing is returned to con.

Luminous_Dev
  • 614
  • 6
  • 14
  • There is no need for you to explicitly call `con.connect()` because it is implicitly invoked the moment you use `getInputStream` or `getOutputStream()`. For more info see this [post](http://stackoverflow.com/questions/16122999/java-urlconnection-when-do-i-need-to-use-the-connect-method). – Enzokie Nov 24 '16 at 01:44
  • Alright we added con.connect(), but it is still giving a null. It is not displaying where it is coming from either – Programmer87 Nov 24 '16 at 01:54
  • what sort of header would I add? – Programmer87 Nov 24 '16 at 02:02
  • @Programmer87 Are you sure that it returns response?. Try putting Log.w("Error Check------", in ); Just before while loop and see the debug if it print out anything – Luminous_Dev Nov 24 '16 at 02:22