-1

I have a SQLite database to store items in cart. I need to get the count of products in cart corresponding to each user and set it to a textview,each time the user clicks addtocart button.But my code throws an null point exception.please help me.

I am getting nullpointer Exception in line 109: if (!crtno.getText().toString().equals(""))

code

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.product_dtls);
    crtno=(TextView)findViewById(R.id.crtno);
    imgbtn=(ImageButton)findViewById(R.id.cartimg);
    add2cart=(Button)findViewById(R.id.add2cart);

    DataBaseHandler dbh = new DataBaseHandler(this);
    SQLiteDatabase db = dbh.getWritableDatabase();

    Intent in = getIntent();
    Bundle bn = in.getExtras();
    Bundle bun=in.getExtras();
    final String dtl=bun.getString("key");
    nme = bn.getString("name");

    Cursor cr = db.rawQuery("SELECT * FROM product WHERE pname = '"+nme+"'", null);

    while(cr.moveToNext())
    {
        String name = cr.getString(cr.getColumnIndex("pname"));
        String pr1price = cr.getString(cr.getColumnIndex("pprice"));
        String prspc=cr.getString(cr.getColumnIndex("pspec"));
        String prfeature=cr.getString(cr.getColumnIndex("pfeature"));
        pname = name;
        prprice = pr1price;
        pspec=prspc;
        pfeature=prfeature;
    }
    name.setText(pname);
    price.setText("Rs " +prprice + "/-");
    specification.setText(pspec);
    feature.setText(pfeature);


    add2cart.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            boolean incart=false;
            String nm=name.getText().toString();

            mydb=Product_Details.this.openOrCreateDatabase("addcart", MODE_PRIVATE, null);
            mydb.execSQL("CREATE TABLE IF NOT EXISTS add2cart(usr TEXT,img BLOB,pnme TEXT,prate NUMERIC,pqty NUMERIC,ptotl NUMERIC)");
            Cursor cur=mydb.rawQuery("select * from add2cart where pnme='"+nm+"' AND usr='"+dtl+"'",null);

            if (cur.moveToFirst()){
                String prdname=cur.getString(cur.getColumnIndex("pnme"));

                if (nm.equals(prdname)){
                    add2cart.setText("Already in Cart");
                    incart=true;
                }
            }

            if(incart==false){
                mydb.execSQL("INSERT INTO add2cart (usr,pnme,prate)VALUES('"+dtl+"','"+nm+"','"+prprice+"')");
                Toast.makeText(getApplicationContext(),"added to cart",Toast.LENGTH_SHORT).show();                  Cursor crsr=mydb.rawQuery("select pnme from add2cart where usr='"+dtl+"'", null);

                int count=0;
                if (!crtno.getText().toString().equals("")) {

                    count=crsr.getCount();                      
                }
                crtno.setText(Integer.toString(count));

            }

        }
    });
}
}
panda_heart_3
  • 227
  • 2
  • 17
  • http://stackoverflow.com/q/23353173/2389078 – DroidDev Feb 02 '15 at 06:18
  • Either Debug your code by putting the break point on onClick method, or try to use Android Logcat to see on which line it is getting the null pointer exception so that some more specific information can be obtained about your issue. – Arslan Sohail Feb 02 '15 at 06:20
  • post your logcat or whole code – user1140237 Feb 02 '15 at 06:22
  • @ArslanSohail I found the line that throws exception.But don't know how to solve – panda_heart_3 Feb 02 '15 at 06:37
  • Put a break point on that line and analyse the values in the debugger window something is missing here any of your class member can be empty look for that member and properly initialize or assigned it before the on-click method gets called. – Arslan Sohail Feb 02 '15 at 06:46
  • `ctrno` appears to be null. Does your `product_dtls` layout really have a view with such id? – laalto Feb 02 '15 at 06:49
  • @laalto yes.I have a textview with that id – panda_heart_3 Feb 02 '15 at 06:51
  • 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) – Simon Feb 02 '15 at 07:19
  • so your java code line 109 equals this start Activity(in); now do you have buy_nw.class inside your project and also does you declare it in manifest as an activity because log-cat is saying there is something wrong with in either you don't have buy_nw.class in your project or it is not declared in manifest,cross check it. – Arslan Sohail Feb 02 '15 at 07:22
  • String Blank=" "; crtno.getText().toString().equals(Blank) – IntelliJ Amiya Feb 02 '15 at 07:28

2 Answers2

1

Try like

crtno.getText().toString().equals("")

instead of

crtno.equals("")

Because crtno.getText().toString().equals("") is the command for checking equality of the text inside the textview.

Remees M Syde
  • 2,564
  • 1
  • 19
  • 42
  • While this code sample may answer the question, it lacks explanation. As it stands now, it adds no value, and stands the change of being downvoted / deleted. Please add some explanation what is does and why it is a solution for the problem of the OP. – oɔɯǝɹ Feb 02 '15 at 10:12
0

At first you should first pull string displayed in textview and it's done as by:

getText()

Return the text the TextView is displaying. 

so your way should be

crtno.getText().toString().equals("")

also try this

crtno.getText().toString().equals(null)
surhidamatya
  • 2,419
  • 32
  • 56
  • @paypaluser edtPinOne.getText().toString().equalsIgnoreCase("") || edtPinOne.getText().toString().equalsIgnoreCase(null) try this once. – surhidamatya Feb 02 '15 at 09:27
  • @paypaluser my other suggestion would be check ur condition 1ce again. Look by keeping that code inside onclick method rather than inside any condition. – surhidamatya Feb 02 '15 at 09:35
  • then you must have set condition in wrong way cause I am using same code everywhere – surhidamatya Feb 03 '15 at 12:10