分析得出了三个BUG.
BUG 1: Main.java中,BUG原因是外部的程序可以修改对象中的数据,所以不安全,以后注意.
public static Hashtable<String,User> getUsers(){
return users;
}
Public static method may expose internal representation by returning array
A public static method returns a reference to an array that is part of the static state of the class. Any code that calls this method can freely modify the underlying array. One fix is to return a copy of the array.
其实这个方法,在后的程序中有两个地方用了:
out.writeUTF(KEYS.USERS+Main.getUsers().size());
Enumeration<User> ele=Main.getUsers().elements();
修改方法:
将Main.java中的原来方法删除,并添加两个新的方法,OK了!
public static int getUserSize(){
return users.size();
}
public static Enumeration<User> getUsers(){
return users.elements();
}
BUG 2:Clinet.java中,这一个是因为,如果DBS.checkUser(name,pass)抛出异常,那么,user就为null,则抛出NullPointerException
User user=null;
try {
user=DBS.checkUser(name, pass);
if(user==null){
out.writeUTF(KEYS.ERROR+"密码错误");
close();
return;
}
} catch (SQLException e) {
e.printStackTrace();
}
user.setPort(nport);//这一句有BUG.
Possible null pointer dereference in method on exception path
A reference value which is null on some exception control path is dereferenced here. This may lead to a NullPointerException
when the code is executed. Note that because FindBugs currently does not prune infeasible exception paths, this may be a false warning.
Also note that FindBugs considers the default case of a switch statement to be an exception path, since the default case is often infeasible.
修改办法在e.printStackTrace()之后,加上return;
BUG 3:MyIcon.java没读懂意思,大概是这样退出不好.
item.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);//这一句有BUG
}
});
Method invokes System.exit(...)
Invoking System.exit shuts down the entire Java virtual machine. This should only been done when it is appropriate. Such calls make it hard or impossible for your code to be invoked by other code. Consider throwing a RuntimeException instead.
还不知道具体原因,所以没法修正.
英语差就是不好.
![](http://c.services.spaces.live.com/CollectionWebService/c.gif?space=vsplay&page=RSS%3A+%E4%BB%8A%E5%A4%A9%E8%AF%95%E4%BA%86%E4%B8%8BFindBugs%E5%88%86%E6%9E%90%E4%BA%86%E4%B8%80%E4%B8%AA%E9%A1%B9%E7%9B%AE%2C%E7%BB%93%E6%9E%9C%E5%A6%82%E4%B8%8B.&referrer=)
![](https://i-blog.csdnimg.cn/blog_migrate/177fa8045eabe6f09969c4a92724dc14.gif)