Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/188.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java NullPointerException,简单的错误?_Java_Android_Nullpointerexception - Fatal编程技术网

Java NullPointerException,简单的错误?

Java NullPointerException,简单的错误?,java,android,nullpointerexception,Java,Android,Nullpointerexception,我不太清楚为什么我会得到一个NullPointerException,这是Java的新功能,因此非常感谢您的帮助 public class MainActivity extends AppCompatActivity { private TextView httpstuff; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInsta

我不太清楚为什么我会得到一个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);
        }
    }
}

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!  
删除第一个文本视图

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

httpstuff被指定为一个字段,但表示它未初始化

您正在Asynctask中使用该字段,是的,但您从未初始化它,正如警告所说


OnCreate有一个同名的局部变量

您正在跟踪
httpstuff
谢谢您的帮助!我删除了onCreate中的第一个textview,但仍然在同一行中得到异常,如果我遗漏了一些明显的内容,很抱歉。findViewById返回null。我认为不能将文本设置为空字符串。您可以在代码中插入print语句,以查看使用View.onClickListener()会发生什么情况来解决此问题?您已经在使用它了。Button.onClickListener实际上并不存在,我现在正在看一个教程,我将代码一行一行地复制,但仍然会出错..hmm
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)