-2

I have one model class for defining variables and when I'm trying to set some value from my MainActivity to Model class, I'm getting errors.

From my MainActivity.class

public class MainActivity extends Activity {
   private ListView productList;
   private OfferModel offerModel;

        @Override
    protected void onCreate (Bundle savedInstanceState){

        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        setContentView(R.layout.activity_main);
        productList = (ListView)findViewById(R.id.listView1);

        ...many codes here...

        productList.setOnItemClickListener(new OnItemClickListener() {

            @SuppressWarnings("deprecation")
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                    long arg3) {

                    offerModel.setProductName(arg0.getItemAtPosition(position).toString());


            }
        });

    }}

And this is from my Model class

public class OfferModel {


    private String productName;
    public String getProductName(){return this.productName;}
    public void setProductName(String pName){ this.productName=pName;}
}

And here is my Log result

08-01 11:17:46.446: W/dalvikvm(2305): threadid=1: thread exiting with uncaught exception (group=0xa4b70648)
08-01 11:17:46.446: E/AndroidRuntime(2305): FATAL EXCEPTION: main
08-01 11:17:46.446: E/AndroidRuntime(2305): java.lang.NullPointerException
08-01 11:17:46.446: E/AndroidRuntime(2305):     at stok.kontrol.noyagrup.MainActivity$2.onItemClick(MainActivity.java:71)
08-01 11:17:46.446: E/AndroidRuntime(2305):     at android.widget.AdapterView.performItemClick(AdapterView.java:298)
08-01 11:17:46.446: E/AndroidRuntime(2305):     at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
08-01 11:17:46.446: E/AndroidRuntime(2305):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:2788)
08-01 11:17:46.446: E/AndroidRuntime(2305):     at android.widget.AbsListView$1.run(AbsListView.java:3463)
08-01 11:17:46.446: E/AndroidRuntime(2305):     at android.os.Handler.handleCallback(Handler.java:730)
08-01 11:17:46.446: E/AndroidRuntime(2305):     at android.os.Handler.dispatchMessage(Handler.java:92)
08-01 11:17:46.446: E/AndroidRuntime(2305):     at android.os.Looper.loop(Looper.java:137)
08-01 11:17:46.446: E/AndroidRuntime(2305):     at android.app.ActivityThread.main(ActivityThread.java:5103)
08-01 11:17:46.446: E/AndroidRuntime(2305):     at java.lang.reflect.Method.invokeNative(Native Method)
08-01 11:17:46.446: E/AndroidRuntime(2305):     at java.lang.reflect.Method.invoke(Method.java:525)
08-01 11:17:46.446: E/AndroidRuntime(2305):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
08-01 11:17:46.446: E/AndroidRuntime(2305):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-01 11:17:46.446: E/AndroidRuntime(2305):     at dalvik.system.NativeStart.main(Native Method)

What I'm missing ?

Thanks in advance.

  • possible duplicate of [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) – flotothemoon Aug 01 '14 at 11:25

3 Answers3

6

OfferModel offerModel is not instantiated and is null. So, when you call offerModel.setProductName(arg0.getItemAtPosition(position).toString()); you get an exception.

ABucin
  • 869
  • 5
  • 17
  • 2
    What? Wrong! this.productName = pName won't throw any exception. offerModel.setProductName(arg0.getItemAtPosition(position).toString()); will throw it. – flotothemoon Aug 01 '14 at 11:23
2

You have not initialized private OfferModel offerModel; in you code and you are using offerModel.setProductName(arg0.getItemAtPosition(position).toString()); as it will throw NPE

So first initialize it moreover productName is null which is not yet initialized but you can initialize it by the use of parameterized constructor (You have to add constructor first).

offerModel=new OfferModel(productNameValue);//Pass initial value of `productName`
//So it will first initialize your productName 

And Add constructor

public OfferModel(String pro){
   productName=pro;
}

Or from the code I can see you love this keyword, so you can do it like this

public OfferModel(String productName){
   this.productName=productName;
}
ΔȺȾΔ
  • 21,571
  • 10
  • 52
  • 80
1
         productList.setOnItemClickListener(new OnItemClickListener() {

        @SuppressWarnings("deprecation")
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                long arg3) {
                 offerModel= new OfferModel();
                 offerModel.setProductName(arg0.getItemAtPosition(position).toString());


        }
    });
Dhwanik Gandhi
  • 675
  • 6
  • 12