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(); } 

}

16
  • 2
    Provice a stacktrace please. Commented Nov 24, 2016 at 1: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. Commented Nov 24, 2016 at 1:34
  • The stacktrace is: Failed to set EGL_SWAP_BEHAVIOR on surface 0xa8d63f60, error=EGL_BAD_MATCH Commented Nov 24, 2016 at 2:01
  • @enzokie are u referring to the website itself that ive provided? When I run it through postman I am getting a proper response Commented Nov 24, 2016 at 2:03
  • @Enzokie HEAD request returns 200 for me... Anyways, Programmer87, Volley / OkHttp may be better HTTP libraries for your needs. Commented Nov 24, 2016 at 2:08

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

12 Comments

It was my mistake, its returning 200 now.
@Nobody so are you saying that me assembling the string is what is giving the null error?
No, there is no null error in the code you provided. I am just pointing out a more efficient way of assembling it.
@enzokie what are you getting for the value of sname then when you run it?
I'm getting "Exchange, a song by Bryson Tiller " when I run your code, directly copy-pasted
|
-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.

4 Comments

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.
Alright we added con.connect(), but it is still giving a null. It is not displaying where it is coming from either
what sort of header would I add?
@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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.