-2

I have tried to write a little code using libGDX to work with network. Here's a code:

public class Test {

public static void main(String[] args) {
    String accessToken = "********"; //a set of symbols, not important because it is specific of request target
    String userID = "*********"; //also not important
    String message = "Hello World";
    String uri = "method/wall.post?owner_id=" + userID + "&message=" + message + "&access_token=" + accessToken;
    HttpRequestBuilder requestBuilder = new HttpRequestBuilder();
    HttpRequest httpRequest = requestBuilder.newRequest().method(HttpMethods.GET).url("https://api.vk.com/").content(uri).build();
    Gdx.net.sendHttpRequest(httpRequest, //Here Eclipse shows NullPointerException
            null); //But not here
}

If I write this URL in browser, it works right. It means, that the problem on my side. How to fix it?

Summary of values of the object which causes NullPointerException:

debugger screenshot

Community
  • 1
  • 1
S. O. Chaos
  • 136
  • 1
  • 7

1 Answers1

1

You are writing this code in the static main entry point of your program. Your Gdx is not yet loaded,so Gdx is still null at this point.

Create a none static class and put your code in it's constructor and initialize that class within this static entry point.

public class WebTest()
{
  public WebTest()
  {
    String accessToken = "********"; //a set of symbols, not important because it is specific of request target
    String userID = "*********"; //also not important
    String message = "Hello World";
    String uri = "method/wall.post?owner_id=" + userID + "&message=" + message + "&access_token=" + accessToken;
    HttpRequestBuilder requestBuilder = new HttpRequestBuilder();
    HttpRequest httpRequest = requestBuilder.newRequest().method(HttpMethods.GET).url("https://api.vk.com/").content(uri).build();
    Gdx.net.sendHttpRequest(httpRequest, //Here Eclipse shows NullPointerException
            null); //But not here
  }
}

public class Test {    
  public static void main(String[] args) {
    new WebTest();
  }
}
Madmenyo
  • 7,643
  • 6
  • 46
  • 88
  • No effect. I've just copied your code but there's still NPException. – S. O. Chaos Jan 24 '16 at 12:53
  • So what exactle is `Null`? I would surely guess it is `Gdx` without a stacktrace. Are you sure you have your project setup correctly since you should not even get to code in the entry point for a `LibGDX` project `static main(String[] args) { ... }` but just in the core module where `GameName extends ApplicationListener / Game`. – Madmenyo Jan 25 '16 at 08:33