-1

I am trying to fetch image from an Url using 'Picasso' but I am getting a message in My Logcat like

Caused by: java.lang.IllegalArgumentException: Context must not be null.

Below is my code and Logcat.

09-10 02:53:14.420  29527-    29527/green.example.tejask.asynctasknetworking I/art﹕ Late-enabling -Xcheck:jni
09-10 02:53:14.764  29527-29562/green.example.tejask.asynctasknetworking D/OpenGLRenderer﹕ Render dirty regions requested: true
09-10 02:53:14.788  29527-29527/green.example.tejask.asynctasknetworking D/﹕ HostConnection::get() New Host Connection established 0xabc8c8b0, tid 29527
09-10 02:53:14.799  29527-29527/green.example.tejask.asynctasknetworking D/Atlas﹕ Validating map...
09-10 02:53:14.879  29527-29562/green.example.tejask.asynctasknetworking D/libEGL﹕ loaded /system/lib/egl/libEGL_emulation.so
09-10 02:53:14.881  29527-29562/green.example.tejask.asynctasknetworking D/libEGL﹕ loaded /system/lib/egl/libGLESv1_CM_emulation.so
09-10 02:53:14.894  29527-29562/green.example.tejask.asynctasknetworking D/libEGL﹕ loaded /system/lib/egl/libGLESv2_emulation.so
09-10 02:53:14.914  29527-29562/green.example.tejask.asynctasknetworking D/﹕ HostConnection::get() New Host Connection established 0xa3116120, tid 29562
09-10 02:53:14.962  29527-29562/green.example.tejask.asynctasknetworking I/OpenGLRenderer﹕ Initialized EGL, version 1.4
09-10 02:53:15.132  29527-29562/green.example.tejask.asynctasknetworking D/OpenGLRenderer﹕ Enabling debug mode 0
09-10 02:53:15.166  29527-29562/green.example.tejask.asynctasknetworking W/EGL_emulation﹕ eglSurfaceAttrib not implemented
09-10 02:53:15.166  29527-29562/green.example.tejask.asynctasknetworking W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa3119220, error=EGL_SUCCESS
09-10 02:53:31.853  29527-29527/green.example.tejask.asynctasknetworking D/AndroidRuntime﹕ Shutting down VM
09-10 02:53:31.858  29527-29527/green.example.tejask.asynctasknetworking E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: green.example.tejask.asynctasknetworking, PID: 29527
    java.lang.RuntimeException: Unable to start activity ComponentInfo{green.example.tejask.asynctasknetworking/green.example.tejask.asynctasknetworking.SecondActivity}: java.lang.IllegalArgumentException: Context must not be null.
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     Caused by: java.lang.IllegalArgumentException: Context must not be null.
            at com.squareup.picasso.Picasso$Builder.<init>(Picasso.java:701)
            at com.squareup.picasso.Picasso.with(Picasso.java:662)
            at green.example.tejask.asynctasknetworking.SecondActivity.onCreate(SecondActivity.java:22)
            at android.app.Activity.performCreate(Activity.java:5933)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
09-10 02:53:39.188  29527-29527/green.example.tejask.asynctasknetworking I/Process﹕ Sending signal. PID: 29527 SIG: 9

This is my code:

    package green.example.tejask.asynctasknetworking;
    import android.content.Context;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.widget.ImageView;

    import com.squareup.picasso.Picasso;

/**
 * Created by tejas k on 10-09-2015.
 */
    public class SecondActivity extends AppCompatActivity {
    private ImageView img;
    Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String imgUri = "https://i.imgur.com/tGbaZCY.jpg";
        img= (ImageView) findViewById(R.id.img);
        Picasso.with(context).load(imgUri).into(img);

    }
}
klaus19
  • 23
  • 1
  • 10
  • 1
    `IllegalArgumentException: Context must not be null` because `context ` is null. add `context =this` before calling `Picasso.with` – ρяσѕρєя K Sep 10 '15 at 07:18

5 Answers5

2

You have not initialize Context in onCreate() method. please initialize it.

Try this.

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = SecondActivity.this;
        String imgUri = "https://i.imgur.com/tGbaZCY.jpg";
        img= (ImageView) findViewById(R.id.img);
        Picasso.with(context).load(imgUri).into(img);

    }
Rajesh Jadav
  • 12,801
  • 5
  • 53
  • 78
1

The exception says all

IllegalArgumentException error in Android,Context must not be null

in your case you declared a class member of type Context called context without initialising it. Its default value is null, hence the crash. Since Activity inherits from Context, you can use the keyword this.

E.g.

Picasso.with(this).load(imgUri).into(img);
Blackbelt
  • 156,034
  • 29
  • 297
  • 305
1

Always initialize a variable before using it

just put Context context=this;

Ashish Shukla
  • 1,027
  • 16
  • 36
0

Initialize you context as :

context=SecondActivity.this;

before :

Picasso.with(context).load(imgUri).into(img);
KOTIOS
  • 11,177
  • 3
  • 39
  • 66
  • I did it, as you said but now I am not able to see the image though, I can see the blank Activity without any error. – klaus19 Sep 10 '15 at 08:37
0

Either you should initialize your context. public class SecondActivity extends AppCompatActivity { private ImageView img; Context context;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    String imgUri = "https://i.imgur.com/tGbaZCY.jpg";
    img= (ImageView) findViewById(R.id.img);
    context = SecondActivity.this;
    Picasso.with(context).load(imgUri).into(img);

}

}

or you dont need to declare the context.

Picasso.with(this).load(imgUri).into(img); 
Alex Chengalan
  • 8,211
  • 4
  • 42
  • 56