Reputation: 91
When I start the application, everything works normal, but when I press the button enter, it closes alone. I do not know what could be the problem I think this is it, could you please help me
I have this code
package gt.eade.eadeapp;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import org.json.JSONArray;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class login extends AppCompatActivity implements View.OnClickListener {
Button btnIngresar;
EditText txtUsu,txtPas;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
txtUsu=(EditText)findViewById(R.id.txtusu);
txtPas=(EditText)findViewById(R.id.txtpas);
btnIngresar=(Button)findViewById(R.id.btnIngresar);
btnIngresar.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Thread tr=new Thread(){
@Override
public void run() {
final String resultado=enviarDatosGET(txtUsu.getText().toString(), txtPas.getText().toString());
runOnUiThread(new Runnable() {
@Override
public void run() {
int r = obtDatosJSON(resultado);
Toast.makeText(getApplicationContext(), r+"", Toast.LENGTH_LONG).show();
if (r > 0) {
Intent i = new Intent(getApplicationContext(), Ubicanos.class);
i.putExtra("cod", txtUsu.getText().toString());
startActivity(i);
} else {
Toast.makeText(getApplicationContext(), "Usuario o Pas Incorrectos", Toast.LENGTH_LONG).show();
}
}
});
}
};
tr.start();
}
public String enviarDatosGET(String usu, String pas){
URL url=null;
String linea="";
int respuesta=0;
StringBuilder resul=null;
try{
url=new URL("http://eade.tv/serviciosWeb/valida.php?usu="+usu+"&pas="+pas);
HttpURLConnection conection=(HttpURLConnection)url.openConnection();
respuesta=conection.getResponseCode();
resul=new StringBuilder();
if(respuesta==HttpURLConnection.HTTP_OK){
InputStream in=new BufferedInputStream(conection.getInputStream());
BufferedReader reader=new BufferedReader(new InputStreamReader(in));
while((linea=reader.readLine())!=null){
resul.append(linea);
}
}
}catch (Exception e){}
return resul.toString();
}
public int obtDatosJSON(String response){
int res=0;
try{
JSONArray json=new JSONArray(response);
if(json.length()>0){
res=1;
}
}catch(Exception e){}
return res;
}
}
Method invocation 'toString' may produce 'java.lan.NullPointerException'
Can you help me? Please
Upvotes: 2
Views: 8284
Reputation: 544
In enviarDatosGET this method in catch case
StringBuilder resul=null will not be init.
then call return resul.toString(); = null.toString();
You need do something to resolve your exception case to do what you want to handle this exception .
Upvotes: 1
Reputation: 3237
you can replace StringBuilder resul=null;
with StringBuilder resul=new StringBuilder();
and in the try block, delete resul=new StringBuilder();
.
if your connection catch any exception, the code resul=new StringBuilder();
will not be reached. Under this circumstance, the method will return null.toString()
, This is why you got NullPointException
.
Upvotes: 1