Android 空指针异常找不到解决方案

发布于 2024-12-05 11:56:52 字数 2304 浏览 1 评论 0原文

我正在开发一个与服务器通信并接收数据流的应用程序,这就像 60-80k 个符号。所以我将该字符串拆分并将流计算为多个片段,当我获得我创建的流的类型时像这样的类的实例:

BasePacket packet=null; // this is the base packet
if(packetType=1)
    startPacket packet = new startPacket(params) // startPacket class extends BasePacket

if(packetType=2)
    endPacket packet = new endPacket(params) // endPacket class extends BasePacket
......

startPacket 中,我创建了 userPacket 的实例,它也扩展了 BasePacket

userPacket user; // in startPacket

我还有另一个类,它是 RPCCommunicator 哪里我包含了与服务器通信所需的所有方法。

在 RPCCommnicator 中我有:

userPacket user;
startPacket startP;
.......

这是我的问题...... 在 RPCCOmmunicator 中,我有一个这样的方法:

    private static Integer localUserIdByServerUserId(int serverUserId, String serverName,Context context){

    try {
        dbHelper = new DataBaseHelper(context, "opa_sys_tpl.sqlite", null, 1);
        dbHelper.checkDatabase("opa_sys_tpl.sqlite");
        dbHelper.copyDataBase("opa_sys_tpl.sqlite");
    } catch (IOException e) {
        e.printStackTrace();
    }
    dbHelper.getDatabase();

    String query = "SELECT id FROM users WHERE objectId = "+serverUserId+" AND serverName = '"+serverName+"' LIMIT 1";
    ArrayList<String> result = new ArrayList<String>();
    cursor = dbHelper.executeSQLQuery(query);
    cursor.moveToFirst();
    while(!cursor.isAfterLast()) {
        result.add(cursor.getString(cursor.getColumnIndex("id")));
         cursor.moveToNext();
    }

    Log.i("result ","Result : "+result.toString());
    Log.i("CURSOR ","Cursor Position : "+cursor.getPosition());
    Integer uuid = Integer.parseInt(result.get(cursor.getColumnIndex("id")));
    Log.w("localUSerByIdServerUserId","LocalUserByIdServerUserId result : "+uuid);
    cursor.close();
    return uuid;
}

我在 startPacket 中调用它,如下所示:

int uuId = rpc.lUserIdByServerUserId(userId,newServerName,context);

它在最后一行抛出 NullPointerException,我找不到如何修复它。对我的数据库使用辅助类,所以我不认为问题存在,但这里是我的 DataBaseHelper.class 代码的链接:DatabaseHelper.class

所以先谢谢了!!!

I'm working on an application which communicates with server and receiving a data stream,which is like 60-80k symbols.So I am splitin' that string and calculating the stream into a pieces and when I get the type of the stream I create an instance of class like this :

BasePacket packet=null; // this is the base packet
if(packetType=1)
    startPacket packet = new startPacket(params) // startPacket class extends BasePacket

if(packetType=2)
    endPacket packet = new endPacket(params) // endPacket class extends BasePacket
......

In startPacket I create instance to userPacket which extends BasePacket too:

userPacket user; // in startPacket

I have another class which is RPCCommunicator where I include all the methods which I need to communicate with server.

In RPCCommnicator I have :

userPacket user;
startPacket startP;
.......

And here is my problem...
In RPCCOmmunicator I have a method like this :

    private static Integer localUserIdByServerUserId(int serverUserId, String serverName,Context context){

    try {
        dbHelper = new DataBaseHelper(context, "opa_sys_tpl.sqlite", null, 1);
        dbHelper.checkDatabase("opa_sys_tpl.sqlite");
        dbHelper.copyDataBase("opa_sys_tpl.sqlite");
    } catch (IOException e) {
        e.printStackTrace();
    }
    dbHelper.getDatabase();

    String query = "SELECT id FROM users WHERE objectId = "+serverUserId+" AND serverName = '"+serverName+"' LIMIT 1";
    ArrayList<String> result = new ArrayList<String>();
    cursor = dbHelper.executeSQLQuery(query);
    cursor.moveToFirst();
    while(!cursor.isAfterLast()) {
        result.add(cursor.getString(cursor.getColumnIndex("id")));
         cursor.moveToNext();
    }

    Log.i("result ","Result : "+result.toString());
    Log.i("CURSOR ","Cursor Position : "+cursor.getPosition());
    Integer uuid = Integer.parseInt(result.get(cursor.getColumnIndex("id")));
    Log.w("localUSerByIdServerUserId","LocalUserByIdServerUserId result : "+uuid);
    cursor.close();
    return uuid;
}

and I'm calling it in startPacket like this :

int uuId = rpc.lUserIdByServerUserId(userId,newServerName,context);

And the NullPointerException it throws on that last line and I can't find how to fix it.I'm using a helper class for my database so I don't think that the problem is there,but here is a link to my DataBaseHelper.class code : DatabaseHelper.class.

So thanks in advance!!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不美如何 2024-12-12 11:56:52

如果该行

int uuId = rpc.lUserIdByServerUserId(userId,newServerName,context);

确实是引发异常的行,则 rpc 必须为 null

使用调试器单步执行程序以找出出现这种情况的原因。

If this line

int uuId = rpc.lUserIdByServerUserId(userId,newServerName,context);

is indeed the line that throws the exception, rpc must be null.

Use a debugger to step through the program to find out why this is the case.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文