爪哇 - NullPointerException异常得到的JSONObject时 [英] Java - NullPointerException when getting JSONObject

查看:1553
本文介绍了爪哇 - NullPointerException异常得到的JSONObject时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图实现使用的 Luken code 的RESTClient实现类。我有一个名为 UserFunctions 一类通过RESTClient实现检索的JSONObject:

I'm trying to implement a login feature in my Android app using Lukencode's RestClient class. I have a class named UserFunctions to retrieve JSONObject via RestClient:

public class UserFunctions {

    private RestClient restClient;
    private String json;
    private Context mContext;

    private static String login_tag = "login";
    private static String register_tag = "register";

    // constructor
    public UserFunctions(Context context){
        restClient = new RestClient("http://10.0.2.2:81/HGourmet/user");
        mContext = context;
    }

    /**
     * function make Login Request
     * @param email
     * @param password
     * */
   public JSONObject loginUser(String email, String password){
        // Building Parameters      
        restClient.AddParam("tag", login_tag);
        restClient.AddParam("email", email);
        restClient.AddParam("password", password);

        // getting JSON Object
        try{
    restClient.Execute(RequestMethod.POST);
    }catch(Exception e){
        e.printStackTrace();
    }               
    json = restClient.getResponse();

        JSONObject jObj = null;
        try {
            jObj = new JSONObject(json);
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }
        return jObj;
    }

和下面是我在logcat中得到:

And below is what I get in logcat:

01-06 09:01:05.805: E/Trace(1819): error opening trace file: No such file or directory (2)
01-06 09:02:05.485: E/AndroidRuntime(1819): FATAL EXCEPTION: main
01-06 09:02:05.485: E/AndroidRuntime(1819): java.lang.NullPointerException
01-06 09:02:05.485: E/AndroidRuntime(1819):     at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:116)
01-06 09:02:05.485: E/AndroidRuntime(1819):     at org.json.JSONTokener.nextValue(JSONTokener.java:94)
01-06 09:02:05.485: E/AndroidRuntime(1819):     at org.json.JSONObject.<init>(JSONObject.java:154)
01-06 09:02:05.485: E/AndroidRuntime(1819):     at org.json.JSONObject.<init>(JSONObject.java:171)
01-06 09:02:05.485: E/AndroidRuntime(1819):     at com.hanu.hgourmet.library.UserFunctions.loginUser(UserFunctions.java:49)
01-06 09:02:05.485: E/AndroidRuntime(1819):     at com.hanu.hgourmet.LoginActivity$1.onClick(LoginActivity.java:56)
01-06 09:02:05.485: E/AndroidRuntime(1819):     at android.view.View.performClick(View.java:4202)
01-06 09:02:05.485: E/AndroidRuntime(1819):     at android.view.View$PerformClick.run(View.java:17340)
01-06 09:02:05.485: E/AndroidRuntime(1819):     at android.os.Handler.handleCallback(Handler.java:725)
01-06 09:02:05.485: E/AndroidRuntime(1819):     at android.os.Handler.dispatchMessage(Handler.java:92)
01-06 09:02:05.485: E/AndroidRuntime(1819):     at android.os.Looper.loop(Looper.java:137)
01-06 09:02:05.485: E/AndroidRuntime(1819):     at android.app.ActivityThread.main(ActivityThread.java:5039)
01-06 09:02:05.485: E/AndroidRuntime(1819):     at java.lang.reflect.Method.invokeNative(Native Method)
01-06 09:02:05.485: E/AndroidRuntime(1819):     at java.lang.reflect.Method.invoke(Method.java:511)
01-06 09:02:05.485: E/AndroidRuntime(1819):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-06 09:02:05.485: E/AndroidRuntime(1819):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-06 09:02:05.485: E/AndroidRuntime(1819):     at dalvik.system.NativeStart.main(Native Method)

我真的希望有人会帮我找出这个问题的原因。非常感谢你。

I really hope that someone would help me pinpoint the reason of this problem. Thank you very much.

推荐答案

我不会扩大你应该怎么做,但只对为什么会发生。一个AsyncTask的,顾名思义,是异步的。这意味着,当你执行它,但它确实在其并行作业,在不同的线程。所以你不能指望它之后的execute()已被称为(否则它会被称为的 SyncTask 的终止,也不会有什么理由存在)。

I won't expand on how you should do this, but only on why it happens. An AsyncTask, by definition, is asynchronous. This means that when you execute it, it does its job in parallel, in a different thread. So you can't expect it to be terminated right after execute() has been called (else it would be called SyncTask, and wouldn't have any reason to exist).

请参阅的AsyncTask 作为一个烤面包机。当您启动烤面包机(执行AsyncTask的),它烤面包面包,但不会立即返回烤面包。这需要一些时间来敬酒了。虽然它烤面包,你可以做别的事情(因此不完全冻结UI线程)。而当它已经完成烤面包,它会通知你用的叮!的是,面包烤。这是这里的同样的事情。您应该只开始使用 JSON (的烤面包),当AsyncTask的通知您,它已经完成执行。

See AsyncTask as a toaster. When you start the toaster (execute the AsyncTask), it toasts your bread, but doesn't return the toasted bread immediately. It takes some time to toast it. While it's toasting your bread, you can do something else (and thus not freeze the UI thread completely). And when it has finished toasting your bread, it notifies you with a ding! that the bread is toasted. That's the same thing here. You should only start using json (the toasted bread), when the AsyncTask notifies you that it has finished executing.

这篇关于爪哇 - NullPointerException异常得到的JSONObject时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆