Search code examples
androidtoast

Error in Toast with null


My code is like this

public Database(Context context) {
    super(context, dbname, null, dbversion);
    try{
      db=getWritableDatabase();
      // TODO Auto-generated constructor stub
      if (db.isOpen()){
        Toast.makeText(null, "Database is open", Toast.LENGTH_LONG).show();
      } else {      
        Toast.makeText(null, "Database is Closed", Toast.LENGTH_LONG).show();
      }
    } catch(Exception e) {
      Log.e(dbname, e.getMessage());        
    }
}

I am getting exception a toast and log cat shows

java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.example.manager/com.example.manager.MainActivity}:
java.lang.NullPointerException: println needs a message

Solution

  • Replace this

     Toast.makeText(null, "Database is open", Toast.LENGTH_LONG).show();
    

    to this

     Toast toast = Toast.makeText(context, "Database is open", Toast.LENGTH_LONG);
     toast.show();