android - 方法调用 'toString' 可能会产生 'java.lang.NullPointerException' ?

标签 android tostring java

当我启动应用程序时,一切正常,但是当我按下 Enter 按钮时,它会单独关闭。我不知道可能是什么问题,我想就是这样,你能帮我吗

我有这个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;
    }



}

方法调用“toString”可能会产生“java.lan.NullPointerException”

你能帮我吗?请

最佳答案

您可以将 StringBuilder resul=null; 替换为 StringBuilder resul=new StringBuilder(); 并在 try block 中删除 resul=new StringBuilder() ;.

如果您的连接捕获任何异常,则将无法到达代码 resul=new StringBuilder();。在这种情况下,该方法将返回 null.toString(),这就是为什么你会得到 NullPointException

关于android - 方法调用 'toString' 可能会产生 'java.lang.NullPointerException' ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41626140/

相关文章:

android - 视频标签本地文件下载 Cordova Android

android - 尽量减少按下后退键时的 Activity

java - 关键字 "this"和 "toString"到底是什么意思和作用?

java - 为什么我的maven项目无法解决

java - 我如何从声音中播放数组? - 安卓

android - Google Maps Android API V2 检查 Google Maps 应用程序是否被禁用

android - 后台计时器

java - 无法为 Junit 的方法创建模拟调用

java - 在 switch-case 中使用枚举值的字符串表示

JavaScript 检测不同场景下的八进制值