public static int randomInt(int lo, int hi) { return (Math.abs(random.nextInt()) % (hi - lo + 1)) + lo; }
The Random.nextInt () method returns any number that is valid in int. The Math.abs method gets the modulus of a number. The problem is that Math.abs does not work for Integer.MIN_VALUE, because, as you know, the opposite of this number does not fit into the int. However, Random.nextInt () may well produce this number (about once per 4 billion), then the whole method will work incorrectly. private void skipUntilEmptyOrCommentLine(String line) throws IOException { // skip everything until empty line, or comment line while (line != null && line.length() > 0 && line.charAt(0) != '#') { line = input.readLine().trim(); } }
The input stream is not checked for readiness by the ready () method, and the result of readLine () is not checked for null. As a result, a malformed input file will cause a NullPointerException. if (atom.getFormalCharge() != null & atom.getFormalCharge().intValue() == 1) {...}
return (getAtomTypeName() == type.getAtomTypeName()) && (maxBondOrder == type.maxBondOrder) && (bondOrderSum == type.bondOrderSum);
getAtomTypeName () returns a String, and bondOrderSum is of type Double. The application logic completely assumes that there will be different, but equal equals objects and the comparison will work incorrectly. while (line != null) { line.trim(); if (line.length() > 0) blacklist.add(line); line = reader.readLine(); }
Calling line.trim () is useless, as it does not change the line itself, and no one uses the result. The author obviously meant line = line.trim (). Similarly, there are calls to String.substring without saving the result.if( atom.equals("H") )
, where the atom is an IAtom type. It is implied, apparently, if( atom.getSymbol().equals("H") )
. In general, this is a mysterious place, since there are more than a dozen such errors, and, in my opinion, they should greatly influence the semantics and distort the result. public void setReactionMetadata(String metadata) { this.reactionInfo.add( metadata ); }
FindBugs determines that the private field reactionInfo is not initialized in any other method, so this method will always throw a NullPointerException. public class AtomValenceTool { private static Map<String,Integer> valencesTable = null; public static int getValence(IAtom atom) { if (valencesTable == null) { valencesTable = new HashMap<String, Integer>(); valencesTable.put("H", 1); valencesTable.put("He", 8); valencesTable.put("Ne", 8); ... } return valencesTable.get(atom.getSymbol()); } }
When called from different streams, race condition is possible, when valencesTable is no longer null, but not yet full. Then one thread will throw a NullPointerException for a completely correct atom. String path = file.getPath().replaceAll(File.separator, ".");
The replaceAll method takes a regular expression as an argument. Under Windows, File.separator is a backslash that is specially interpreted in regular expressions, so this code will fall from a PatternSyntaxException. ILoggingTool logger = LoggingToolFactory.createLoggingTool(DebugAtomContainer.class);
public void addStereoElement(IStereoElement parity) { logger.debug("Adding stereo element: ", parity); super.addStereoElement(parity); }
The problem is that this method is called in one of the constructors of the base class, when assigning the value to the variable logger has not yet worked. As a result, a NullPointerException will naturally occur.Source: https://habr.com/ru/post/198938/
All Articles