首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >方法调用'toString‘可能会产生'java.lang.NullPointerException'?

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

Stack Overflow用户
提问于 2017-01-13 01:45:45
回答 2查看 7.4K关注 0票数 2

当我启动应用程序时,一切正常工作,但是当我按下enter按钮时,它就会单独关闭。我不知道问题出在哪里,我想就是这样,你能帮帮我吗?

我有这个代码

代码语言:javascript
复制
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’

你能帮帮我吗?请

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-01-13 01:53:45

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

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

票数 1
EN

Stack Overflow用户

发布于 2017-01-13 04:18:53

在enviarDatosGET中,这种方法适用于catch情况

StringBuilder resul=null不会在里面。

然后调用返回resul.toString();= null.toString();

您需要做一些事情来解决您的异常情况,以执行您想要处理的异常。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41626140

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档