2017-08-05 219 views
-1

我正在嘗試使用AsyncTask來做HTTP POST。我必須創建Response類,因爲我們不能擴展多個類。在Response類,我接受HTTP請求的響應,然後根據響應開始新的活動。行:responseClass.responseFunction(response);正在拋出NullPointer異常。我如何從登錄方法採取response空指針異常

public class Login extends AsyncTask <String, Void, Void> { 

    Response responseClass = new Response(); 

    @Override 
    protected Void doInBackground(String... strings) { 
     String email = strings[0]; 
     String password = strings[1]; 

     login(email, password); 

     return null; 
    } 

    public void login(String email, String password) { 
     HttpURLConnection connection; 
     OutputStreamWriter request = null; 

     URL url = null; 
     String response = null; 
     String parameters = "email=" + email + "&password=" + password; 

     try { 
      url = new URL("url"); 
      connection = (HttpURLConnection) url.openConnection(); 
      connection.setDoOutput(true); 
      connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
      connection.setRequestMethod("POST"); 

      request = new OutputStreamWriter(connection.getOutputStream()); 
      request.write(parameters); 
      request.flush(); 
      request.close(); 
      String line = ""; 
      InputStreamReader isr = new InputStreamReader(connection.getInputStream()); 
      BufferedReader reader = new BufferedReader(isr); 
      StringBuilder sb = new StringBuilder(); 
      while ((line = reader.readLine()) != null) { 
       sb.append(line + "\n"); 
      } 

      System.out.println("parameter: " + parameters); 
      // Response from server after login process will be stored in response variable. 
      response = sb.toString(); 
      isr.close(); 
      reader.close(); 
     } catch (IOException e) { 
      // Error 

     } 
     System.out.print("response" +response); 
     responseClass.responseFunction(response); 

    } 

    class Response extends AppCompatActivity{ 

     public void responseFunction(String response){ 

      if(response.charAt(0) == '1'){ 

      Toast.makeText(getApplicationContext(), "e-mail veya şifre hatalı!", Toast.LENGTH_LONG).show(); 

     } 
     else if(response.charAt(0) == '0'){ 
      System.out.print("Sonucc1"); 
      Toast.makeText(getApplicationContext(), "Başarıyla giriş yapıldı!", Toast.LENGTH_LONG).show(); 

      //session.createLoginSession(email,password); 
      Intent intent = new Intent(this, Amount.class); 
      startActivity(intent); 
     } 
     else if(response.charAt(0)== '2'){ 
      Toast.makeText(getApplicationContext(), "Geçerli bir e-mail adresi giriniz!", Toast.LENGTH_LONG).show(); 
     } 
     else if(response.charAt(0)=='3'){ 
      Toast.makeText(getApplicationContext(), "Şifre 6 karakterden kısa olamaz!", Toast.LENGTH_LONG).show(); 


      } 
     } 

    } 
} 

這裏是logcat的:

E/AndroidRuntime: FATAL EXCEPTION: main 
                 Process: com.example.merve.tev, PID: 20768 
                 java.lang.NullPointerException 
                  at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:109) 
                  at com.example.merve.tev.Login$Response.responseFunction(Login.java:97) 
                  at com.example.merve.tev.Login.login(Login.java:82) 
                  at com.example.merve.tev.Login.doInBackground(Login.java:31) 
                  at com.example.merve.tev.LoginActivity$1.onClick(LoginActivity.java:54) 
                  at android.view.View.performClick(View.java:4438) 
                  at android.view.View$PerformClick.run(View.java:18422) 
                  at android.os.Handler.handleCallback(Handler.java:733) 
                  at android.os.Handler.dispatchMessage(Handler.java:95) 
                  at android.os.Looper.loop(Looper.java:136) 
                  at android.app.ActivityThread.main(ActivityThread.java:5017) 
                  at java.lang.reflect.Method.invokeNative(Native Method) 
                  at java.lang.reflect.Method.invoke(Method.java:515) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
                  at dalvik.system.NativeStart.main(Native Method) 

線97:Toast.makeText( 「!Başarıylagirişyapıldı」 getApplicationContext(),Toast.LENGTH_LONG).show();

+1

我看不到'responseClass'在哪裏被賦值。它也似乎沒有被注入。 –

+0

你有沒有得到?您缺少responseClass變量的初始化... 響應responseClass = new Response(); – Bayou

+0

我初始化了responseClass。但是,NullPointer異常再次拋出。 @TimBiegeleisen – m1129839

回答

0

正如@Tim在評論中指出的,您只需要初始化您的responseClass變量。也許你想做到這一點,如下所示:在發生

public class Login extends AsyncTask <String, Void, Void> { 

    Response responseClass = new Response(); // initialisation 

    @Override 
    protected Void doInBackground(String... strings) { 
     ... 
     ... 
+0

我初始化了'responseClass',但是,NullPointer異常再次拋出。 – m1129839

+0

用確切的錯誤logcat更新你的問題。 – ZeekHuge

+0

「響應」本身是否爲空? –

0

的錯誤,因爲你總是被設置爲null,響應串在String response = null;。你沒有給任何例外的字符串。因此,每當代碼面臨異常時,responseClass.responseFunction(response);將收到null字符串。然後它會拋出空指針異常。

public void login(String email, String password) { 
    ... 
    // You always set response as null 
    String response = null; 
    try { 
     ... 
     StringBuilder sb = new StringBuilder(); 
     while ((line = reader.readLine()) != null) { 
     sb.append(line + "\n"); 
     } 
     ... 
     response = sb.toString(); 
      ... 
    } catch (IOException e) { 
     // Error 
     // But here you don't set any string for response 
    } 

    // Hence the response will be null an throw an exception. 
    System.out.print("response" +response); 
    responseClass.responseFunction(response); 
}