AsyncTask() 期间的 Null 指针,但定义了值 [duplicate]

Null Pointer during AsyncTask() yet values are defined [duplicate]

提问人:Aaron 提问时间:7/4/2018 最后编辑:Empty BrainAaron 更新时间:7/4/2018 访问量:194

问:

我正在尝试将我的身份验证代码交换为访问令牌。 我正在使用 ,但是,我收到一个错误:AsyncTaskRunner

java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference at java.net.URLEncoder.encode(URLEncoder.java:205).

这是我的请求参数:

CLIENT_ID = "1fb7c350-bb6a-5741-86b9-43afc2f1642f";
REDIRECT_URI = "https://xxxx.xxxxx.xxxx/rest/callback.html";
CLIENT_SECRET = "xxxxxxx";
GRANT_TYPE = "authorization_code";
ACCESS_TOKEN_URL = "https://xxxx.xxxxxx.com/oauth/token";

我的 AsyncTask 类

private class AsyncTaskRunner extends AsyncTask<String, String, String> {
    @Override
    protected String doInBackground(String... params) {
        try {
            URL url = null;
            InputStream stream = null;
            HttpURLConnection urlConnection = null;
            try {
                url = new URL(ACCESS_TOKEN_URL);
                urlConnection = (HttpURLConnection) url.openConnection();
                urlConnection.setRequestMethod("POST");
                urlConnection.setDoOutput(true);

                data = URLEncoder.encode("client_id", "UTF-8")
                        + "=" + URLEncoder.encode(CLIENT_ID, "UTF-8"); //*This line above is where the caused by exception is pointing to.*

                data += "&" + URLEncoder.encode("client_secret", "UTF-8") + "="
                        + URLEncoder.encode(CLIENT_SECRET, "UTF-8");
                data += "&" + URLEncoder.encode("grant_type", "UTF-8") + "="
                        + URLEncoder.encode(GRANT_TYPE, "UTF-8");
                data += "&" + URLEncoder.encode("code", "UTF-8") + "="
                        + URLEncoder.encode(authCode, "UTF-8");
                data += "&" + URLEncoder.encode("redirect_uri", "UTF-8") + "="
                        + URLEncoder.encode(REDIRECT_URI, "UTF-8");

                urlConnection.connect();

                OutputStreamWriter wr = new OutputStreamWriter(urlConnection.getOutputStream());
                wr.write(data);
                wr.flush();

                stream = urlConnection.getInputStream();
                BufferedReader reader = new BufferedReader(new InputStreamReader(stream, "UTF-8"), 8);
                String result = reader.readLine();
                accessToken = result;
                return accessToken;

            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (urlConnection != null) {
                    urlConnection.disconnect();
                }
            }

            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
                Log.i("Result", "SLEEP ERROR");
            }
            return null;
        } finally {
            Log.i("Result", "SLEEP ERROR");
        }
    }
}

在我的“发送”按钮上:

authCode = sharedPreferences.getString("code", null);
params=new String[5];
CLIENT_ID = params[0];
CLIENT_SECRET = params[1];
GRANT_TYPE = params[2];
REDIRECT_URI = params[3];
authCode = params[4];
new AsyncTaskRunner().execute(params);

这是我的完整日志:

07-04 11:43:57.749 7157-8120/almac.com.cameratutorialtest E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #2
Process: almac.com.cameratutorialtest, PID: 7157
java.lang.RuntimeException: An error occurred while executing doInBackground()
    at android.os.AsyncTask$3.done(AsyncTask.java:325)
    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
    at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
    at java.util.concurrent.FutureTask.run(FutureTask.java:242)
    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
    at java.lang.Thread.run(Thread.java:761)
    Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
    at java.net.URLEncoder.encode(URLEncoder.java:205)
    at almac.com.cameratutorialtest.MainActivity$AsyncTaskRunner.doInBackground(MainActivity.java:275)
    at almac.com.cameratutorialtest.MainActivity$AsyncTaskRunner.doInBackground(MainActivity.java:256)
    at android.os.AsyncTask$2.call(AsyncTask.java:305)
    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607) 
    at java.lang.Thread.run(Thread.java:761) 

当它被设置在上面时,它是如何变得空的?为什么它没有达到检索访问令牌的程度?

java android-asynctask nullpointerexception oauth-2.0

评论

0赞 Lino 7/4/2018
格式化这个可怕的混乱问题。它是高度不可读的。此外,仅仅粘贴所有代码并不能使这个问题更加清晰。请告诉我们更多。堆栈跟踪很好,但我们没有行号,这对我们来说非常困难
0赞 AskNilesh 7/4/2018
可能是你的authCode是空的
0赞 Zun 7/4/2018
叹息。这是你的错误.修复该问题Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
0赞 Aaron 7/4/2018
对不起@Lino我编辑了它,因为我无法输入行号,我没有粘贴我认为需要的所有代码。而且我的authcode不是null,我已经调试过它并打印了很多次,它有一个值
0赞 Aaron 7/4/2018
我能看到@ZUNJAE。这就是我问这个问题的原因。

答:

2赞 Zun 7/4/2018 #1

取代

authCode = params[4];

params[4] = authCode

就在这儿

authCode = sharedPreferences.getString("code", null);
params=new String[5];
CLIENT_ID = params[0];
CLIENT_SECRET = params[1];
GRANT_TYPE = params[2];
REDIRECT_URI = params[3];
authCode = params[4];
new AsyncTaskRunner().execute(params);`

您将每个变量设置为 null,这不是您想要的,这就是您收到 nullpointer 错误的原因。

评论

0赞 Aaron 7/4/2018
与 null 对象在同一行上仍然出现相同的错误
0赞 Zun 7/4/2018
你能拿出证据证明错误仍然存在吗?截取屏幕截图,包括新代码和堆栈跟踪。
0赞 Aaron 7/4/2018
好的,不再崩溃了,谢谢!但由于某种原因,我仍然没有获得访问令牌。
0赞 Zun 7/4/2018
是的,这是一个完全不同的问题。将我的答案标记为正确,因为它确实解决了您的问题
0赞 Zun 7/4/2018
或者创建一个新帖子或编辑您当前的帖子,因为现在您有多个问题,并且您正在尝试将其放入一个提交中。