【问题标题】:Null Pointer Exception - Android studio空指针异常 - Android Studio
【发布时间】:2017-09-07 17:00:44
【问题描述】:

我不断收到空指针异常错误。我查看了我的代码,但不确定为什么会出现此错误。我在编译程序时填充

错误如下所示 空指针异常:尝试在空对象引用上调用虚拟方法 int java.lang.String.length()。提前致谢。

 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);


}

}

【问题讨论】:

  • 如果没有完整的堆栈跟踪异常,我们甚至无法猜测问题可能是什么

标签: android pointers exception null


【解决方案1】:

在您的 onPostExecute 方法中名为“array”的 JSONArray 为空。 很可能您的 weatherInfo 字符串为空。

我建议您发布完整的堆栈跟踪以获得更好的解释。

【讨论】:

    猜你喜欢
    • 2013-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-18
    • 1970-01-01
    相关资源
    最近更新 更多