尝试在空对象引用上调用方法
attempt to invoke method on a null object reference
我正在编写 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();
}
和点计算器:
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.access0(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.access0(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)
我不明白是什么导致了这个问题。
您还没有为 pointCalculator 对象分配内存:
pointCalculator = new PointCalculator();
digi = pointCalculator.digicalculator();
您需要先初始化 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();
}
您还没有初始化您的 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();
}
您正在使用对象 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();
}
我正在编写 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();
}
和点计算器:
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.access0(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.access0(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)
我不明白是什么导致了这个问题。
您还没有为 pointCalculator 对象分配内存:
pointCalculator = new PointCalculator();
digi = pointCalculator.digicalculator();
您需要先初始化 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();
}
您还没有初始化您的 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();
}
您正在使用对象 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();
}