Sophie
Sophie

Reputation: 2634

Caused by: java.lang.IllegalArgumentException: Host name may not be null

I am using four Strings in my code. The first String contains "http://", the second String contains user host URL (entered by user), the third contains "/musics/a.mp3" and the fourth String contains complete URL, by combining the previous Strings, like this:

String fourth = first+second+third;

Then I'm using the fourth String in AsyncTask's execute, like this:

new JSONAsyncTask().execute(fourth);

And I'm getting the error below.

Log:

10-07 06:17:15.934: E/AndroidRuntime(1194): FATAL EXCEPTION: AsyncTask #1
10-07 06:17:15.934: E/AndroidRuntime(1194): java.lang.RuntimeException: An error occured while executing doInBackground()
10-07 06:17:15.934: E/AndroidRuntime(1194):     at android.os.AsyncTask$3.done(AsyncTask.java:278)
10-07 06:17:15.934: E/AndroidRuntime(1194):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
10-07 06:17:15.934: E/AndroidRuntime(1194):     at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
10-07 06:17:15.934: E/AndroidRuntime(1194):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
10-07 06:17:15.934: E/AndroidRuntime(1194):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
10-07 06:17:15.934: E/AndroidRuntime(1194):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
10-07 06:17:15.934: E/AndroidRuntime(1194):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
10-07 06:17:15.934: E/AndroidRuntime(1194):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
10-07 06:17:15.934: E/AndroidRuntime(1194):     at java.lang.Thread.run(Thread.java:856)
10-07 06:17:15.934: E/AndroidRuntime(1194): Caused by: java.lang.IllegalArgumentException: Host name may not be null
10-07 06:17:15.934: E/AndroidRuntime(1194):     at org.apache.http.HttpHost.<init>(HttpHost.java:83)
10-07 06:17:15.934: E/AndroidRuntime(1194):     at org.apache.http.impl.client.AbstractHttpClient.determineTarget(AbstractHttpClient.java:497)
10-07 06:17:15.934: E/AndroidRuntime(1194):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
10-07 06:17:15.934: E/AndroidRuntime(1194):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
10-07 06:17:15.934: E/AndroidRuntime(1194):     at com.app.ifko.HomeActivity$JSONAsyncTask.doInBackground(HomeActivity.java:101)
10-07 06:17:15.934: E/AndroidRuntime(1194):     at com.app.ifko.HomeActivity$JSONAsyncTask.doInBackground(HomeActivity.java:1)
10-07 06:17:15.934: E/AndroidRuntime(1194):     at android.os.AsyncTask$2.call(AsyncTask.java:264)
10-07 06:17:15.934: E/AndroidRuntime(1194):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
10-07 06:17:15.934: E/AndroidRuntime(1194):     ... 5 more
10-07 06:17:22.764: E/WindowManager(1194): Activity com.app.ifko.HomeActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@416b80b0 that was originally added here
10-07 06:17:22.764: E/WindowManager(1194): android.view.WindowLeaked: Activity com.app.ifko.HomeActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@416b80b0 that was originally added here
10-07 06:17:22.764: E/WindowManager(1194):  at android.view.ViewRootImpl.<init>(ViewRootImpl.java:343)
10-07 06:17:22.764: E/WindowManager(1194):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:245)
10-07 06:17:22.764: E/WindowManager(1194):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:193)
10-07 06:17:22.764: E/WindowManager(1194):  at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:118)
10-07 06:17:22.764: E/WindowManager(1194):  at android.view.Window$LocalWindowManager.addView(Window.java:537)
10-07 06:17:22.764: E/WindowManager(1194):  at android.app.Dialog.show(Dialog.java:274)
10-07 06:17:22.764: E/WindowManager(1194):  at com.app.ifko.HomeActivity$JSONAsyncTask.onPreExecute(HomeActivity.java:90)
10-07 06:17:22.764: E/WindowManager(1194):  at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:561)
10-07 06:17:22.764: E/WindowManager(1194):  at android.os.AsyncTask.execute(AsyncTask.java:511)
10-07 06:17:22.764: E/WindowManager(1194):  at com.app.ifko.HomeActivity.onCreate(HomeActivity.java:74)
10-07 06:17:22.764: E/WindowManager(1194):  at android.app.Activity.performCreate(Activity.java:4465)
10-07 06:17:22.764: E/WindowManager(1194):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
10-07 06:17:22.764: E/WindowManager(1194):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
10-07 06:17:22.764: E/WindowManager(1194):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
10-07 06:17:22.764: E/WindowManager(1194):  at android.app.ActivityThread.access$600(ActivityThread.java:122)
10-07 06:17:22.764: E/WindowManager(1194):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
10-07 06:17:22.764: E/WindowManager(1194):  at android.os.Handler.dispatchMessage(Handler.java:99)
10-07 06:17:22.764: E/WindowManager(1194):  at android.os.Looper.loop(Looper.java:137)
10-07 06:17:22.764: E/WindowManager(1194):  at android.app.ActivityThread.main(ActivityThread.java:4340)
10-07 06:17:22.764: E/WindowManager(1194):  at java.lang.reflect.Method.invokeNative(Native Method)
10-07 06:17:22.764: E/WindowManager(1194):  at java.lang.reflect.Method.invoke(Method.java:511)
10-07 06:17:22.764: E/WindowManager(1194):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-07 06:17:22.764: E/WindowManager(1194):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
10-07 06:17:22.764: E/WindowManager(1194):  at dalvik.system.NativeStart.main(Native Method)
10-07 06:17:24.144: I/Process(1194): Sending signal. PID: 1194 SIG: 9

Activity code:

public class HomeActivity extends Activity {

String first, second, third, fourth;
TextView etTextToSave;
private SharedPreferences  sharedprefs; 

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.home);

    etTextToSave = (TextView) findViewById(R.id.etTextToSave);      

    sharedprefs = getSharedPreferences("MyPrefs", MODE_PRIVATE);
    etTextToSave.setText(sharedprefs.getString("SharedPrefsData",""));

    second = etTextToSave.getText().toString();     

    first = "http://";
    third = "/musics/a.mp3";            
    fourth = first+second+third;

    .........................

    new JSONAsyncTask().execute(fourth);

}

Upvotes: 2

Views: 5340

Answers (2)

laalto
laalto

Reputation: 152777

If you're calling etTextToSave.getText() in onCreate(), the returned text will be whatever there's by default in the layout and not what was entered by the user.

The code doesn't show if you're using an onclick listener or similar but if you are, move the getText() to the click handler.

Upvotes: 1

Eran
Eran

Reputation: 393771

If the host part of the URL comes from the user, you must validate it before using it to construct a URL.

Don't call new JSONAsyncTask().execute(fourth); if second is null.

Upvotes: 2

Related Questions