2017-09-07 1 views
-2

Ich bekomme weiterhin einen Nullzeiger Ausnahmefehler. Ich habe meinen Code durchgesehen und bin mir nicht sicher, warum ich diesen Fehler erhalte. Ich bevölkert, wenn ich das Programm kompiliereNull-Zeiger-Ausnahme - Android-Studio

Der Fehler liest sich wie folgt Nullzeiger Ausnahme: Versuch, virtuelle Methode int java.lang.String.length() auf eine Null-Objekt-Referenz aufzurufen. Danke im Voraus.

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

Ohne den FULL Stacktrace für die Ausnahme können wir nicht einmal erraten, was das Problem sein könnte – hardillb

Antwort

1

Die JSONArray namens 'Array' ist null in Ihrem OnPostExecute Mehtod. Höchstwahrscheinlich ist Ihre weatherInfo-Zeichenfolge null.

Ich schlage vor, dass Sie den vollständigen Stacktrace für eine bessere Erklärung veröffentlichen.

+0

Ich fand es heraus. Vielen Dank :) – mustyg123