Mufrah
Mufrah

Reputation: 534

NullPointerException. Unable to read through stream

I'm trying to make a http request to connect with an application and do authentication procedure.But i'm gettting NullpointerException .The connection object is not able to get the stream data.Which is causing a null pointer exception .Here is my code

String api="URL of the application";
    String query="format=json";

    //Necessary Variables
    String publicKey=publickey;
    String privateKey=privatekey;
    String email=uemail;
    String password=upassword;
    String algo="HmacSHA1";
    //identify Application
    long time=System.currentTimeMillis() / 1000;
    SecretKeySpec sks=new SecretKeySpec(privateKey.getBytes(),algo);
    try {
        Mac mac=Mac.getInstance(algo);
        mac.init(sks);
        String hash="";
        byte[] digest=mac.doFinal((query+time).getBytes());
        for (byte b : digest) {
            hash +=String.format("%02x", b);

        }
        //Make the request
        String url=api+"?"+query+"&apikey="+publicKey+"&hash="+hash+"&t="+time;
        Log.d("URL", "url: "+url);

        try {
            HttpsURLConnection c=(HttpsURLConnection) new URL(url.trim()).openConnection();
            c.setRequestProperty("Authorization","Basic "+DatatypeConverter.printBase64Binary((email+":"+password).getBytes()));
            InputStream iStream=null;
            try {

                ***iStream=c.getInputStream();***
            } catch (Exception e) {

                iStream=c.getErrorStream();
            }

            InputStreamReader iStreamReader=new InputStreamReader(iStream);
            BufferedReader bReader=new BufferedReader(iStreamReader);
            String result="";
            String line="";
            while ((line=bReader.readLine())!=null) {
                result +=line+"\n"; 
            }

        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    } catch (NoSuchAlgorithmException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InvalidKeyException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

Here is the Exception.

10-17 11:09:38.127: E/AndroidRuntime(864): FATAL EXCEPTION: main
10-17 11:09:38.127: E/AndroidRuntime(864): java.lang.NullPointerException
10-17 11:09:38.127: E/AndroidRuntime(864):  at libcore.net.http.HttpEngine.writeRequestHeaders(HttpEngine.java:646)
10-17 11:09:38.127: E/AndroidRuntime(864):  at libcore.net.http.HttpEngine.readResponse(HttpEngine.java:800)
10-17 11:09:38.127: E/AndroidRuntime(864):  at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:274)
10-17 11:09:38.127: E/AndroidRuntime(864):  at libcore.net.http.HttpURLConnectionImpl.getErrorStream(HttpURLConnectionImpl.java:100)
10-17 11:09:38.127: E/AndroidRuntime(864):  at libcore.net.http.HttpsURLConnectionImpl.getErrorStream(HttpsURLConnectionImpl.java:124)
10-17 11:09:38.127: E/AndroidRuntime(864):  at com.arc.distimoapp.LoginScreen.loginDistimo(LoginScreen.java:113)
10-17 11:09:38.127: E/AndroidRuntime(864):  at com.arc.distimoapp.LoginScreen$1.onClick(LoginScreen.java:62)
10-17 11:09:38.127: E/AndroidRuntime(864):  at android.view.View.performClick(View.java:4084)
10-17 11:09:38.127: E/AndroidRuntime(864):  at android.view.View$PerformClick.run(View.java:16966)
10-17 11:09:38.127: E/AndroidRuntime(864):  at android.os.Handler.handleCallback(Handler.java:615)
10-17 11:09:38.127: E/AndroidRuntime(864):  at android.os.Handler.dispatchMessage(Handler.java:92)
10-17 11:09:38.127: E/AndroidRuntime(864):  at android.os.Looper.loop(Looper.java:137)
10-17 11:09:38.127: E/AndroidRuntime(864):  at android.app.ActivityThread.main(ActivityThread.java:4745)
10-17 11:09:38.127: E/AndroidRuntime(864):  at java.lang.reflect.Method.invokeNative(Native Method)
10-17 11:09:38.127: E/AndroidRuntime(864):  at java.lang.reflect.Method.invoke(Method.java:511)
10-17 11:09:38.127: E/AndroidRuntime(864):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-17 11:09:38.127: E/AndroidRuntime(864):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-17 11:09:38.127: E/AndroidRuntime(864):  at dalvik.system.NativeStart.main(Native Method)

Upvotes: 2

Views: 1749

Answers (1)

Mufrah
Mufrah

Reputation: 534

Some modification in my code make me to achieve destination. Firstly i add e.printstacktrack in catch body.

try {
      Log.d(TAG, "Trying to get the input stream..");
      iStream=c.getInputStream();
    } catch (Exception e) {
      e.printStackTrace();
      iStream=c.getErrorStream();
}

Which let me know about the new exception.which was

 android.os.NetworkOnMainThreadException ..............

And this exception is resolved by putting you code in a thread

            Thread t = new Thread(){
            @Override
            public void run() {
                // TODO Auto-generated method stub
                try {
                    HttpsURLConnection c=(HttpsURLConnection) new URL(url.trim()).openConnection();

                    Log.d(TAG, "Parameter value is " + "Basic " + DatatypeConverter.printBase64Binary((email+":"+password).getBytes()));

                    c.setRequestProperty("Authorization","Basic "+DatatypeConverter.printBase64Binary((email+":"+password).getBytes()));
                    InputStream iStream=null;
                    try {
                        Log.d(TAG, "Trying to get the input stream..");

                        iStream=c.getInputStream();
                    } catch (Exception e) {

                        e.printStackTrace();

                        iStream=c.getErrorStream();
                    }

                    Log.d(TAG, "Trying to read...");
                    InputStreamReader iStreamReader=new InputStreamReader(iStream);

                    Log.d(TAG, "Buffering...");
                    BufferedReader bReader=new BufferedReader(iStreamReader);
                    String result="";
                    String line="";

                    Log.d(TAG, "About to parse results...");
                    while ((line=bReader.readLine())!=null) {
                        Log.d(TAG, "Line is " + line);
                        result +=line+"\n"; 

                    }

                    Log.d(TAG, "Output is:  " + result);

                } catch (MalformedURLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }                   

                super.run();
            }

        };

        t.start();

Now its working fine.

Upvotes: 2

Related Questions