0

my code will crash if i not comment statement else if (message.equals("holiday")) on postexecute tell me why is not go further if not euqal this line (message.equals("holiday")) why not print "School is off today. Reason: if i comment else if (message.equals("holiday")) this code app work fine check my if else stateement please

          String message;

        public class getDataTask extends AsyncTask<Void, Void, Void> {

    getDataTask() {

    }

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
         yourBoolean=false;
    }

    @Override
    protected Void doInBackground(Void... arg0) {
        // TODO Auto-generated method stub

        displayData();
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub
        yourBoolean=true ;

        if ((Category_ID.size() > 0) ) {



                            listCategory.setAdapter(cla);
                            cla.notifyDataSetChanged() ;
                            listCategory.invalidateViews();
                            menu_nametxt.setText(mVal2);

                        }





else if (message.equals("holiday")) 
{
    menu_nametxt.setText("No menu available .");
    listCategory.setVisibility(View.GONE);
}   
        else
        menu_nametxt.setText("School is off today. Reason: "+mVal3);
    listCategory.setVisibility(View.GONE);

             }


                   private void displayData() {



        Cursor mCursor3 =  db.selectQuery("SELECT * FROM uss_vacation WHERE calendar_id);


    if (mCursor3.moveToFirst()) {
        do {


       Vacation_Date.add(mCursor3.getString(mCursor3.getColumnIndex("date")));

      if(mCursor3.getString(mCursor3.getColumnIndex("date")).equals(mydate))
    {
    message = "holiday";
           String  mVal  ;
    mVal = (mCursor3.getString(mCursor3.getColumnIndex("title")));

    mVal2 = mVal.toString();


    mCursor3.close();
    return;

    }


        } while (mCursor3.moveToNext());
    }

          mCursor3.close();
             }




              if i comment this code application print "School is off today. Reason: text


            else if (message.equals("holiday")) 
 // {
 //     menu_nametxt.setText("No menu available .");
 //     listCategory.setVisibility(View.GONE);
 // }   

1 Answers1

0

Since message is null when the app is run for the first time, when the if-else statement reaches else if (message.equals("holiday"), it will throws NullPointerException instead of false and crashing the app.

see SO: Java null String equals result

Consider initialize the message with empty string instead. (i.e. message = "";).

OR

Change the order of checking to else if ("holiday".equals(message)).

Community
  • 1
  • 1
Andrew T.
  • 4,701
  • 8
  • 43
  • 62