Question

I have created a simple class called "PhotoManagement" that is my actual main activity. After do that, i create another class, that extends the activity. Inside this second class i have created a method (now is empty) that i want call in the other class to create pragmatically the layout. What i'm trying to do now so, is only a simple call. It should be extremely simple, but i'm receiving a null pointer exception. So can you please help me to understand what i'm doing wrong? This is the main activity:

public class PhotoManagement extends Activity{

PhotoManagementLayout photoManagementLayout;

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

}

And this is the class that should create the layout in future:

public class PhotoManagementLayout extends Activity{

  public void buildLayout(){
    ScrollView scrollView = new ScrollView(this); //create a new scrollView
    scrollView.setBackground(getResources().getDrawable(R.drawable.background)); //give the background gradient
    scrollView.setLayoutParams(new ScrollView.LayoutParams(ScrollView.LayoutParams.MATCH_PARENT, //set the main params about the dynamic size of the scrollView
                                                 ScrollView.LayoutParams.MATCH_PARENT));
    scrollView.setPadding(0, 20, 0, 0);
    setContentView(scrollView);
  }
}

This is the error that i'm receving:

09-30 09:58:27.495: E/AndroidRuntime(3102): FATAL EXCEPTION: main
09-30 09:58:27.495: E/AndroidRuntime(3102): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.dilandprints2/com.example.dilandprints2.PhotoManagement}: java.lang.NullPointerException
09-30 09:58:27.495: E/AndroidRuntime(3102):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
09-30 09:58:27.495: E/AndroidRuntime(3102):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
09-30 09:58:27.495: E/AndroidRuntime(3102):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
09-30 09:58:27.495: E/AndroidRuntime(3102):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
09-30 09:58:27.495: E/AndroidRuntime(3102):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-30 09:58:27.495: E/AndroidRuntime(3102):     at android.os.Looper.loop(Looper.java:137)
09-30 09:58:27.495: E/AndroidRuntime(3102):     at android.app.ActivityThread.main(ActivityThread.java:5041)
09-30 09:58:27.495: E/AndroidRuntime(3102):     at java.lang.reflect.Method.invokeNative(Native Method)
09-30 09:58:27.495: E/AndroidRuntime(3102):     at java.lang.reflect.Method.invoke(Method.java:511)
09-30 09:58:27.495: E/AndroidRuntime(3102):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
09-30 09:58:27.495: E/AndroidRuntime(3102):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
09-30 09:58:27.495: E/AndroidRuntime(3102):     at dalvik.system.NativeStart.main(Native Method)
09-30 09:58:27.495: E/AndroidRuntime(3102): Caused by: java.lang.NullPointerException
09-30 09:58:27.495: E/AndroidRuntime(3102):     at com.example.dilandprints2.PhotoManagement.onCreate(PhotoManagement.java:21)
09-30 09:58:27.495: E/AndroidRuntime(3102):     at android.app.Activity.performCreate(Activity.java:5104)
09-30 09:58:27.495: E/AndroidRuntime(3102):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
09-30 09:58:27.495: E/AndroidRuntime(3102):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
09-30 09:58:27.495: E/AndroidRuntime(3102):     ... 11 more

The photoManagement class is present on the manifest, the photoManagementLayout class not.

Thanks

Was it helpful?

Solution

Your photoManagementLayout is null, it haven't been initialized.

Furthermore both of your classes are extending Activity, and you are using an Activity inside another one.

Activities are meant to be launched one after the other via intents, they are not normal classes that you can instantiate using the new key word. So your problem here is more than a simple NullPointerException, you are misunderstanding the Android workflow.

Why don't you put your method code directly in the core of the PhotoManager activity ?

-----------------------------------------------

Just take your buildLayout method and put it in the PhotManagment Acticity directly, then call it in the onCreate, it should work


EDIT Here is a way to (more or less) decouple the method buildLayout from the activity :

public class PhotoManagementLayout {
      public static View buildLayout(Context context){
        ScrollView scrollView = new ScrollView(context); //create a new scrollView
        scrollView.setBackground(context.getResources().getDrawable(R.drawable.background)); //give the background gradient
        scrollView.setLayoutParams(new ScrollView.LayoutParams(ScrollView.LayoutParams.MATCH_PARENT, //set the main params about the dynamic size of the scrollView
                                                     ScrollView.LayoutParams.MATCH_PARENT));
        scrollView.setPadding(0, 20, 0, 0);
        return scrollView ; 
      }
}

And in the onCreate :

public class PhotoManagement extends Activity{



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(PhotoManagementLayout.buildLayout(this));
}

}

OTHER TIPS

09-30 09:58:27.495: E/AndroidRuntime(3102): Caused by: java.lang.NullPointerException
09-30 09:58:27.495: E/AndroidRuntime(3102):     at com.example.dilandprints2.PhotoManagement.onCreate(PhotoManagement.java:21)

NullPointerException in PhotoManagement.onCreate()

PhotoManagementLayout photoManagementLayout;

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

Here photoManagementLayout is obviously null as you have never initialized it.

PhotoManagementLayout photoManagementLayout just declares a variable photoManagementLayout which is a reference to PhotoManagementLayout object. By default, references are initialized to null.

photoManagementLayout = new PhotoManagementLayout()

That is needed before any call to photoManagementLayout is performed

Caused by: java.lang.NullPointerException
    at com.example.dilandprints2.PhotoManagement.onCreate(PhotoManagement.java:21)

Looks like photoManagementLayout is null at photoManagementLayout.buildLayout();

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top