Java Gdx.net.sendHttpRequest中的NullPointerException
我曾尝试使用libGDX编写一个小代码来处理网络。这里有一个代码:Java Gdx.net.sendHttpRequest中的NullPointerException,java,networking,libgdx,Java,Networking,Libgdx,我曾尝试使用libGDX编写一个小代码来处理网络。这里有一个代码: 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 = "******
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
}
如果我在浏览器中编写此URL,它将正常工作。这意味着,问题在我这边。如何修复它
导致NullPointerException的对象值摘要:
您正在程序的静态主入口点中编写此代码。您的Gdx尚未加载,因此,
Gdx
此时仍然为空
创建一个非静态类,将代码放入其构造函数中,并在此静态入口点内初始化该类
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();
}
}
你能发布你的跟踪吗?是否有可能与给定的“null”参数有关?您是否使用了调试器?可能重复,但没有效果。我刚刚复制了你的代码,但仍然有NPException。那么什么是exactle
Null
?我肯定猜它是没有堆栈跟踪的Gdx
。您确定您的项目设置正确吗?因为您甚至不应该在LibGDX
projectstatic main(String[]args){…}
的入口点进行编码,而应该在GameName扩展ApplicationListener/Game
的核心模块中进行编码。