0

I'm completly new to Java and Android. What I'm trying to do is a class imported into my main activity, which can be used to generate interface for me. for example if I want a button i call the class and the the button function and supply it with the width, height, text and so on, and then let the class handle the rest. However i can't even get it to start. This is the error:

 java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference

This is main activity onCreate:

application_interface applicationInterfaceClass;

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


    applicationInterfaceClass = new application_interface();

    // Interface part
    RelativeLayout applicationLayout;
    applicationLayout = applicationInterfaceClass.standardLayout();

    // Button
    Button applicationButton;
    applicationButton = applicationInterfaceClass.standardButton();
    //Button standardButton = new Button(this);

    applicationLayout.addView(applicationButton);
    setContentView(applicationLayout);
}

And this is my interface class:

package com.example.kristofer.myapplication;

import android.support.v7.app.ActionBarActivity;
import android.widget.Button;
import android.widget.RelativeLayout;

/**
 * Created by Kristofer on 2015-04-22.
 */

public class application_interface extends ActionBarActivity {

    public RelativeLayout standardLayout(){
        RelativeLayout Layout;
        Layout = new RelativeLayout(this);

        return Layout;
    }

    public Button standardButton(){
        Button standardButton;
        standardButton = new Button(this);

        return standardButton;
    }
}

Full error:

04-22 18:41:50.458    1951-1951/com.example.kristofer.myapplication E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.kristofer.myapplication, PID: 1951
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.kristofer.myapplication/com.example.kristofer.myapplication.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
            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.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
            at android.content.ContextWrapper.getResources(ContextWrapper.java:85)
            at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:74)
            at android.view.View.<init>(View.java:3570)
            at android.view.View.<init>(View.java:3675)
            at android.view.ViewGroup.<init>(ViewGroup.java:491)
            at android.widget.RelativeLayout.<init>(RelativeLayout.java:248)
            at android.widget.RelativeLayout.<init>(RelativeLayout.java:244)
            at android.widget.RelativeLayout.<init>(RelativeLayout.java:240)
            at android.widget.RelativeLayout.<init>(RelativeLayout.java:236)
            at com.example.kristofer.myapplication.application_interface.standardLayout(application_interface.java:15)
            at com.example.kristofer.myapplication.MainActivity.onCreate(MainActivity.java:26)
            at android.app.Activity.performCreate(Activity.java:5937)
            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)
04-22 18:43:23.543    2303-2303/com.example.kristofer.myapplication D/AndroidRuntime﹕ Shutting down VM
04-22 18:43:23.544    2303-2303/com.example.kristofer.myapplication E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.kristofer.myapplication, PID: 2303
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.kristofer.myapplication/com.example.kristofer.myapplication.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
            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.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
            at android.content.ContextWrapper.getResources(ContextWrapper.java:85)
            at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:74)
            at android.view.View.<init>(View.java:3570)
            at android.view.View.<init>(View.java:3675)
            at android.view.ViewGroup.<init>(ViewGroup.java:491)
            at android.widget.RelativeLayout.<init>(RelativeLayout.java:248)
            at android.widget.RelativeLayout.<init>(RelativeLayout.java:244)
            at android.widget.RelativeLayout.<init>(RelativeLayout.java:240)
            at android.widget.RelativeLayout.<init>(RelativeLayout.java:236)
            at com.example.kristofer.myapplication.application_interface.standardLayout(application_interface.java:15)
            at com.example.kristofer.myapplication.MainActivity.onCreate(MainActivity.java:26)
            at android.app.Activity.performCreate(Activity.java:5937)
            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)
Cevil
  • 215
  • 2
  • 5
  • 15
  • Post the full stack trace of the exception. The stack trace contains the line number indicating where the exception is thrown. We need to have this information, and see the line in the posted code. – JB Nizet Apr 22 '15 at 19:45
  • 3
    don't use the new operator to instantiate an Activity subclass. – Blackbelt Apr 22 '15 at 19:49
  • Your variable name `Layout` could be causing problems. And if not that you should read something about java/android naming conventions. – puelo Apr 22 '15 at 19:51
  • Youre doing alot between super.onCreate and setContentView. Most times when i see this type of error it has to do with trying to reference the layout before you call setContentView (thats why you currently have commented some code for creating standardButton). You should move your setContentView to the line after super.onCreate – Chris Newman Apr 22 '15 at 22:31

0 Answers0