Need help with Android NullPointer problem

2019-08-12 05:20发布

问题:

I'm currently making a game and I came across this annoying problem. Tried to google it but no luck so far. I have a class defined in its own file(code here is not of my game but a simplified one with same errors):

package graf.grafika;

public class MyClass {
    int number;
}

And the problem is in myMethod(Application stops unexpectedly):

public class aktivnost extends Activity {
    MyClass[] myclass = new MyClass[5];

    void myMethod()
    {
        //Error
        myclass[0].number = 1;
    }

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        myMethod();
        setContentView(new Panel(this));
    }
...other required methods
}

Logcat:

07-02 10:01:13.507: ERROR/AndroidRuntime(3203): FATAL EXCEPTION: main
07-02 10:01:13.507: ERROR/AndroidRuntime(3203): java.lang.RuntimeException: Unable to start activity ComponentInfo{graf.grafika/graf.grafika.aktivnost}: java.lang.NullPointerException
07-02 10:01:13.507: ERROR/AndroidRuntime(3203):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1736)
07-02 10:01:13.507: ERROR/AndroidRuntime(3203):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1752)
07-02 10:01:13.507: ERROR/AndroidRuntime(3203):     at android.app.ActivityThread.access$1500(ActivityThread.java:123)
07-02 10:01:13.507: ERROR/AndroidRuntime(3203):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:993)
07-02 10:01:13.507: ERROR/AndroidRuntime(3203):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-02 10:01:13.507: ERROR/AndroidRuntime(3203):     at android.os.Looper.loop(Looper.java:126)
07-02 10:01:13.507: ERROR/AndroidRuntime(3203):     at android.app.ActivityThread.main(ActivityThread.java:3997)
07-02 10:01:13.507: ERROR/AndroidRuntime(3203):     at java.lang.reflect.Method.invokeNative(Native Method)
07-02 10:01:13.507: ERROR/AndroidRuntime(3203):     at java.lang.reflect.Method.invoke(Method.java:491)
07-02 10:01:13.507: ERROR/AndroidRuntime(3203):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
07-02 10:01:13.507: ERROR/AndroidRuntime(3203):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
07-02 10:01:13.507: ERROR/AndroidRuntime(3203):     at dalvik.system.NativeStart.main(Native Method)
07-02 10:01:13.507: ERROR/AndroidRuntime(3203): Caused by: java.lang.NullPointerException
07-02 10:01:13.507: ERROR/AndroidRuntime(3203):     at graf.grafika.aktivnost.myMethod(aktivnost.java:17)
07-02 10:01:13.507: ERROR/AndroidRuntime(3203):     at graf.grafika.aktivnost.onCreate(aktivnost.java:23)
07-02 10:01:13.507: ERROR/AndroidRuntime(3203):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
07-02 10:01:13.507: ERROR/AndroidRuntime(3203):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1700)
07-02 10:01:13.507: ERROR/AndroidRuntime(3203):     ... 11 more

Could anyone please help me fix this? I'm pretty new to java programming btw.

回答1:

myclass[0] is null because it doesn't point to any object. That's why you get a null pointer. You should do myclass[0] = new MyClass();

public class aktivnost extends Activity {
    MyClass[] myclass = new MyClass[5];

    void myMethod()
    {
        //Error
        myclass[0] = new MyClass(); 
        myclass[0].number = 1;
    }

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        myMethod();
        setContentView(new Panel(this));
    }
...other required methods
}


回答2:

To actually put in 5 new classes in your array:

MyClass[] myclass = new MyClass[5];
for(int i = 0; i < 5; i++)
{
myclass[i] = new MyClass();
}


回答3:

MyClass[] myclass = new MyClass[5]; means your initializing the array object,not individual object,you should initialize individual objects which you want use them in array object like myclass[0] = new MyClass();