有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

有没有一种方法可以在netbeans IDE中编写java中的空检查代码?

在Netbeans中,您可以编写“psvm”,然后按tab键,它将生成

 public static void main(String[] args) {
     //your cursor is placed here.
 }

类似地,也有写周期的方法,比如catch块、instanceof检查等等。有没有类似于这种方法的东西,来生成变量的空检查

我想要这样的东西:

ResultSet rs;
rs //pressing some magic button like ctrl+space or "rs null<TAB>" 
   //and a code like this would be generated:

if (rs != null) //your cursor will be placed here.

if (rs != null)
{
    //your cursor here
}

共 (2) 个答案

  1. # 1 楼答案

    您可以为此创建自己的模板:

    转到工具->;选项->;编辑->;代码模板->;新->;缩写:ifnn

    扩展文本:

    if (${EXP instanceof=”Object”} != null) {
        ${selection}${cursor}
    }
    

    然后在编辑器中按“ifnn”+选项卡

  2. # 2 楼答案

    所以我决定创建这个模板:(已接受答案的修改版本)

    if(${EXP instanceof="Object"} != null) {
        ${EXP}.${cursor}
    } 
    

    仅分配给“nn”代表“不为空”

    这也有助于在方法开始时验证属性/参数

    Validate.notNull(${var}, "${var} can't be null");
    

    我通过写“valp”并按下tab(validate parameter的缩写)来扩展它。 它比:

    if(someVar == null){
        throw new Exception("someVar can't be null");
    }
    

    当有许多经过验证的参数时,这很有用