为什么不是';难道我的java代码没有通过if语句吗?
好的,我以前从来没有遇到过这个问题,所以我不确定如何使用它或修复它,我正在构建一个创建经销商的java应用程序,在该应用程序中,我将参数传递到DealerFactory.createDealer方法,然后首先检查该经销商是否存在,并使用如下条件语句:为什么不是';难道我的java代码没有通过if语句吗?,java,performance,debugging,methods,conditional-statements,Java,Performance,Debugging,Methods,Conditional Statements,好的,我以前从来没有遇到过这个问题,所以我不确定如何使用它或修复它,我正在构建一个创建经销商的java应用程序,在该应用程序中,我将参数传递到DealerFactory.createDealer方法,然后首先检查该经销商是否存在,并使用如下条件语句: if (DealerFactory.fetchDealer(loginId).getLoginId().equals(loginId)) { throw new Exception("Sorry That Dealer A
if (DealerFactory.fetchDealer(loginId).getLoginId().equals(loginId)) {
throw new Exception("Sorry That Dealer Already Exists");
} else if (DealerFactory.fetchDealer(loginId).getId().equals(DNo)){
throw new Exception("Sorry That Dealer Already Exists");
} else {
<< proceed with the rest of the method here >>
if(DealerFactory.fetchDealer(loginId) != null &&
DealerFactory.fetchDealer(loginId).getLoginId().equals(loginId))
boolean dealerExists(String id) {
return (YOUR_DATA_STRUCTURE_OF_DEALERS.get(id) != null);
}
我已经包含了println语句来跟踪程序的运行,当条件的计算结果为false时,我从不将其放入else语句中。我似乎无法理解为什么当它被评估为错误时,它会违反条件声明,有什么想法吗
编辑:::
好的,这样我就可以在帮助你们的过程中提供更多的帮助。哈哈,这里是完整的方法,我很抱歉没有首先发布它
public static int create(String DNo, String name, String admin,
String loginId, String password, String callSrc, String voiSys,
String whoCall, String callBrt, String active, String adfEmail)
throws SQLException, Exception {
int validateResult = 0;
if (DealerFactory.fetchDealer(loginId).getLoginId().equals(loginId)
|| DealerFactory.fetchDealer(loginId).getId().equals(DNo)) {
throw new Exception("Sorry That Dealer Already Exists");
}
try {
DealerFactory.pool = DBConnector.getInstance();
DealerFactory.connect = DBConnector.getConnection();
DealerFactory.preparedStatement = connect
.prepareStatement("Insert Into Dealers (DNo, Dealer, Admin, Login, Password, CallSrc, VoiSys, WhoC, CBrt, Active, ADFemail) "
+ "values(?,?,?,?,?,?,?,?,?,?,?)");
DealerFactory.preparedStatement.setString(1, DNo);
DealerFactory.preparedStatement.setString(2, name);
DealerFactory.preparedStatement.setString(3, admin);
DealerFactory.preparedStatement.setString(4, loginId);
DealerFactory.preparedStatement.setString(5, password);
DealerFactory.preparedStatement.setString(6, callSrc);
DealerFactory.preparedStatement.setString(7, voiSys);
DealerFactory.preparedStatement.setString(8, whoCall);
DealerFactory.preparedStatement.setString(9, callBrt);
DealerFactory.preparedStatement.setString(10, active);
DealerFactory.preparedStatement.setString(11, adfEmail);
validateResult = DealerFactory.preparedStatement
.executeUpdate();
} catch (SQLException ex) {
System.err.println("Error: " + ex + "\n");
ex.printStackTrace();
} finally {
DBUtils.closePrepStatement(DealerFactory.preparedStatement);
DealerFactory.pool.freeConnection(DealerFactory.connect);
}
return validateResult;
}
首先,由于存在
NullPointerException
所以这一部分:
if(DealerFactory.fetchDealer(loginId).getLoginId().equals(loginId))
可能看起来像这样:
if (DealerFactory.fetchDealer(loginId).getLoginId().equals(loginId)) {
throw new Exception("Sorry That Dealer Already Exists");
} else if (DealerFactory.fetchDealer(loginId).getId().equals(DNo)){
throw new Exception("Sorry That Dealer Already Exists");
} else {
<< proceed with the rest of the method here >>
if(DealerFactory.fetchDealer(loginId) != null &&
DealerFactory.fetchDealer(loginId).getLoginId().equals(loginId))
boolean dealerExists(String id) {
return (YOUR_DATA_STRUCTURE_OF_DEALERS.get(id) != null);
}
或者,您可以在所有if
语句之前进行单独的null
检查
然而,你所做的是过分的。这一部分:
DealerFactory.fetchDealer(loginId).getLoginId()
如果找不到已有的经销商或登录名,则返回null
。
假设您的fetchDealer()
方法在找不到经销商时返回null
而不是:
if(DealerFactory.fetchDealer(loginId).getLoginId().equals(loginId)))
您只需执行以下操作:
if(DealerFactory.fetchDealer(loginId) != null)
您可以做的另一个改进是向DealerFactory
添加一个名为dealerExists(字符串id)
的方法,声明如下:
if (DealerFactory.fetchDealer(loginId).getLoginId().equals(loginId)) {
throw new Exception("Sorry That Dealer Already Exists");
} else if (DealerFactory.fetchDealer(loginId).getId().equals(DNo)){
throw new Exception("Sorry That Dealer Already Exists");
} else {
<< proceed with the rest of the method here >>
if(DealerFactory.fetchDealer(loginId) != null &&
DealerFactory.fetchDealer(loginId).getLoginId().equals(loginId))
boolean dealerExists(String id) {
return (YOUR_DATA_STRUCTURE_OF_DEALERS.get(id) != null);
}
甚至:
boolean dealerExists(String id) {
return (fetchDealer(loginId) != null);
}
这将允许更好的代码逻辑流。您的如果声明将非常清楚,那么:
if(DealerFactory.dealerExists(loginId) {
throw new Exception("Sorry That Dealer Already Exists");
}
此外,值DNo
是我假定的经销商编号,您正在检查是否存在经销商,该经销商是否具有提供的loginId
或提供的DNo
。但是,在您的检查中,您正在将DNo
与loginId
进行比较,以检查是否存在经销商。DNo
的确切意义是什么,并且loginId
是否足以确定经销商是否存在?
如果您真的需要检查DNo
,只需将其作为检查添加到我上面建议的方法中即可。更多信息重新对传递到条件中的变量进行分级将非常有帮助(即loginId
和DNo
)。我要做的第一件事是简化if
和else if
语句中的条件
似乎您可以将if
条件更改为if(DealerFactory.fetchDealer(loginId))
,并使其正常工作,因为如果工厂返回具有您正在查找的ID的经销商,则该经销商已经存在。从我对你想要达到的目标的理解来看,没有必要再深入挖掘了。还可以尝试使用调试器和监视变量一步一步地运行这段代码,以查看每行代码的情况
编辑:
您还可以将表达式移到条件之外,并将其设置为变量。例如:
int dealerId = DealerFactory.fetchDealer(loginId);
if(dealerId != null && dealerId != loginId) {
//<< proceed with the rest of the method here >>
} else {
//<<throw exception>>
}
int-dealerId=DealerFactory.fetchDealer(loginId);
if(dealerId!=null&&dealerId!=loginId){
//>
}否则{
//
}
检查条件中的dealerId
是否为null将避免NullPointerException
s,并且声明条件之外的内容将使手动调试更容易。不会DealerFactory.fetchDealer(loginId)
返回null
,如果该交易商不存在??如果不是,它会返回什么?@JonathanDrapeau是的,它会返回null,但是如果它为null,它不会因为不等于当前输入而推到下一个语句吗?如果我错了,我会处理空值,但我以前从未处理过它,所以我不知道类型是DNo
,它包含什么?不,它不会,它会抛出NullPointerException
,就像尼姆在回答中说的那样。@RichardDavy不,如果你调用.getLoginId()
onnull
。我刚刚从原始版本编辑了这篇文章,很抱歉没有这么做,我提供的信息有点缺乏我不知道为什么我没有这样想,为什么我直接寻找他们的id和登录对我来说很有意义,但这更有意义,并且减少了很多不必要的代码,非常感谢我的朋友。