简体   繁体   中英

Hashtable returns null in JAVA

I'm trying to input a few key value pairs into a hashtable, encode the data, then decode in in a separate android activity. However, the Hashtable returns null, when I try to get the values.

First, I'm passing a hex string as an input:

String result="000700016D0009393837363"+
              "7363636360001620002422B"+
              "00016C000553696E6768000"+
              "17300044D616C6500016600"+
              "05526168756C00017020A";

Following which, I'm decoding the string as follows-

System.out.println("hijk...."+Util.hex2ByteArray(result)+"aa "+Util.hex2ByteArray(result).length);

Hashtable<String , String> htParams = new MobileEncoderDecoder().decode(Util.hex2ByteArray(result));

System.out.println(htParams);
System.out.println("lmno...");

Where Util.hex2ByteArray(result) uses a class that converts the given hex string to a byte array. The problem's not in in converting from hex to Byte Array, as I've printed the value of the Byte Array and it is correct.

MobileEncoderDecoder() is a class that has the decode() method, described as follows-

public Hashtable decode(byte[] byData) 
{
    try
    {
        Hashtable htParams = new Hashtable();
        if(byData == null || byData.length == 0)
            return null;
        DataInputStream oStream = new DataInputStream(new ByteArrayInputStream(byData));
        int iTotalParameters = oStream.readShort();

        for(int i=0; i < iTotalParameters; i++)
        {
            byte[] byParam = new byte[oStream.readShort()];
            oStream.read(byParam);

            int iValueLen = oStream.readShort();
            byte[] byValue = null;

            if(iValueLen > 0)
            {
                byValue = new byte[iValueLen];
                oStream.read(byValue);
            }

            if(byValue != null)

                htParams.put(new String(byParam), new String(byValue));
        }

        return htParams;
    }catch(Exception ex)
    {
        ex.printStackTrace();
        return null;
    }
}

The problem's not in the input string, the conversion from hex to byte array. The run time error occurs on the line where I'm decoding the byte array. ie

Hashtable htParams = new MobileEncoderDecoder().decode(Util.hex2ByteArray(result));

How do I fix it?

EDIT- Here's my Log

06-18 12:53:29.182: D/OpenGLRenderer(15346): Enabling debug mode 0
06-18 12:53:30.412: I/AndroidRuntime(15346): VM exiting with result code 0, cleanup skipped.
06-18 12:53:35.109: I/Adreno-EGL(15627): <qeglDrvAPI_eglInitialize:385>: EGL 1.4 QUALCOMM build:  ()
06-18 12:53:35.109: I/Adreno-EGL(15627): OpenGL ES Shader Compiler Version: E031.24.00.01
06-18 12:53:35.109: I/Adreno-EGL(15627): Build Date: 12/11/13 Wed
06-18 12:53:35.109: I/Adreno-EGL(15627): Local Branch: 8226workspace
06-18 12:53:35.109: I/Adreno-EGL(15627): Remote Branch: 
06-18 12:53:35.109: I/Adreno-EGL(15627): Local Patches: 
06-18 12:53:35.109: I/Adreno-EGL(15627): Reconstruct Branch: 
06-18 12:53:35.136: D/OpenGLRenderer(15627): Enabling debug mode 0
06-18 12:53:36.063: I/System.out(15627): 12345
06-18 12:53:37.336: W/IInputConnectionWrapper(15627): showStatusIcon on inactive InputConnection
06-18 12:53:42.566: I/System.out(15627): abcdef....
06-18 12:53:42.569: I/System.out(15627): hijk....[B@41ff7730aa56
06-18 12:53:42.572: I/System.out(15627): ****Total Parameters : 7
06-18 12:53:42.573: W/System.err(15627): java.io.EOFException
06-18 12:53:42.580: W/System.err(15627):    at libcore.io.Streams.readFully(Streams.java:83)
06-18 12:53:42.580: W/System.err(15627):    at java.io.DataInputStream.readShort(DataInputStream.java:152)
06-18 12:53:42.580: W/System.err(15627):    at com.example.androscan.MobileEncoderDecoder.decode(MobileEncoderDecoder.java:37)
06-18 12:53:42.580: W/System.err(15627):    at com.example.androscan.MainActivity2.onCreate(MainActivity2.java:131)
06-18 12:53:42.580: W/System.err(15627):    at android.app.Activity.performCreate(Activity.java:5248)
06-18 12:53:42.580: W/System.err(15627):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)
06-18 12:53:42.581: W/System.err(15627):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2173)
06-18 12:53:42.581: W/System.err(15627):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2269)
06-18 12:53:42.581: W/System.err(15627):    at android.app.ActivityThread.access$800(ActivityThread.java:139)
06-18 12:53:42.581: W/System.err(15627):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
06-18 12:53:42.581: W/System.err(15627):    at android.os.Handler.dispatchMessage(Handler.java:102)
06-18 12:53:42.581: W/System.err(15627):    at android.os.Looper.loop(Looper.java:136)
06-18 12:53:42.581: W/System.err(15627):    at android.app.ActivityThread.main(ActivityThread.java:5102)
06-18 12:53:42.582: W/System.err(15627):    at java.lang.reflect.Method.invokeNative(Native Method)
06-18 12:53:42.582: W/System.err(15627):    at java.lang.reflect.Method.invoke(Method.java:515)
06-18 12:53:42.582: W/System.err(15627):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
06-18 12:53:42.583: W/System.err(15627):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
06-18 12:53:42.584: W/System.err(15627):    at dalvik.system.NativeStart.main(Native Method)
06-18 12:53:42.584: I/System.out(15627): null
06-18 12:53:42.584: I/System.out(15627): lmno...
06-18 12:53:42.584: D/AndroidRuntime(15627): Shutting down VM
06-18 12:53:42.584: W/dalvikvm(15627): threadid=1: thread exiting with uncaught exception (group=0x416e4d40)
06-18 12:53:42.588: E/AndroidRuntime(15627): FATAL EXCEPTION: main
06-18 12:53:42.588: E/AndroidRuntime(15627): Process: com.example.androscan, PID: 15627
06-18 12:53:42.588: E/AndroidRuntime(15627): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.androscan/com.example.androscan.MainActivity2}: java.lang.NullPointerException
06-18 12:53:42.588: E/AndroidRuntime(15627):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
06-18 12:53:42.588: E/AndroidRuntime(15627):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2269)
06-18 12:53:42.588: E/AndroidRuntime(15627):    at android.app.ActivityThread.access$800(ActivityThread.java:139)
06-18 12:53:42.588: E/AndroidRuntime(15627):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
06-18 12:53:42.588: E/AndroidRuntime(15627):    at android.os.Handler.dispatchMessage(Handler.java:102)
06-18 12:53:42.588: E/AndroidRuntime(15627):    at android.os.Looper.loop(Looper.java:136)
06-18 12:53:42.588: E/AndroidRuntime(15627):    at android.app.ActivityThread.main(ActivityThread.java:5102)
06-18 12:53:42.588: E/AndroidRuntime(15627):    at java.lang.reflect.Method.invokeNative(Native Method)
06-18 12:53:42.588: E/AndroidRuntime(15627):    at java.lang.reflect.Method.invoke(Method.java:515)
06-18 12:53:42.588: E/AndroidRuntime(15627):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
06-18 12:53:42.588: E/AndroidRuntime(15627):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
06-18 12:53:42.588: E/AndroidRuntime(15627):    at dalvik.system.NativeStart.main(Native Method)
06-18 12:53:42.588: E/AndroidRuntime(15627): Caused by: java.lang.NullPointerException
06-18 12:53:42.588: E/AndroidRuntime(15627):    at com.example.androscan.MainActivity2.onCreate(MainActivity2.java:134)
06-18 12:53:42.588: E/AndroidRuntime(15627):    at android.app.Activity.performCreate(Activity.java:5248)
06-18 12:53:42.588: E/AndroidRuntime(15627):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)
06-18 12:53:42.588: E/AndroidRuntime(15627):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2173)
06-18 12:53:42.588: E/AndroidRuntime(15627):    ... 11 more
06-18 12:53:44.447: I/Process(15627): Sending signal. PID: 15627 SIG: 9

This way of reading a byte-array is very hacky and error-prone, but if you insist on doing it the least you should do is check that there are enough bytes to read before any read() you're executing.

Since you're only reading short (2-bytes) you should check that there are at least two bytes in the stream before every read:

public static Hashtable decode(byte[] byData) throws Exception
{        
    try
    {
        Hashtable htParams = new Hashtable();
        if(byData == null || byData.length == 0)
            return null;
        DataInputStream oStream = new DataInputStream(new ByteArrayInputStream(byData));
        int iTotalParameters = oStream.readShort();

        for(int i=0; i < iTotalParameters; i++)
        {
            if(oStream.available() < 2)
                break;
            int len = oStream.readShort();
            byte[] byParam = new byte[len];
            if(oStream.available() <2)
                break;
            oStream.read(byParam);
            if(oStream.available() <2)
                break;
            int iValueLen = oStream.readShort();
            byte[] byValue = null;

            if(iValueLen > 0)
            {
                byValue = new byte[iValueLen];
                if(oStream.available() < 2)
                    break;
                oStream.read(byValue);
            }

            if(byValue != null)

                htParams.put(new String(byParam), new String(byValue));
        }

        return htParams;
    }catch(Exception ex)
    {
        ex.printStackTrace();
        System.out.println(ex.getMessage());
        throw ex;
    }
}

The output that I'm getting is:

i = b; val: B+
i = m; val: 987676666
i = l; val: Singh
i = s; val: Male
i = f; val: Rahul

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM