Repository: apache/shardingsphere
Issue: CachedDatabaseMetaData.getTables throw NullPointerException when use MasterSlaveDataSource

## Bug Report

**For English only**, other languages will not accept.

Before report a bug, make sure you have:

- Searched open and closed [GitHub issues](https://github.com/sharding-sphere/sharding-sphere/issues).
- Read documentation: [ShardingSphere Doc](http://shardingsphere.io/document/current/en/overview/).

Please pay attention on issues you submitted, because we maybe need more details. 
If no response **more than 7 days** and we cannot reproduce it on current information, we will **close it**.

Please answer these questions before submitting your issue. Thanks!

### Which version of ShardingSphere did you use?
4.0.0-RC1
### Which project did you use? Sharding-JDBC or Sharding-Proxy?
Sharding-JDBC
### Expected behavior
Return table names
### Actual behavior
Get npe
### Reason analyze (If you can)
asset shardingRule is null
### Steps to reproduce the behavior, such as: SQL to execute, sharding rule configuration, when exception occur etc.
DatabaseMetaData databaseMetaData = connection.getMetaData();
databaseMetaData.getTables(null, null, "ACT_ID_PROPERTY", new String[]{ "TABLE" });
### Example codes for reproduce this issue (such as a github link).


Changed files:
sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/jdbc/core/datasource/metadata/ResultSetReturnedDatabaseMetaData.java
sharding-jdbc/sharding-jdbc-core/src/test/java/org/apache/shardingsphere/shardingjdbc/jdbc/core/datasource/metadata/CachedDatabaseMetaDataTest.java
sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/jdbc/core/resultset/DatabaseMetaDataResultSet.java

diff --git a/sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/jdbc/core/datasource/metadata/ResultSetReturnedDatabaseMetaData.java b/sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/jdbc/core/datasource/metadata/ResultSetReturnedDatabaseMetaData.java
index 3db568a57c2..13c2a12d1e1 100644
--- a/sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/jdbc/core/datasource/metadata/ResultSetReturnedDatabaseMetaData.java
+++ b/sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/jdbc/core/datasource/metadata/ResultSetReturnedDatabaseMetaData.java
@@ -263,7 +263,10 @@ public abstract class ResultSetReturnedDatabaseMetaData extends WrapperAdapter i
     }
     
     private String getActualTableNamePattern(final String tableNamePattern) {
-        return null == tableNamePattern ? tableNamePattern : (shardingRule.findTableRule(tableNamePattern).isPresent() ? "%" + tableNamePattern + "%" : tableNamePattern);
+        if (null == tableNamePattern || null == shardingRule) {
+            return tableNamePattern;
+        }
+        return shardingRule.findTableRule(tableNamePattern).isPresent() ? "%" + tableNamePattern + "%" : tableNamePattern;
     }
     
     private String getActualTable(final String table) {
diff --git a/sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/jdbc/core/resultset/DatabaseMetaDataResultSet.java b/sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/jdbc/core/resultset/DatabaseMetaDataResultSet.java
index b71bf21f0a1..5aff76a5d4b 100644
--- a/sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/jdbc/core/resultset/DatabaseMetaDataResultSet.java
+++ b/sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/jdbc/core/resultset/DatabaseMetaDataResultSet.java
@@ -30,6 +30,7 @@ import java.sql.Time;
 import java.sql.Timestamp;
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Iterator;
@@ -100,7 +101,7 @@ public final class DatabaseMetaDataResultSet extends AbstractUnsupportedDatabase
         for (int i = 1; i <= columnLabelIndexMap.size(); i++) {
             if (tableNameColumnIndex == i) {
                 String tableName = resultSet.getString(i);
-                Collection<String> logicTableNames = shardingRule.getLogicTableNames(tableName);
+                Collection<String> logicTableNames = null == shardingRule ? Collections.<String>emptyList() : shardingRule.getLogicTableNames(tableName);
                 result.addObject(logicTableNames.isEmpty() ? tableName : logicTableNames.iterator().next());
             } else if (indexNameColumnIndex == i) {
                 String tableName = resultSet.getString(tableNameColumnIndex);
diff --git a/sharding-jdbc/sharding-jdbc-core/src/test/java/org/apache/shardingsphere/shardingjdbc/jdbc/core/datasource/metadata/CachedDatabaseMetaDataTest.java b/sharding-jdbc/sharding-jdbc-core/src/test/java/org/apache/shardingsphere/shardingjdbc/jdbc/core/datasource/metadata/CachedDatabaseMetaDataTest.java
index 875519df792..7b3c07fc90e 100644
--- a/sharding-jdbc/sharding-jdbc-core/src/test/java/org/apache/shardingsphere/shardingjdbc/jdbc/core/datasource/metadata/CachedDatabaseMetaDataTest.java
+++ b/sharding-jdbc/sharding-jdbc-core/src/test/java/org/apache/shardingsphere/shardingjdbc/jdbc/core/datasource/metadata/CachedDatabaseMetaDataTest.java
@@ -858,8 +858,8 @@ public final class CachedDatabaseMetaDataTest {
     
     @Test
     public void assertGetTables() throws SQLException {
-        when(databaseMetaData.getTables("test", null, null, null)).thenReturn(resultSet);
-        assertThat(cachedDatabaseMetaData.getTables("test", null, null, null), instanceOf(DatabaseMetaDataResultSet.class));
+        when(databaseMetaData.getTables("test", null, "%" + TABLE_NAME + "%", null)).thenReturn(resultSet);
+        assertThat(cachedDatabaseMetaData.getTables("test", null, TABLE_NAME, null), instanceOf(DatabaseMetaDataResultSet.class));
     }
     
     @Test