Why do I get this error? java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText (java.lang.CharSequence)' on a null object reference
I paste the rss reader code:
public class MainActivity extends Activity
{
Button conectar;
TextView t_cmpFeed;
String url_web = "http://ep00.epimg.net/rss/elpais/portada.xml";
Conection conexion;
@Override
protected void onCreate(Bundle savedInstanceState)
{
t_cmpFeed = (TextView) findViewById(R.id.txt1);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
conectar = (Button) findViewById(R.id.btnconectar);
conectar.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
switch (v.getId())
{
case R.id.btnconectar:
conexion = new Conection();
conexion.execute();
break;
default:
break;
}
}
});
}
public class Conection extends AsyncTask<Void, Integer, String>
{
@Override
protected String doInBackground(Void... params)
{
String l_mostrar = " ";
int i=0, j=0;
try
{
URL url = new URL(url_web);
HttpURLConnection t_connect = (HttpURLConnection) url.openConnection();
t_connect.setRequestProperty("User-Agent", "Mozilla/5.0" + "(Windows; Android 6.0; es-ES) Ejemplo HTTP");
int setRespuesta = t_connect.getResponseCode();
if(setRespuesta == HttpURLConnection.HTTP_OK)
{
BufferedReader rfeed = new BufferedReader( new InputStreamReader(t_connect.getInputStream()));
String l = rfeed.readLine();
while (l != null)
{
if(l.indexOf("<title>") >= 0)
{
i = l.indexOf("<title>")+16;
j = l.indexOf("<title>")-3;
l_mostrar = l.substring(j,i);
l_mostrar += "\n--------------------\n";
//l_mostrar += l;
}
l = rfeed.readLine();
}
rfeed.close();
}
else
{
Toast.makeText(getApplicationContext(), "No se ha encontrado la conexión", Toast.LENGTH_SHORT).show();
}
t_connect.disconnect();
return l_mostrar;
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
return null;
}
@Override
protected void onCancelled(String aVoid)
{
super.onCancelled();
}
@Override
protected void onPostExecute(String l_mostrar)
{
t_cmpFeed.append(l_mostrar);
}
}
}