0

i have this exception shows when i run my app so i wish to help me :) the exception is : java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference

the exception shows when i click dd1 button and the button implement (public void getData(View v)) method in Login class

thank you

Login class

public class Login extends Activity {

    Button DD1;
    TextView r;
    EditText PhoneNumber1, Password1;
    ProgressBar PB;
    private RequestPackage p;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        r = (TextView) findViewById(R.id.message);
        PhoneNumber1 = (EditText) findViewById(R.id.PN_login);
        Password1 = (EditText) findViewById(R.id.PW_login);
        DD1 = (Button) findViewById(R.id.loginButton);
        PB = (ProgressBar) findViewById(R.id.pro);
        PB.setVisibility(View.INVISIBLE);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public void getData(View v) {
        PB.setVisibility(View.VISIBLE);
        RequestPackage p = new RequestPackage();
        p.setMethod("GET");
        p.setUri("http://9aleh.com/feed.asmx/CheckAccountWithPassword");
        p.setParams("phoneNumber", PhoneNumber1.getText().toString());
        p.setParams("password", Password1.getText().toString());
        String content = HttpManager.getData(p);
        if (AccountXMLParser.parseFeedChecklogin(content)) {
        r.setText("TRUE");

        }else
            r.setText("FALSE");
        PB.setVisibility(View.INVISIBLE);
    }
}

HttpManager class

public class HttpManager {


    public static String getData(RequestPackage p) {
        BufferedReader reader = null;
        String uri = p.getUri();
       if(p.getMethod().equals("GET")){

           uri+= "?"+p.getEncodeParams();
       }
        try {
            URL url = new URL(uri);
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            con.setRequestMethod(p.getMethod());

            StringBuilder sb = new StringBuilder();
            reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
            String line ;
            int fl=0;
            while ((line = reader.readLine()) != null) {
               if(fl!= 0)
                sb.append(line + "  \n");
                fl++;
            }
            reader.close();
            return sb.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            if ((reader != null)) {

            }
            try {
                reader.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}

Logcat :

    10-02 03:19:48.485  10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1273)
10-02 03:19:48.489  10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at java.net.InetAddress.lookupHostByName(InetAddress.java:431)
10-02 03:19:48.498  10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
10-02 03:19:48.498  10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at java.net.InetAddress.getAllByName(InetAddress.java:215)
10-02 03:19:48.498  10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at com.android.okhttp.internal.Network$1.resolveInetAddresses(Network.java:29)
10-02 03:19:48.498  10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:188)
10-02 03:19:48.498  10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:157)
10-02 03:19:48.498  10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:100)
10-02 03:19:48.498  10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at com.android.okhttp.internal.http.HttpEngine.createNextConnection(HttpEngine.java:357)
10-02 03:19:48.498  10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at com.android.okhttp.internal.http.HttpEngine.nextConnection(HttpEngine.java:340)
10-02 03:19:48.498  10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:330)
10-02 03:19:48.498  10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:248)
10-02 03:19:48.522  10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:433)
10-02 03:19:48.522  10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:384)
10-02 03:19:48.522  10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:231)
10-02 03:19:48.522  10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at com.example.salehabdarhman.ex.HttpManager.getData(HttpManager.java:27)
10-02 03:19:48.522  10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at com.example.salehabdarhman.ex.Login.getData(Login.java:72)
10-02 03:19:48.524  10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at java.lang.reflect.Method.invoke(Native Method)
10-02 03:19:48.524  10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at android.view.View$DeclaredOnClickListener.onClick(View.java:4447)
10-02 03:19:48.524  10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at android.view.View.performClick(View.java:5198)
10-02 03:19:48.525  10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at android.view.View$PerformClick.run(View.java:21147)
10-02 03:19:48.525  10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:739)
10-02 03:19:48.525  10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:95)
10-02 03:19:48.525  10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at android.os.Looper.loop(Looper.java:148)
10-02 03:19:48.525  10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5417)
10-02 03:19:48.525  10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at java.lang.reflect.Method.invoke(Native Method)
10-02 03:19:48.526  10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
10-02 03:19:48.526  10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
10-02 03:19:48.527  10727-10727/com.example.salehabdarhman.ex W/System.err﹕ java.lang.NullPointerException: Attempt to invoke virtual method 'void java.io.BufferedReader.close()' on a null object reference
10-02 03:19:48.527  10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at com.example.salehabdarhman.ex.HttpManager.getData(HttpManager.java:45)
10-02 03:19:48.527  10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at com.example.salehabdarhman.ex.Login.getData(Login.java:72)
10-02 03:19:48.527  10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at java.lang.reflect.Method.invoke(Native Method)
10-02 03:19:48.548  10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at android.view.View$DeclaredOnClickListener.onClick(View.java:4447)
10-02 03:19:48.548  10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at android.view.View.performClick(View.java:5198)
10-02 03:19:48.551  10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at android.view.View$PerformClick.run(View.java:21147)
10-02 03:19:48.551  10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:739)
10-02 03:19:48.552  10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:95)
10-02 03:19:48.553  10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at android.os.Looper.loop(Looper.java:148)
10-02 03:19:48.553  10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5417)
10-02 03:19:48.553  10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at java.lang.reflect.Method.invoke(Native Method)
10-02 03:19:48.553  10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
10-02 03:19:48.554  10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
10-02 03:19:48.556  10727-10727/com.example.salehabdarhman.ex W/System.err﹕ java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
10-02 03:19:48.556  10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at java.io.StringReader.<init>(StringReader.java:47)
10-02 03:19:48.556  10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at com.example.salehabdarhman.ex.AccountXMLParser.parseFeedChecklogin(AccountXMLParser.java:77)
10-02 03:19:48.556  10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at com.example.salehabdarhman.ex.Login.getData(Login.java:73)
10-02 03:19:48.561  10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at java.lang.reflect.Method.invoke(Native Method)
10-02 03:19:48.561  10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at android.view.View$DeclaredOnClickListener.onClick(View.java:4447)
10-02 03:19:48.561  10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at android.view.View.performClick(View.java:5198)
10-02 03:19:48.561  10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at android.view.View$PerformClick.run(View.java:21147)
10-02 03:19:48.561  10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:739)
10-02 03:19:48.562  10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:95)
10-02 03:19:48.562  10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at android.os.Looper.loop(Looper.java:148)
10-02 03:19:48.562  10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5417)
10-02 03:19:48.563  10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at java.lang.reflect.Method.invoke(Native Method)
10-02 03:19:48.563  10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
10-02 03:19:48.563  10727-10727/com.example.salehabdarhman.ex W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
  • Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Andreas Oct 02 '15 at 00:55
  • thank you i found the error cuz i don't use AsyncTask class :) – Saleh Abdaurhman Oct 02 '15 at 17:37

1 Answers1

1

Looks like a relatively simple mistake. In your finally block of your HttpManager class.

Should be

} finally {
    if ((reader != null)) {
      try {
         reader.close();
      } catch (Exception e) {
         e.printStackTrace();
      }
    }
}

However I highly suggest if you are using java 7+ you use try-with-resources.

So your getData method would become

public static String getData(RequestPackage p) {
     = null;
    String uri = p.getUri();
   if(p.getMethod().equals("GET")){

       uri+= "?"+p.getEncodeParams();
   }
    try (BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream()))) {
        URL url = new URL(uri);
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        con.setRequestMethod(p.getMethod());

        StringBuilder sb = new StringBuilder();
        String line ;
        int fl=0;
        while ((line = reader.readLine()) != null) {
           if(fl!= 0)
            sb.append(line + "  \n");
            fl++;
        }
        return sb.toString();
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

Thats much easier and you don't have to worry about closing the resources because they implement AutoCloseable.

  • The "reader.close()" statement is not necessary in this case, is it? – Eduardo Javier Huerta Yero Oct 02 '15 at 03:00
  • thank you i found the error cuz i don't use AsyncTask class :) – Saleh Abdaurhman Oct 02 '15 at 17:37
  • @EduardoJavierHuertaYero Yes you are correct technically a copy paste mistake. Though my understanding is that an unnecessary reader.close() statement doesn't really hurt anything. Fixing sample. – Adam Brockway Oct 05 '15 at 19:09
  • @SalehAbdaurhman So basically you are telling me you solved the problem by using a completely different method. The question is does my answer address the question as posted? If so being a new Overflow user I would appreciate the accepted answer so I can continue contributing. – Adam Brockway Oct 05 '15 at 19:12