0

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

Hieicker
  • 595
  • 1
  • 11
  • 29
  • `photoManagementLayout` is null, since you never instanced it and `buildLayout` is not static – thepoosh Sep 30 '13 at 10:16
  • @thepoosh thanks! But why if i add a scrollview pragmatically it give to me the same result? Isn't it instanced in this case? – Hieicker Sep 30 '13 at 10:23

4 Answers4

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

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

Mikalai Daronin
  • 8,590
  • 2
  • 35
  • 47
1
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.

laalto
  • 150,114
  • 66
  • 286
  • 303
  • @Iaalto i have tryed to update my code adding a scrollView to the layout class, but it is giving me the same problem. Isn't it initialized correctly in this case? – Hieicker Sep 30 '13 at 10:26
1

photoManagementLayout = new PhotoManagementLayout()

That is needed before any call to photoManagementLayout is performed

Maxim Efimov
  • 2,747
  • 1
  • 19
  • 25
1

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));
}

}
Tourki
  • 1,785
  • 15
  • 22
  • i have already added it the first time and it was working, but now i want to create the layout with another class for have the code better organized – Hieicker Sep 30 '13 at 10:29
  • if you want your method to be decoupled of your activity, here is how you should do (am gonna edit the answer) – Tourki Sep 30 '13 at 14:32