Click here to Skip to main content
15,888,984 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all!
I have this source:
Java
protected boolean createDocument(String FileName, Row fileRow, ArrayList rows) {
    String currKey = getCurrentKey(fileRow);
    TransferDocument dtr = null;

    if (documentList.containsKey(currKey)) {
        dtr = (TransferDocument) documentList.get(currKey);
        if (dtr == null) // already found into the search, already done
            return true;
    }

    if (dtr == null) {  // documentList does not contains the key
        dtr = newDocument(fileRow);     // may be null or not
        documentList.put(currKey, dtr); // record even if it is null
        if (dtr == null)
            return true;
    }

    addRows(dtr, fileRow);

    return true;
}

protected TransferDocument newDocument(Row fileRow) {
    String alreadyDone = super.searchAlreadyDone(fileRow);
    if (alreadyDone != null) {
        output.print("Already done: " + alreadyDone);
        return null;
    }

    TransferDocument dtr = (TransferDocument) Factory.createObject(TransferDocument.class);

    return dtr;
}


What I have tried:

Eclipse warn me at the return true; just after the check dtr == null of createDocument method, but newDocument can return both a null or a not null values.

Somewhere I read about the dead code wouldn't be compiled. Have I to fear about this?

Thank you.
Posted
Updated 9-Aug-17 4:57am
v2
Comments
Richard MacCutchan 9-Aug-17 6:31am    
Please show the exact error message.

Globally dead code would not mean any problem after compilation (even I can't see where is some dead code here...), so you need not fear it...
However I can see an other problem in there...

Your version:
Java
dtr = newDocument(fileRow);

documentList.put(getCurrentKey(fileRow), dtr); // what if dtr is null here?

if (dtr == null)
  return true;
My version:
Java
dtr = newDocument(fileRow);

if (dtr == null)
  return true;

documentList.put(getCurrentKey(fileRow), dtr); // put this line here to be sure
 
Share this answer
 
Comments
Menci Lucio 9-Aug-17 6:16am    
I cleared a little bit my code to see only the interesting rows for the purpouse, but the original code, instead:
TransferDocument dtr = (TransferDocument) documentList.get(getCurrentKey(fileRow));
was:
String currKey = getCurrentKey(fileRow);
TransferDocument dtr = null;
if (documentList.containsKey(currKey)) {
dtr = (TransferDocument) documentList.get(currentKey);
if (dtr == null) // already found into the search, already done
return true;
}

In this way, the Already done warning will appair once only.
Kornfeld Eliyahu Peter 9-Aug-17 6:27am    
So your original code is like this?
protected boolean createDocument(String FileName, Row fileRow, ArrayList rows) {	
	String currKey = getCurrentKey(fileRow);	
	TransferDocument dtr = null; 	

	if (documentList.containsKey(currKey)) {		
		dtr = (TransferDocument) documentList.get(currentKey);		

		if (dtr == null) // already found into the search, already done			
			return true;	
	} 	

	if (dtr == null) {		
		dtr = newDocument(fileRow);		
		documentList.put(getCurrentKey(fileRow), dtr);		

		if (dtr == null)			
			return true;	
	} 	

	addRows(dtr, fileRow); 	
	return true;
}

On which line is the dead code warning?
(For sure the 'if(dtr == null)' at line 11 is always true from the compiler point of view...
Menci Lucio 9-Aug-17 10:46am    
I updated my code.

DocumentList can contain the key and can not. If it contains the key, its value can be null and can be not null.

There are four checks. If there is a key (first check true) and its value is null (second check false) returns, if its value is not null, third check is false.

If there is not a key (first check false), the third check is true, newDocument could create or not the document, put the result into the documentList, and the fourth check could be true or false (if the document is created or not).

No checks may have only true or false result.

documentList is a Map (a TreeMap exactly). I think that the compiler doesn't know that a null value could be passed to the put method.
I resolved in this way:
Java
if (dtr == null) {  // documentList does not contains the key
    dtr = newDocument(fileRow);     // may be null or not
    documentList.put(currKey, dtr); // record even if it is null
}

if (dtr != null)
    addRows(dtr, fileRow);

return true;


but that compiler warning is so strange (I think that the compiler doesn't know that a null value could be passed to the put method).
 
Share this answer
 
Comments
Richard MacCutchan 9-Aug-17 11:07am    
You have still not shown us the exact error message. So we can only guess what might be the problem.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900