2

do you guys know what's the problem with this code? When I click the button to insert the data into sqlite external database. But it seems not working. The Error shows at the cv.put("name", txn.getName()); Maybe I used wrong way to write?

java page

 public void onClick(View v) {
            try {
                SQLiteDatabase db = mSQLiteHelper.getWritableDatabase();
                ContentValues cv = new ContentValues();
                cv.put("name", txn.getName());
                cv.put("address", txn.getAddress());
                cv.put("phone", txn.getPhone());
                db.insert("Table1", null, cv);
                db.close();
            } catch (Exception e) {
                e.printStackTrace();
            }

        }

Here is the Error exception says

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.example.waiku.work2.Model.getName()' on a null object reference

this is my OOP Model, I'm a bit confused how it works. But I followed the youtube tutorial...

public class Model {
    private int id;
    private String name;
    private String address;
    private int phone;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public int getPhone() {
        return phone;
    }

    public void setPhone(int phone) {
        this.phone = phone;
    }
}

here is my SQLite Helper

 public class SQLiteHelper extends SQLiteAssetHelper {
private static final String DB_NAME = "MyExternalDatabase.db";
private static final int DATABASE_VERSION = 1;

public SQLiteHelper(Context context) {
    super(context, DB_NAME, null, DATABASE_VERSION);
}
Leon Zaii
  • 163
  • 13
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Sunil Sunny Sep 05 '18 at 05:14
  • txn is null. Where are you getting that model class from? – Sunil Sunny Sep 05 '18 at 05:16
  • At the java page. I put `public static Model txn;` So It's Model. Inside Model have "name" "Address" and "Phone". @sunilsunny – Leon Zaii Sep 05 '18 at 06:47

2 Answers2

0

NullPointerException is thrown when an application attempts to use an object reference that has the null value.

Try this way,

try {
                SQLiteDatabase db = mSQLiteHelper.getWritableDatabase();
                ContentValues cv = new ContentValues();
                Model  modelOBJ = new Model();
                cv.put("name", modelOBJ.setName(your_name_textOBJ.getText().toString());
                cv.put("address", modelOBJ.setAddress(your_address_textOBJ.getText().toString());
                cv.put("phone",modelOBJ.setPhone(your_phone_textOBJ.getText().toString());
                db.insert("Table1", null, cv);
                db.close();
                } 
                catch (Exception e) 
               {
                e.printStackTrace();
               }
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
0

You got 'Model.getName()' on a null object reference may be you not initialize your model class. Do this :

 YourModelClassName txn= new YourModelClassName ();   // initialize your model class first
 txn.setName(youNameEditext.getText().toString()); // set entered name in model class
 public void onClick(View v) {
        try {
            SQLiteDatabase db = mSQLiteHelper.getWritableDatabase();
            ContentValues cv = new ContentValues();
            cv.put("name", txn.getName());  // get the entered name here.
            cv.put("address", txn.getAddress());
            cv.put("phone", txn.getPhone());
            db.insert("Table1", null, cv);
            db.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
Rishav Singla
  • 485
  • 4
  • 10