1. 程式人生 > >String.length()>1 報空指標異常

String.length()>1 報空指標異常

@Override
	public ExpertEntity getExpertById(Long id) {
		ExpertEntity Expert = ExpertMapper.getObjectById(id);
		if (Expert.getFilepath().length()>1) {
			...
		}
		return Expert;
	}

物件的Stirng的length大於1就進行操作,很理想對不對,然而報空指標錯誤

解決方案:

@Override
	public ExpertEntity getExpertById(Long id) {
		ExpertEntity Expert = ExpertMapper.getObjectById(id);
		if (Expert.getFilepath()!=null && Expert.getFilepath().length()>1) {
			...
		}
		return Expert;
	}