尝试在空对象引用上调用方法

时间:2016-01-19 08:32:01

标签: android android-logcat

我正在编写一个Android应用程序,我收到此错误。我几乎评论了我的代码的每一部分,并且唯一未被评论的部分如下:

public class MainActivity extends AppCompatActivity {

int digi=0;
public PointCalculator pointCalculator;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    digi = pointCalculator.digicalculator();
    Toast.makeText(getApplicationContext(), digi, Toast.LENGTH_LONG).show();

}

和PointCalculator:

  public  int digicalculator()
{
    int digipoints=0;
    digipoints = digipoints + 30;
    return digipoints;
}

这是logcat:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.neshat.test/com.example.neshat.test.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'int com.example.neshat.test.PointCalculator.digicalculator()' 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 'int com.example.neshat.test.PointCalculator.digicalculator()' on a null object reference
        at com.example.neshat.test.MainActivity.onCreate(MainActivity.java:18)
        at android.app.Activity.performCreate(Activity.java:5933)
        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)

我无法理解导致问题的原因。

4 个答案:

答案 0 :(得分:2)

您尚未为pointCalculator对象分配内存:

pointCalculator = new PointCalculator();
digi = pointCalculator.digicalculator();

答案 1 :(得分:2)

您需要先初始化PointCalculator

public class MainActivity extends AppCompatActivity {

int digi=0;
public PointCalculator pointCalculator = new PointCalculator(); //<--- Initialized
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    digi = pointCalculator.digicalculator();
    Toast.makeText(getApplicationContext(), digi, Toast.LENGTH_LONG).show();

}

答案 2 :(得分:2)

您尚未初始化PointCalculator。

public PointCalculator pointCalculator;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    pointCalculator = new PointCalculator(); // Here it's initialised 
    digi = pointCalculator.digicalculator();
    Toast.makeText(getApplicationContext(), digi, Toast.LENGTH_LONG).show();

}

答案 3 :(得分:0)

您正在使用对象 PointCalculator 而未初始化它,因此这是您的关注点。尝试在onCreate中初始化它:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    pointCalculator = new PointCalculator(); //init
    digi = pointCalculator.digicalculator();
    Toast.makeText(getApplicationContext(), digi, Toast.LENGTH_LONG).show();

}