转载

java.lang.NullPointerException 空指针异常

java.lang.NullPointerException 空指针异常,虽然不影响打包,不影响程序的运行,但是看着不舒服

1,问题

java.lang.NullPointerException

at org.apache.spark.sql.internal.SQLConf$$anonfun$14.apply(SQLConf.scala:133)

at org.apache.spark.sql.internal.SQLConf$$anonfun$14.apply(SQLConf.scala:133)

at scala.Option.map(Option.scala:146)

at org.apache.spark.sql.internal.SQLConf$.get(SQLConf.scala:133)

at org.apache.spark.sql.catalyst.parser.AbstractSqlParser.parse(ParseDriver.scala:87)

at org.apache.spark.sql.execution.SparkSqlParser.parse(SparkSqlParser.scala:48)

at org.apache.spark.sql.catalyst.parser.AbstractSqlParser.parsePlan(ParseDriver.scala:69)

at org.apache.spark.sql.SparkSession.sql(SparkSession.scala:642)

at netjoy.mysql.sparkModel.getSparkNum(sparkModel.scala:79)

2,例子分析

/*
 *
 * 判断是否连接
 * */
public boolean isConnected() throws Exception{
    boolean isOk=false;
    try {
        Statement statement = con.createStatement();       //注意:第一次运行时,debug查看con是null
        ResultSet rs = statement.executeQuery("select 1");
        isOk =  rs.next();
    } catch (Exception e) {
        e.printStackTrace();    //con是null后,就会抛出异常了
    }
    return isOk;
}

正确写法

/*
 *
 * 判断是否连接
 * */
public boolean isConnected() throws Exception{
    boolean isOk=false;
    try {
        if(con != null){    //判断一下是否为空
	        Statement statement = con.createStatement();
	        ResultSet rs = statement.executeQuery("select 1");
	        isOk =  rs.next();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return isOk;
}

养成良好的代码,以及自测习惯,是很重要,对自己负责,也对别人负责

养成良好的代码,以及自测习惯,是很重要,对自己负责,也对别人负责

养成良好的代码,以及自测习惯,是很重要,对自己负责,也对别人负责

原文  http://blog.51yip.com/android/2173.html
正文到此结束
Loading...