Login_btn.setOnClickListener((View v) ->
        {
            byte[] login_info = new byte[1000];

            FileInputStream fIn;
            try {
                fIn = new FileInputStream(f);
                fIn.read(login_info);
                fIn.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

            String str =  new String(login_info);
            login_info = null;

            int index = str.indexOf('-');
            String s1 = str.substring(0, index);
            String s2 = str.substring(index + 1);

            List<Parameter> parameters = new ArrayList<>();
            parameters.add(new Parameter("user_name", s1));
            parameters.add(new Parameter("pass", s2));

            SoapObject that_user =
                    Connector.Send_request( "Login", null, null, parameters);

        });

class Connector and MyRunnable:

lass MyRunnable implements Runnable
{
    public static final String NAME_SPACE = "http://tempuri.org/";
    public static final String URL = "https://192.168.1.176:44383/Messenger_service.asmx";

    private SoapObject obj;
    private String Class_name, method_name;
    private Class class_obj;
    private List<Parameter> parameters;

    public MyRunnable(String method_name, String Class_name, Class class_obj,
                      List<Parameter> parameters)
    {
        this.Class_name = Class_name;
        this.class_obj = class_obj;
        this.method_name = method_name;
        this.parameters = parameters;
    }

    @Override
    public void run()
    {
        SoapObject request = new SoapObject(NAME_SPACE, method_name);

        for(Parameter par : parameters)
            request.addProperty(par.getName(), par.getObj());

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(request);

        if(Class_name != null)
        {
            envelope.addMapping(NAME_SPACE, Class_name, class_obj);
        }

        HttpTransportSE transporter = new HttpTransportSE(URL);
        transporter.debug = true;

        try
        {
            transporter.call(NAME_SPACE + method_name, envelope);
            obj = (SoapObject) envelope.getResponse();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        }
    }

    public SoapObject getObj()
    {
        return obj;
    }
}

public class Connector
{
    public static final String NAME_SPACE = "http://tempuri.org/";

    public static SoapObject Send_request(String method_name, String Class_name, Class class_obj,
                                          List<Parameter> parameters)
    {
        MyRunnable r = new MyRunnable(method_name, Class_name, class_obj, parameters);
        Thread t = new Thread(r);

        t.start();
        try {
            t.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        return r.getObj();
    }
}

I can't figure out why it throws this error! My guess is because of me passing a "local reference" of s1, s2 from main thread to another thread. But it seems weird because I've done these many times and they all work. What's wrong here!

I'm using:

implementation 'com.google.code.ksoap2-android:ksoap2-android:3.6.2'

I also use these lines to bypass security:

public class ByPasser
{
    public static void disableSSLCertificateChecking()
    {
        TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }

            @Override
            public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
                // Not implemented
            }

            @Override
            public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
                // Not implemented
            }
        } };

        try {
            SSLContext sc = SSLContext.getInstance("TLS");

            sc.init(null, trustAllCerts, new java.security.SecureRandom());

            HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
            HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier()
            {
                @Override
                public boolean verify(String hostname, SSLSession session) { return true; }
            });
        }
        catch (KeyManagementException e)
        {
            e.printStackTrace();
        }
        catch (NoSuchAlgorithmException e)
        {
            e.printStackTrace();
        }
    }
}

My manifest is also given:

< uses-permission android:name="android.permission.INTERNET" />

It's okay when i replace s1, s2 with string literals. I can claim that s1, s2 are not the prolems

My Logcat:

Process: com.example.messenger, PID: 6942 java.lang.NullPointerException: Attempt to invoke virtual method 'int java.io.InputStream.read(byte[], int, int)' on a null object reference at org.ksoap2.transport.HttpTransportSE.readDebug(HttpTransportSE.java:320) at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:271) at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:118) at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:113) at com.example.messenger.Connector.MyRunnable.run(Connector.java:62)

1

There are 1 best solutions below

0
MathematicsBeginner On

The reason is because my stream is oversized with 1000. So, s2 just equals 123����������������������������������������������������. s1, s2 is the reason indeed. Just change byte[] login_info = new byte[1000] to byte[] login_info = new byte[(int)f.size()];. That solves my problem. Thank you your time : D