0

I am having a page with another layout inflated for 4 times for now,I have to populate spinner of the inflated layout with values from database,I am trying but getting null pointer exception.

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.drsdetail);

    changeCode = (TextView) findViewById(R.id.changeCode);
    generate = (Button) findViewById(R.id.generate);
    fromVessel = (Button) findViewById(R.id.fromVessel);
    toVessel = (Button) findViewById(R.id.toVessel);
    overTime = (EditText) findViewById(R.id.overTimeEdit);
    offShoreTime = (EditText) findViewById(R.id.offShoreEdit);
    onShoreTime = (EditText) findViewById(R.id.onShoreEdit);
    LinearLayout parent = (LinearLayout) findViewById(R.id.parentLayout);
    spinnerValues = (Spinner) findViewById(R.id.spinnerValues);
    loadSpinnerData();

    LayoutInflater inflater = (LayoutInflater)                   getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                          for(int i=0;i<4;i++){
    LinearLayout row = new LinearLayout(this);
    View layout_number = inflater.inflate(R.layout.layoutinflate, parent,
            false);

    row.addView(layout_number);
    parent.addView(row);
          }
    ArrayAdapter<String> adapterDataValues = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, dataValues);
    adapterDataValues
            .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    spinnerValues.setAdapter(adapterDataValues);
}

private void loadSpinnerData() {
    dataValues.clear();
    try {
        System.out.println("in try");
        DBHelper dbHelper = new DBHelper(DRSDetail.this);
        dbHelper.openDataBase();
        SQLiteDatabase db = dbHelper.getReadableDatabase();

        // project number
        Cursor crData = db.rawQuery("SELECT DRS_ID FROM  DRS_TBL", null);
        if (crData.getCount() > 0) {
            while (crData.moveToNext()) {
                dataValues.add(crData.getString(0));
                System.out.println("value of cr.getString "
                        + crData.getString(0));

            }
        }

        crData.close();
    } catch (Exception e) {
        System.out.println("exception");
    }

}

logcat details:

    03-21 11:35:49.892: E/AndroidRuntime(15823): FATAL EXCEPTION: main
    03-21 11:35:49.892: E/AndroidRuntime(15823): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.kongsberg/com.kongsberg.ui.DRSDetail}: 
    java.lang.NullPointerException 03-21 11:35:49.892: E/AndroidRuntime(15823):     at  
    android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
    03-21 11:35:49.892: E/AndroidRuntime(15823):    at 
    android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
    03-21 11:35:49.892: E/AndroidRuntime(15823):    at 
    android.app.ActivityThread.access$600(ActivityThread.java:130)
    03-21 11:35:49.892: E/AndroidRuntime(15823):    at 
    android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
    03-21 11:35:49.892: E/AndroidRuntime(15823):    at 
    android.os.Handler.dispatchMessage(Handler.java:99)
    03-21 11:35:49.892: E/AndroidRuntime(15823):    at 
    android.os.Looper.loop(Looper.java:137)
    03-21 11:35:49.892: E/AndroidRuntime(15823):    at 
    android.app.ActivityThread.main(ActivityThread.java:4745)
    03-21 11:35:49.892: E/AndroidRuntime(15823):    at 
    java.lang.reflect.Method.invokeNative(Native Method)
    03-21 11:35:49.892: E/AndroidRuntime(15823):    at 
    java.lang.reflect.Method.invoke(Method.java:511)
    03-21 11:35:49.892: E/AndroidRuntime(15823):    at                    com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
    03-21 11:35:49.892: E/AndroidRuntime(15823):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
    03-21 11:35:49.892: E/AndroidRuntime(15823):    at dalvik.system.NativeStart.main(Native Method)
    03-21 11:35:49.892: E/AndroidRuntime(15823): Caused by: java.lang.NullPointerException
    03-21 11:35:49.892: E/AndroidRuntime(15823):    at com.kongsberg.ui.DRSDetail.onCreate(DRSDetail.java:66)
    03-21 11:35:49.892: E/AndroidRuntime(15823):    at android.app.Activity.performCreate(Activity.java:5008)
    03-21 11:35:49.892: E/AndroidRuntime(15823):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
5
  • post your logcat then. Commented Mar 21, 2014 at 6:12
  • I think your dataValues is null.. Commented Mar 21, 2014 at 6:15
  • no I checked that its getting values Commented Mar 21, 2014 at 6:16
  • 1
    Is your spinner belong to your inflate layout? Commented Mar 21, 2014 at 6:17
  • @SimplePlan yes spinner is from inflated layout Commented Mar 21, 2014 at 6:17

2 Answers 2

3

Change this

 spinnerValues = (Spinner) findViewById(R.id.spinnerValues);

to

 spinnerValues = (Spinner) layout_number.findViewById(R.id.spinnerValues);

It's becoz your Spinner belong to your inflated layout. and you have to initialized Spinner before inflated layout and it is NULL at this position.

So initialized your Spinner after your View inflated like:

View layout_number = inflater.inflate(R.layout.layoutinflate, parent,
        false);

spinnerValues = (Spinner) layout_number.findViewById(R.id.spinnerValues);
Sign up to request clarification or add additional context in comments.

1 Comment

@MD hi i'm having same problem I tried like this final Spinner problems = (Spinner) layout.input_dialog.findViewById((R.id.spinner_problems)); i got this error Cannot invoke findViewById(int) on the primitive type int plz suggest
1

spinner is from inflated layout

So move this

spinnerValues = (Spinner) findViewById(R.id.spinnerValues);

after your inflate() loop. Before it the spinner is not yet in your activity view hierarchy and a null is returned.

Comments

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.