0

My goal is to make an application called "Shopping list".I have 3 tabs , first called: "View list" , second one is : "Add Market" and last one : "Add Product".I have 2 spinners on first and last tab, populated from an SQLite table called "DATABASE_MARKETTABLE". On "Add Market" i add markets to DATABASE_MARKETTABLE.

My problem is that the spinner from the last tab "Add Product" works just fine , it gets all data from DATABASE_MARKETTABLE (all markets added) but spinner from "View list" (first tab) it throws me an NULL POINTER EXCEPTION.

Here is my code:

DatabaseHelper.class :

public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub
    db.execSQL("CREATE TABLE " + DATABASE_PRODTABLE + " (" +
            KEY_PRODROWID + " INTEGER PRIMARY KEY, " +
            KEY_PRODNAME + " TEXT NOT NULL, " + 
            KEY_PRODAMOUNT + " TEXT NO NULL, " +
            KEY_AMOUNTTYPE + " TEXT NOT NULL, " +
            KEY_MARKETID + " LONG);"
            );
    db.execSQL("CREATE TABLE " + DATABASE_MARKETTABLE + " (" +
            KEY_MARKETROWID + " INTEGER PRIMARY KEY, " +
            KEY_MARKETNAME + " TEXT NOT NULL);"
            );

    }.....

             ArrayList<String> getMarkets(){
    String[] columns = new String[]{KEY_MARKETROWID, KEY_MARKETNAME};
    ArrayList<String> Markets = new ArrayList<String>();
    Cursor c= myDatabase.query(DATABASE_MARKETTABLE, columns, null, null, null, null, null);
    if(c.isLast()) {c.moveToFirst();}
    int iName = c.getColumnIndex(KEY_MARKETNAME);
    for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
        Markets.add(c.getString(iName));

    }
    return Markets;
}

And in AddProductActiviy.class(Add Product tab) :

public void onStart()
{
    DatabaseHelper info=new DatabaseHelper(this);
    super.onStart();
    try {
        info.open();
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    ArrayList<String> Markets=info.getMarkets();
    ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, Markets);
    spinDept.setAdapter(spinnerArrayAdapter);
    info.close();


}

And in ViewlistActivity.class (Viewlist Tab) if i use same code it gets me NULL POINTER EXCEPTION.

Honestly i don`t have any idea why isn;t working and i tried lost of solutions but nothing works.

My goal is to user that spinner to show only products linked with a market selected from spinner.

EDIT:

HERE ARE MY ERRORS:

11-16 00:20:27.594: E/AndroidRuntime(338): FATAL EXCEPTION: main
11-16 00:20:27.594: E/AndroidRuntime(338): java.lang.RuntimeException: Unable to start activity ComponentInfo{androidpack.namespace/androidpack.namespace.AndroidProjectActivity}: java.lang.RuntimeException: Unable to start activity ComponentInfo{androidpack.namespace/androidpack.namespace.ViewlistActivity}: java.lang.NullPointerException
11-16 00:20:27.594: E/AndroidRuntime(338):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
11-16 00:20:27.594: E/AndroidRuntime(338):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
11-16 00:20:27.594: E/AndroidRuntime(338):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
11-16 00:20:27.594: E/AndroidRuntime(338):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
11-16 00:20:27.594: E/AndroidRuntime(338):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-16 00:20:27.594: E/AndroidRuntime(338):  at android.os.Looper.loop(Looper.java:123)
11-16 00:20:27.594: E/AndroidRuntime(338):  at android.app.ActivityThread.main(ActivityThread.java:3683)
11-16 00:20:27.594: E/AndroidRuntime(338):  at java.lang.reflect.Method.invokeNative(Native Method)
11-16 00:20:27.594: E/AndroidRuntime(338):  at java.lang.reflect.Method.invoke(Method.java:507)
11-16 00:20:27.594: E/AndroidRuntime(338):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-16 00:20:27.594: E/AndroidRuntime(338):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-16 00:20:27.594: E/AndroidRuntime(338):  at dalvik.system.NativeStart.main(Native Method)
11-16 00:20:27.594: E/AndroidRuntime(338): Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{androidpack.namespace/androidpack.namespace.ViewlistActivity}: java.lang.NullPointerException
11-16 00:20:27.594: E/AndroidRuntime(338):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
11-16 00:20:27.594: E/AndroidRuntime(338):  at android.app.ActivityThread.startActivityNow(ActivityThread.java:1487)
11-16 00:20:27.594: E/AndroidRuntime(338):  at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
11-16 00:20:27.594: E/AndroidRuntime(338):  at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
11-16 00:20:27.594: E/AndroidRuntime(338):  at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:654)
11-16 00:20:27.594: E/AndroidRuntime(338):  at android.widget.TabHost.setCurrentTab(TabHost.java:326)
11-16 00:20:27.594: E/AndroidRuntime(338):  at android.widget.TabHost.addTab(TabHost.java:216)
11-16 00:20:27.594: E/AndroidRuntime(338):  at androidpack.namespace.AndroidProjectActivity.onCreate(AndroidProjectActivity.java:30)
11-16 00:20:27.594: E/AndroidRuntime(338):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-16 00:20:27.594: E/AndroidRuntime(338):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)


11-16 00:20:27.594: E/AndroidRuntime(338): Caused by: java.lang.NullPointerException
11-16 00:20:27.594: E/AndroidRuntime(338):  at androidpack.namespace.ViewlistActivity.onCreate(ViewlistActivity.java:51)
11-16 00:20:27.594: E/AndroidRuntime(338):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-16 00:20:27.594: E/AndroidRuntime(338):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)

It seems that in ViewlistActivity.class the spinDept.setAdapter(spinnerArrayAdapter); is the problem but i don't know why.

EDIT. HERE IS MY VIEWLISTACTIVITY.CLASS

public class ViewlistActivity extends Activity {
Spinner spinDept;
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.spinDept=(Spinner)findViewById(R.id.spinnerprost);


    // setare continut din layout
    setContentView(R.layout.viewlist);
    TextView tv=(TextView)findViewById(R.id.tvSQLinfo);

    //Afisare produse din baza de date
    DatabaseHelper info= new DatabaseHelper(ViewlistActivity.this);
    try {
        info.open();
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
  String data = info.getData();
    info.close();
    tv.setText(data);

    //----------------------------------------------
    try {
        info.open();
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    ArrayList<String> Markets=info.getMarkets();
    ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, R.layout.spinner, Markets);
    spinDept.setAdapter(spinnerArrayAdapter);
    info.close();

}
6
  • 1
    Have you set some debug points to see EXACTLY what is null? Also the stack trace tells you exactly what line of code is causing the NPE. Please post that as well. Commented Nov 15, 2011 at 22:53
  • I added my errors.It seems that spinDept.setAdapter(spinnerArrayAdapter); is the problem , in ViewlistActivity.class (Coresponding to Viewlist Tab) Commented Nov 15, 2011 at 23:01
  • Can you post the code from the ViewListActivity.class, since that is where you are saying you think the Null issue is originating from? Commented Nov 15, 2011 at 23:21
  • It's strange.Now it doesn;t throw any exception but simply doesn;t show anything in Viewlist Tab spinner , and Add Product tab spinner works just fine. Commented Nov 15, 2011 at 23:33
  • what about the layout file? You're using a different layout file in the ViewListActivity class. Since the code for setting up at then calling setAdapter is the same except for the layout being used, I would compare the two to see if something is missing or wrong. Commented Nov 15, 2011 at 23:44

2 Answers 2

1

This is most likely is not related directly to the issue. However:

KEY_MARKETID + " LONG);"

SQLite does not support LONG datatype. I wonder how this can work at all. Probably change to INTEGER.

Sign up to request clarification or add additional context in comments.

2 Comments

It works :P. Products and markets id selected from spinner in "Add Product" tab are shown in Viewlist Tab.The problem is why spinner from ViewList tab doesn't work , and spinner from Add Product works very well, and still...i am using same code and same class "getMarkets".
However , i am using the second db.execSQL related to DATABASE_MARKETTABLE.
1

You said:

It seems that in ViewlistActivity.class the spinDept.setAdapter(spinnerArrayAdapter); is the problem but i don't know why.

A Null Pointer Exception can mean only one thing: Something is null.

Either spinDept is not instantiated, or spinnerArrayAdapter is not instantiated. My guess is spinDept is NULL because I think setAdapter(null) is a valid call (to clear the adapter).

EDIT: If you debug all the way UP TO BUT NOT PAST that line of code, hover over the variables or add a watch, one of them is null.

1 Comment

+1 reading up on how to set breakpoints and inspect variables is essential. A lot of debugging and head scratching can be resolved by doing this simple check to verify your code is behaving as planned.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.