NullPointerException - 热门摆脱它?

时间:2017-11-20 20:24:53

标签: android nullpointerexception

用户活动java类文件

 package com.sumeet.codefactree.ndmcpl;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;



public class User_detail_activity extends AppCompatActivity implements View.OnClickListener{
    Button saveButton, cancelButton;
    EditText ownerName, unitName,type,address,contactNumber,mobileNumber;
    String ownerString, unitString, typeString, addressString, cnString,mnString;
    Intent i;

    UserDatabase userDatabase;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_user_detail);

        ownerName=(EditText)findViewById(R.id.et_ownerName);
        unitName=(EditText)findViewById(R.id.et_unitName);
        type=(EditText)findViewById(R.id.et_type);
        address=(EditText)findViewById(R.id.et_address);
        contactNumber=(EditText)findViewById(R.id.et_contactNumber);
        mobileNumber=(EditText)findViewById(R.id.et_mobileNumber);

        saveButton=(Button)findViewById(R.id.buttonSave);
        cancelButton=(Button)findViewById(R.id.buttonCancel);

        saveButton.setOnClickListener(this);
        cancelButton.setOnClickListener(this);


    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.buttonSave:
                ownerString= ownerName.getText().toString();
                unitString= unitName.getText().toString();
                typeString= type.getText().toString();
                addressString= address.getText().toString();
                cnString= contactNumber.getText().toString();
                mnString=mobileNumber.getText().toString();

                Log.d("User_detail_activity", " activity Owner Name:"+ ownerString+
                        " unit: "+unitString+ " type: "+ typeString+"address: "+ addressString+
                        " contactNo: "+cnString+" mo: "+mnString ); /* TS LOG IS WORKING WITH ALL DATA
                        REFERENCE BARABAR HAI KYA DEKH RAHA HU*/
                boolean insertData = userDatabase.addUserData(ownerString,unitString,typeString,addressString,cnString,mnString);
                if (insertData){
                    Toast.makeText(this,R.string.saveMessage,Toast.LENGTH_LONG).show();
                }else {
                    Toast.makeText(this,R.string.errorMessage,Toast.LENGTH_LONG).show();
                }

                break;


        }
    }
}

用于处理SQL任务的UserDatabase java类文件

  package com.sumeet.codefactree.ndmcpl;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;



public class UserDatabase extends SQLiteOpenHelper{
    SQLiteDatabase db;
    ContentValues contentValues;

    private static final String TABLE_NAME = "user";
    private static final String ID= "Id";
    private static final String OwnerName= "OwnerName";
    private static final String UnitName = "UnitName";
    private static final String Type ="Type";
    private static final String Address = "Address";
    private static final String ContactNumber= "ContactNumber";
    private static final String MobileNumber= "MobileNumber";


    public UserDatabase(Context context) {
        super(context, TABLE_NAME, null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
            String createQuery = "CREATE TABLE" +TABLE_NAME+ "("+ ID+ "INTEGER PRIMARY KEY AUTOINCREMENT, "+OwnerName+" TEXT, "
                    +UnitName+" TEXT, "+Type+" TEXT "+ Address+" TEXT, "
                    +ContactNumber+" TEXT, "+MobileNumber+" TEXT )";
            sqLiteDatabase.execSQL(createQuery);
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
        sqLiteDatabase.execSQL("DROP TABLE "+ TABLE_NAME);
        onCreate(sqLiteDatabase);
    }

    public boolean addUserData(String ownerName,String unitName, String type, String address, String contactNumber, String mobileNumber){
        db= this.getWritableDatabase();
        contentValues= new ContentValues();

        contentValues.put(OwnerName,ownerName);
        contentValues.put(UnitName,unitName);
        contentValues.put(Type,type);
        contentValues.put(Address,address);
        contentValues.put(ContactNumber,contactNumber);
        contentValues.put(MobileNumber,mobileNumber);
        Log.d("UserDatabase", "Owner Name:"+ ownerName+" unit: "+unitName+ " type: "+ type+"address: "+ address+" contactNo: "+contactNumber+" mo: "+mobileNumber );
        long result = db.insert(TABLE_NAME,null,contentValues);

        if (result == -1){
            return false;
        }else {
            return true;
        }

    }
}

错误

java.lang.NullPointerException: Attempt to invoke virtual method 'boolean com.sumeet.codefactree.ndmcpl.UserDatabase.addUserData(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String)' on a null object reference
                                                   at com.sumeet.codefactree.ndmcpl.User_detail_activity.onClick(User_detail_activity.java:61)
                                                   at android.view.View.performClick(View.java:5181)
                                                   at android.view.View$PerformClick.run(View.java:20887)
                                                   at android.os.Handler.handleCallback(Handler.java:739)
                                                   at android.os.Handler.dispatchMessage(Handler.java:95)
                                                   at android.os.Looper.loop(Looper.java:145)
                                                   at android.app.ActivityThread.main(ActivityThread.java:5942)
                                                   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:1400)
                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)

你们可以看到我得到Null指针异常, 在日志中我从设备获取所有值但仍然得到Null指针错误 你能帮我解决这个问题吗?为什么我得到"尝试调用虚方法错误"。 提前致谢

2 个答案:

答案 0 :(得分:1)

您必须实例化您的userDatabase,您可以在oncreate方法中执行此操作:

@Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_user_detail);
        userDatabase = new UserDatabase(this);

答案 1 :(得分:1)

userDatabase = new UserDatabase(this);

boolean insertData = userDatabase.addUserData(ownerString,unitString,typeString,addressString,cnString,mnString);
                if (insertData){
                    Toast.makeText(this,R.string.saveMessage,Toast.LENGTH_LONG).show();
                }else {
                    Toast.makeText(this,R.string.errorMessage,Toast.LENGTH_LONG).show();
                }
  

添加以上行。您错过了 userDatabase   由于您获得空引用的对象实例化。