需要有关Android NullPointer问题的帮助

时间:2011-07-02 10:43:33

标签: java android class methods nullpointerexception

我正在制作游戏,但我遇到了这个恼人的问题。试图google它但到目前为止没有运气。我在自己的文件中定义了一个类(这里的代码不是我的游戏,而是一个有相同错误的简化版):

package graf.grafika;

public class MyClass {
    int number;
}

问题出在myMethod(应用程序意外停止):

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

有人可以帮我解决这个问题吗?我不熟悉java编程。

3 个答案:

答案 0 :(得分:1)

在数组中实际放入5个新类:

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

答案 1 :(得分:0)

myclass[0]为null,因为它不指向任何对象。这就是你得到空指针的原因。你应该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 :(得分:0)

MyClass [] myclass = new MyClass [5];  表示初始化数组对象,而不是单个对象,您应该初始化您希望在数组对象中使用它们的单个对象,如myclass [0] = new MyClass();