尝试在null对象上调用虚方法

时间:2016-01-19 18:52:45

标签: android android-logcat

我正在接收此错误。以下是相关的代码:

 int digi=0;
public Pointcalculator pointCalculator = new Pointcalculator();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    digi = pointCalculator.digicalculator();

    Toast.makeText(getApplicationContext(),  Integer.toString(digi), Toast.LENGTH_LONG).show();

}

这是另一件作品:

     public  int digicalculator()
    {
        //int digipoints=0;
        // Log.d("ggggggggg","digicallll");
        fetchPoints("hh");
        digipoints = digipoints + 30;
        return digipoints;
    }


    private void fetchPoints(String email)
    {
        // Tag used to cancel the request
        String tag_string_req = "fetch_points";
        Log.d("ggggggggg","digicallll");

        Toast.makeText(getApplicationContext(),"in fetch", Toast.LENGTH_LONG).show();
          pD.setMessage("Fetching Points ...");
        showDialog(10);

/*        StringRequest strReq = new StringRequest(Request.Method.POST, AppConfig.URL_FETCH_USER_POINTS, new Response.Listener<String>()
        {

            @Override
            public void onResponse(String response)
            {
                Log.d(TAG, "FETCH RESPONSE: " + response.toString());
                Toast.makeText(getApplicationContext(), response.toString(), Toast.LENGTH_LONG).show();

                //hideDialog(10);

                try {
                    JSONObject jObj = new JSONObject(response);
                    // boolean error = jObj.getBoolean("error");
                    //  if (!error)
                    //  {
                    // Now store the user in sqlite
                    String totalpts = (jObj.getString("totalpoints"));
                    String digipts = (jObj.getString("digipoints"));
                    Toast.makeText(getBaseContext(), "totalpoints : "+totalpts, Toast.LENGTH_SHORT).show();
                    Toast.makeText(getBaseContext(), "digipoints : "+digipts, Toast.LENGTH_SHORT).show();

                    try
                    {
                        totalpoints = Integer.parseInt(totalpts);
                        digipoints = Integer.parseInt(digipts);
                    }
                    catch(NumberFormatException nfe)
                    {
                        System.out.println("Could not parse " + nfe);
                    }


                    // Inserting row in users table
                    // db.addUser(name, email, uid, created_at);

                    Toast.makeText(getApplicationContext(), "Points successfully retrieved!", Toast.LENGTH_LONG).show();

                }
                catch (JSONException e)
                {
                    e.printStackTrace();
                }

            }
        }, new Response.ErrorListener()
        {

            @Override
            public void onErrorResponse(VolleyError error)
            {
                Log.e(TAG, "Points retriving Error: " + error.toString());
                Toast.makeText(getApplicationContext(),
                        error.getMessage(), Toast.LENGTH_LONG).show();
                //hideDialog();
            }
        }) {

            @Override
            protected Map<String, String> getParams()
            {
                // Posting params to register url
                Map<String, String> params = new HashMap<String, String>();
                params.put("email", "hh");



                return params;
            }

        };*/

        // Adding request to request queue
     //   AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
    }

我无法理解导致此错误的原因。正如您所看到的,我评论了方法中的所有提取信息部分。而且我不能同时调用pdialoge()和Toast函数。

这是logcat:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
        at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:105)
        at com.example.neshat.test.Pointcalculator.fetchPoints(Pointcalculator.java:64)
        at com.example.neshat.test.Pointcalculator.digicalculator(Pointcalculator.java:52)
        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)

1 个答案:

答案 0 :(得分:0)

Toast的第一个参数是一个Context,我认为你应该将它作为YourActivity.thisMainActivity.this传递给它,如果它在你的MainActivity中就像class SubCategory(models.Model): objects = CountryQuerySet.as_manager() id = models.AutoField(primary_key=True,db_column='sub_category_id') name = models.TextField() female_items_in_category = models.BooleanField() male_items_in_category = models.BooleanField() kids_items_in_category = models.BooleanField() category = models.ForeignKey('Category') class Meta: managed = True db_table = Constants().SUBCATEGORY 那样,给它一个尝试。