0

So I created a SQLiteDatabase and I made a method to get the information from that database but whenever I try to access it my app stops working. Here is the method I used to get information, this is where the exception happens:

public Product getProducts(Context ctx) {
    Product products = new Product();
    try {
        ourHelper = new DbHelper(ctx);
        db = ourHelper.getReadableDatabase();
        String projection[] = { DatabaseContract.KEY_ROWID,
                DatabaseContract.KEY_NAME, DatabaseContract.KEY_UM,
                DatabaseContract.KEY_SPECIFICATIONS };
        c = db.query(DatabaseContract.DATABASE_TABLE, projection, null,
                null, null, null, null);
        if (c.moveToFirst()) {
            do {
                products.setID(c.getString(c
                        .getColumnIndex(DatabaseContract.KEY_ROWID)), c
                        .getPosition());
                products.setName(c.getString(c
                        .getColumnIndex(DatabaseContract.KEY_NAME)), c
                        .getPosition());
                products.setUM(c.getString(c
                        .getColumnIndex(DatabaseContract.KEY_UM)), c
                        .getPosition());
                products.setOtherSpecs(
                        c.getString(c
                                .getColumnIndex(DatabaseContract.KEY_SPECIFICATIONS)),
                        c.getPosition());
            } while (c.moveToNext());
        }
        ourHelper.close();
        db.close();
        return products;
    } catch (Exception e) {
        System.out.println(Error.EXCEPTION_GETPRODUCTS);
    }
    return null;
}

And here is the Product class:

public class Product {

private String ID[];
private String name[];
private String UM[];
private String otherSpecifications[];

public void setID(String setID, int index){
    ID[index] = setID;
}
public void setName(String setName, int index){
    name[index] = setName;
}
public void setUM(String setUM, int index){
    UM[index] = setUM;
}
public void setOtherSpecs(String setSpecs, int index){
    otherSpecifications[index] = setSpecs;
}
public String[] getID(){
    return ID;
}
public String[] getName(){
    return name;
}
public String[] getUM(){
    return UM;
}
public String[] getSpecs(){
    return otherSpecifications;
}

}

Also if you want this is the main Activity class:

public class MainActivity extends ActionBarActivity implements OnClickListener {

EditText ID, name, UM, specs;
Button save, load;
TextView data;
Database myDatabase;
Product products;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ID = (EditText)findViewById(R.id.etID);
    name = (EditText)findViewById(R.id.etName);
    UM = (EditText)findViewById(R.id.etUM);
    specs = (EditText)findViewById(R.id.etSpecs);
    save = (Button)findViewById(R.id.bSave);
    load = (Button)findViewById(R.id.bLoad);
    data = (TextView)findViewById(R.id.tvData);

    save.setOnClickListener(this);
    load.setOnClickListener(this);

    myDatabase = new Database();
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch(v.getId()){
    case R.id.bSave:
        myDatabase.createProduct(this, ID.getText().toString(), name.getText().toString(), UM.getText().toString(), specs.getText().toString());
        break;
    case R.id.bLoad:
        products = myDatabase.getProducts(this);
        for(String x : products.getID())
            for(String xx : products.getName())
                for(String xxx : products.getUM())
                    for(String xxxx : products.getSpecs())
                        data.setText(x +"\t"+ xx +"\t"+ xxx +"\t"+ xxxx + "\n");
        break;
    }
}

Here is the log:

08-31 18:17:39.811: E/AndroidRuntime(277): FATAL EXCEPTION: main
08-31 18:17:39.811: E/AndroidRuntime(277): java.lang.NullPointerException
08-31 18:17:39.811: E/AndroidRuntime(277):  at com.example.linicdecor.MainActivity.onClick(MainActivity.java:48)
08-31 18:17:39.811: E/AndroidRuntime(277):  at android.view.View.performClick(View.java:2408)
08-31 18:17:39.811: E/AndroidRuntime(277):  at android.view.View$PerformClick.run(View.java:8816)
08-31 18:17:39.811: E/AndroidRuntime(277):  at android.os.Handler.handleCallback(Handler.java:587)
08-31 18:17:39.811: E/AndroidRuntime(277):  at android.os.Handler.dispatchMessage(Handler.java:92)
08-31 18:17:39.811: E/AndroidRuntime(277):  at android.os.Looper.loop(Looper.java:123)
08-31 18:17:39.811: E/AndroidRuntime(277):  at android.app.ActivityThread.main(ActivityThread.java:4627)
08-31 18:17:39.811: E/AndroidRuntime(277):  at java.lang.reflect.Method.invokeNative(Native Method)
08-31 18:17:39.811: E/AndroidRuntime(277):  at java.lang.reflect.Method.invoke(Method.java:521)
08-31 18:17:39.811: E/AndroidRuntime(277):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-31 18:17:39.811: E/AndroidRuntime(277):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-31 18:17:39.811: E/AndroidRuntime(277):  at dalvik.system.NativeStart.main(Native Method)
gdany
  • 104
  • 7
  • Log your exceptions to see the helpful stacktrace you should include in your question. – laalto Aug 31 '14 at 17:50
  • See [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it). – CL. Sep 01 '14 at 06:59

0 Answers0