Java 正在将数组初始化为null,但null比较失败

Java 正在将数组初始化为null,但null比较失败,java,arrays,Java,Arrays,在下面的代码中,我的期望是如果listreaderList.size=0,headerLine在方法commonValidation()中应该是null。但事实并非如此,有人能告诉我原因吗 String[] headerLine = null; if (readerList != null && readerList.size() != 0){ headerLine = readerList.get(0); } commonValidation(he

在下面的代码中,我的期望是如果list
readerList.size=0
headerLine
在方法
commonValidation()
中应该是
null
。但事实并非如此,有人能告诉我原因吗

String[] headerLine = null;

if (readerList != null && readerList.size() != 0){
    headerLine = readerList.get(0);         
}

commonValidation(headerLine, logDTO, hrGroupName, feedFileName);
//方法定义

public void commonValidation(String[] headerLine, TransactionLogDTO logDTO, String hrGroupName, String filename) throws PersistenceException, ServiceException, Exception {
    if ((headerLine == null) || (headerLine.length == 0)){
        logDTO.setGyr("R");

            String subject = MessageWrapper.getMessage("hr.mail.emptyfile.subject");
        String body = MessageWrapper.getMessage("hr.mail.emptyfile.body", filename);

            if (logDTO.getMessage() == null){
                logDTO.setMessage(body);
            }
            else{
                logDTO.setMessage(logDTO.getMessage() + ";" + body);
            }

        logDTO.setIsLogErrorMailSent(Boolean.TRUE);

        sendTransactionLogErrorReport(hrGroupName, subject, body);                  

        throw new Exception(body);
    }
}

String[]headerLine=null这是一个成员变量吗?如果是,则将其设为局部变量

您可以在ArrayList中添加空元素,并增加大小

    ArrayList<String[]> arrayList = new ArrayList<String[]>();
    arrayList.add(null);
    System.out.println(arrayList.size());
如果您看到没有检查
null
,那么您必须自己检查:)

但事实并非如此,(……)


我不相信。如果
readerList.size()==0
headerLine未更改值且保持
null

是否检查
readerList.size()
?不是0并且
readerList
不是
null
?否则我看不出有什么问题。这个问题有一个逻辑错误。从接受的答案来看,OP显然对“IfA then B”冲突不感兴趣,而是对“IfB then A”冲突感兴趣。对不起,伙计们,实际上在调用方法“commonValidation”之前有一个空指针,这导致了上面的场景。看到下面的答案,我实际上改变了我的实现,使用arraylist中的现有值,而不是使用单独的数组从列表中捕获数据,然后使用它进行验证。因此,我接受了下面的答案。如果我接受答案时出错,请道歉。当然,除非另有线程更改OP忽略的值或其他代码。
 410       public boolean add(E e) {
 411           ensureCapacityInternal(size + 1);  // Increments modCount!!
 412           elementData[size++] = e;
 413           return true;
 414       }