java - NullPointerException,简单的错误?

标签 java android nullpointerexception

不太清楚为什么我会收到 NullPointerException,这是 Java 新手,因此我们将不胜感激!

public class MainActivity extends AppCompatActivity {


    private TextView httpstuff;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView httpstuff = (TextView)findViewById(R.id.tvHttp);
        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(
            new Button.OnClickListener(){
                public void onClick(View v){
                    new JSONTask().onPostExecute("http://samples.openweathermap.org/data/2.5/weather?q=Chicago&appid=93eea9a9277a0520d2f444c3ff8c62da");
                }
            }
    );

}

   class JSONTask extends AsyncTask<String,String,String> {
        @Override
        protected String doInBackground(String... urls) {
        HttpsURLConnection connection = null;
        BufferedReader reader = null;
        try {
            URL url = new URL("https://samples.openweathermap.org/data/2.5/weather?q=Chicago&appid=93eea9a9277a0520d2f444c3ff8c62da");
            connection = (HttpsURLConnection) url.openConnection();
            connection.connect();

            InputStream stream = connection.getInputStream();
            reader = new BufferedReader(new InputStreamReader(stream));
            StringBuffer buffer = new StringBuffer();
            String line;
            while ((line = reader.readLine()) != null) {
                buffer.append(line);
            }
            return buffer.toString();

        } catch(MalformedURLException e) {
            e.printStackTrace();
        } catch(IOException e) {
            e.printStackTrace();
        }             
        finally {
            if(connection != null){
                connection.disconnect();
            }
            try{
                if(reader != null){
                    reader.close();
                }
            }catch(IOException e) {
                e.printStackTrace();
            }

        }
            return null;
    }
   @Override
   protected void onPostExecute (String result){
       super.onPostExecute(result);
       httpstuff.setText(result);
        }
    }
}

Android Studio 日志显示 NullPointerException 错误发生在

httpstuff.setText(result);

并且在

new JSONTask().onPostExecute("http://samples.openweathermap.org/data/2.5/weather?q=Chicago&appid=93eea9a9277a0520d2f444c3ff8c62da");


private TextView httpstuff is a designated as a field but says that it is not initialized, but I use it in class JSONTask? Any ideas on why the exception is occurring? Thanks alot in advance!  

最佳答案

 TextView httpstuff = (TextView)

删除第一个 TextView。

在学习时始终尝试使用 this.httpstuff (或 MainActivity.this.httpstuff),直到您了解何时不使用 this

httpstuff is a designated as a field but says that it is not initialized

您正在异步任务中使用该字段,是的,但您从未初始化它,正如警告所述。

OnCreate 有一个同名的局部变量

关于java - NullPointerException,简单的错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42216224/

相关文章:

java - Hibernate 5+ 未映射 MySQL 表

android - 为什么 Firebase 分析的自定义事件未显示在仪表板上?

java - Android 测试框架说明

java - 如何使用piccaso将下载的图像从一个 Activity 传递到另一个 Activity

java - 如何修复 ListView 中的 NullPointerException?

android - 无法实例化 PlayersActivity NullPointerException 错误

java - 在 View 上使用线程和绘图 Canvas 的空指针,android 初学者,包括 logcat

java - 泛型 0 无法转换为 java.lang.Short

java - 如何在java中将Sparql端点写入DBpedia

java - Jenkins - 将源代码编译成 jarfile,用作工件