尝试调用虚方法~ApplicationController.buildNetworkService(java.lang.String,int)'在null对象引用上

时间:2016-09-08 03:41:49

标签: android

当我尝试连接到服务器时遇到了问题。 我从logcat获得此错误消息 我不知道什么是真正的问题

  

引起:java.lang.NullPointerException:尝试调用虚方法' void org.androidtown.myjsgexample.ApplicationController.buildNetworkService(java.lang.String,int)'在null对象引用上       在org.androidtown.myjsgexample.MainActivity.onCreate(MainActivity.java:34)

  public class ApplicationController extends Application{

    private static ApplicationController instance;
    // 메소드 만들었네 getInstance
    public static ApplicationController getInstance() {
        return instance;
    }

    @Override
    public void onCreate() {
        super.onCreate();

        ApplicationController.instance = this; // 어플리케이션 처음 실행될때 instance생성
    }

    private NetworkService networkService;
    public NetworkService getNetworkService(){
        return networkService;
    }

    private String baseUrl;

    public void buildNetworkService(String ip, int port){
        synchronized (ApplicationController.class){

            if(networkService == null){
                baseUrl = String.format("http://simon-prac1.herokuapp.com/",ip, port);
                Gson gson = new GsonBuilder().create();

                GsonConverterFactory factory = GsonConverterFactory.create(gson);
                //서버에서 json형식으로 데이터를 보내고 이를 파싱해서 받아오기 위함

                Retrofit retrofit = new Retrofit.Builder().baseUrl(baseUrl).addConverterFactory(factory).build();
                networkService = retrofit.create(NetworkService.class);

            }
        }
    }


}

这是MainActivity第34行的来源。

ApplicationController application = ApplicationController. getInstance();
    application.buildNetworkService("127.0.0.1",3306);
    networkService = ApplicationController.getInstance().getNetworkService();

这是NetworkService接口。

public interface NetworkService {

    @POST("/PersonalData")
    Call<PersonalData> post_personalData(@Body PersonalData personalData);

/*
    @GET("/PersonalData")
    Call<List<PersonalData>> getAllData();
*/

    @GET("/PersonalData/{name}")
    Call<PersonalData> getNamePersonalData(@Path("name")String name);


}

错误内容

09-08 03:17:46.059 4244-4244/org.androidtown.myjsgexample E/AndroidRuntime: FATAL EXCEPTION: main
Process: org.androidtown.myjsgexample, PID: 4244
java.lang.RuntimeException: Unable to start activity ComponentInfo{org.androidtown.myjsgexample/org.androidtown.myjsgexample.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void org.androidtown.myjsgexample.ApplicationController.buildNetworkService(java.lang.String, int)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void org.androidtown.myjsgexample.ApplicationController.buildNetworkService(java.lang.String, int)' on a null object reference
at org.androidtown.myjsgexample.MainActivity.onCreate(MainActivity.java:34)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
请帮助我!

1 个答案:

答案 0 :(得分:0)

检查application是否为空。

确保manifest.xml使用您的CustomApplication入口点(针对您的案例的ApplicationController)