2017-09-07 2 views
-2

Je continue d'obtenir une erreur d'exception de pointeur nul. J'ai regardé mon code et je ne suis pas sûr de savoir pourquoi je reçois cette erreur. Je remplit lorsque je complie le programmeNull Pointer Exception - Studio Android

L'erreur se lit comme suit Null pointeur Exception: Tentative d'invoquer la méthode virtuelle int java.lang.String.length() sur une référence d'objet null. Merci d'avance.

EditText enterCity; 
TextView weatherView; 


public void onClick (View view) { 



    downloadAPIInfo task = new downloadAPIInfo(); 

    String APIKEY = "b4fabae83c89c469d7a458a230b7a267"; 
    String website = "http://api.openweathermap.org/data/2.5/weather?q="; 

    String url = website + enterCity.getText().toString() + APIKEY; 

    task.execute(url); 

    Log.i("User Entry", enterCity.getText().toString()); 



} 

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

    URL url; 
    HttpURLConnection httpURLConnection = null; 
    String result = ""; 

    @Override 
    protected String doInBackground(String... urls) { 

     try { 
      url = new URL(urls[0]); 

      httpURLConnection = (HttpURLConnection) url.openConnection(); 

      InputStream input = httpURLConnection.getInputStream(); 

      InputStreamReader reader = new InputStreamReader(input); 

      int data = reader.read(); 

      while(data != -1) { 

       char one = (char) data; 

       result += one; 

       data = reader.read(); 
      } 

      return result; 


     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     return null; 
    } 

    //Onpostexecute interacts with the UI 
    @Override 
    protected void onPostExecute(String result) { 
     super.onPostExecute(result); 

     try { 

      String message = ""; 

      JSONObject object = new JSONObject(result); 

      String weatherInfo = object.getString("weather"); 

      Log.i("Weather content", weatherInfo); 


      JSONArray array = new JSONArray(weatherInfo); 

      for(int i = 0; i < array.length(); i++) { 

       JSONObject jsonPart = array.getJSONObject(i); 

       Log.i("main", jsonPart.getString("main")); 

       String main = ""; 
       String description = ""; 

       main = jsonPart.getString("main"); 
       description = jsonPart.getString("description"); 

       if(main != "" && description != "") { 

        message+= main + ":" + description + "\r\n"; 
       } 

      } 


      if(message != "") { 

       weatherView.setText(message); 
      } 

     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 


     //Log.i("WebsiteContent", result); 
    } 
} 



@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    enterCity = (EditText) findViewById(R.id.cityeditText); 
    weatherView = (TextView) findViewById(R.id.weathertextView); 


} 

}

+0

Sans le stacktrace FULL pour l'exception, nous ne pouvons même pas deviner quel pourrait être le problème – hardillb

Répondre

1

Le JSONArray nommé 'array' est nul dans votre onPostExecute mehtod. Très probablement votre chaîne weatherInfo est nulle.

Je vous suggère de poster la pile complète pour une meilleure explication.

+0

Je l'ai compris. Merci :) – mustyg123