0

I'm trying to get the IP address of a computer(or Raspberry Pi) from its hostname from an Android App using InetAddress,The problem is when i type "www.google.com" for example it works but when I try "PC****" which is my PC hostname (got it from the cmd command hostname) it doesn't work Please Help me

 public void SearchMachines(View v) {
     String netAddress = null;
     TextView localTextView = (TextView)findViewById(R.id.tvScaned);
     try
      {
       netAddress = new NetTask().execute("www.google.com").get();
       localTextView.setText("Address: " + netAddress);
      }
      catch (Exception e1)
       {
        e1.printStackTrace();
       }


 }
 public class NetTask extends AsyncTask<String, Integer, String>
    {
        @Override
        protected String doInBackground(String... params)
        {
            InetAddress addr = null;
            try
            {
                addr = InetAddress.getByName(params[0]);
            }

            catch (UnknownHostException e)
            {
                            e.printStackTrace();
            }
            return addr.getHostAddress();
        }
    }
 ![Here is Logcat][1]
Hedi Jl
  • 29
  • 7

1 Answers1

0

return addr.getHostAddress();. Even after a catch you are using addr but it is null then so you have a NullPointerException.

Put that statement in the try block and put return e.getMessage(); in the catch block.

greenapps
  • 11,154
  • 2
  • 16
  • 19