0

Not quite sure why i'm getting a NullPointerException, new to Java so any help would be greatly appreciated!

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 Logs says that the NullPointerException error is occurring at

httpstuff.setText(result);

And at

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!  
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
B.Quinn
  • 77
  • 1
  • 7

1 Answers1

1
 TextView httpstuff = (TextView)

Remove the first TextView.

Always try to use this.httpstuff (or MainActivity.this.httpstuff) while learning and until you understand when not to use this

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

You're using the field in the Asynctask, yes, but you never initialized it, as the warning says.

OnCreate has a local variable of the same name

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • Thanks for the help! I removed the first textview in the onCreate but am still getting the exception at the same lines, sorry if i am missing something obvious. – B.Quinn Feb 14 '17 at 01:23
  • Well, findViewById returns null. I don't think you can set the text to a null string. You can put print statements through the code to see what happens – OneCricketeer Feb 14 '17 at 01:25
  • Would using View.onClickListener() resolve this? – B.Quinn Feb 14 '17 at 01:45
  • You already are using that. Button.onClickListener does not actually exist – OneCricketeer Feb 14 '17 at 01:46
  • Im a bit confused, watching a tutorial right now and I have the code copied line for line, but am still getting errors..hmmm – B.Quinn Feb 14 '17 at 02:00
  • `activity_main` must contain an ID of tvHttp, otherwise it's null. If your web server throws an error, then you return a null string object. And in the later case, you print the stacktrace from the URL connection event, so maybe read above the nullpointerexception to see if you find that – OneCricketeer Feb 14 '17 at 14:37
  • Or just don't setText on anything and print the result string into the logcat – OneCricketeer Feb 14 '17 at 14:39