Click to See Complete Forum and Search --> : problem with null value reading an ArrayList


pouncer
February 3rd, 2010, 09:11 AM
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.commons.lang.time.DateUtils;

public class TestClass
{
public static ArrayList<Ledger> ledgerArray = new ArrayList<Ledger>();
public static Map<Ledger, String[]> expectedData = new HashMap<Ledger, String[]>();

private static class Ledger
{
public String ledgerId, narrativeOwner, reference, fund, orgDate, dueDate, action, value, narrative, narrativesLeft, currency, refNumber, entryType,
postDate, recordType;

public Ledger(String ledgerId, String narrativeOwner, String reference, String fund, String orgDate,
String dueDate, String action, String value, String narrative,
String narrativesLeft, String currency, String refNumber, String entryType,
String postDate, String recordType)
{
ledgerId = this.ledgerId;
narrativeOwner = this.narrativeOwner;
reference = this.reference;
fund = this.fund;
orgDate = this.orgDate;
dueDate = this.dueDate;
action = this.action;
value = this.value;
narrative = this.narrative;
narrativesLeft = this.narrativesLeft;
currency = this.currency;
refNumber = this.refNumber;
entryType = this.entryType;
postDate = this.postDate;
recordType = this.postDate;

ledgerArray.add(this);
}
}

public static void main(String[] args)
{
Calendar cal1 = Calendar.getInstance();
cal1.set(2010, 01, 29);
Date dt = cal1.getTime();
String strDate = DateFormatUtils.ISO_DATETIME_FORMAT.format(DateUtils.truncate(dt, Calendar.DAY_OF_MONTH));

expectedData.put(new Ledger("51782891", "51782891", "E17599", " ", strDate, strDate, " ", "10.00",
"Some narrative test", "0", "GBP", "B001712512", "D", strDate, "110"),

new String[] { "51782891", "17599", null, "17599~TAX", strDate, "N", "10",
"Some narrative test", "GBP", "INC", "B001712512", null, "D", null, null,
strDate, "Y", strDate, "-10", null});

for (int i = 0; i < ledgerArray.size(); i++)
{
Ledger led = (Ledger) ledgerArray.get(i);

System.out.println("ledgerid........... " + led.ledgerId);
}
}
}


ledgerid........... null

anyone see what im doing wrong? should be printing "51782891"

Deliverance
February 3rd, 2010, 01:33 PM
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.commons.lang.time.DateUtils;

public class TestClass
{
public static ArrayList<Ledger> ledgerArray = new ArrayList<Ledger>();
public static Map<Ledger, String[]> expectedData = new HashMap<Ledger, String[]>();

private static class Ledger
{
public String ledgerId, narrativeOwner, reference, fund, orgDate, dueDate, action, value, narrative, narrativesLeft, currency, refNumber, entryType,
postDate, recordType;

public Ledger(String ledgerId, String narrativeOwner, String reference, String fund, String orgDate,
String dueDate, String action, String value, String narrative,
String narrativesLeft, String currency, String refNumber, String entryType,
String postDate, String recordType)
{
ledgerId = this.ledgerId;
narrativeOwner = this.narrativeOwner;
reference = this.reference;
fund = this.fund;
orgDate = this.orgDate;
dueDate = this.dueDate;
action = this.action;
value = this.value;
narrative = this.narrative;
narrativesLeft = this.narrativesLeft;
currency = this.currency;
refNumber = this.refNumber;
entryType = this.entryType;
postDate = this.postDate;
recordType = this.postDate;

ledgerArray.add(this);
}
}

public static void main(String[] args)
{
Calendar cal1 = Calendar.getInstance();
cal1.set(2010, 01, 29);
Date dt = cal1.getTime();
String strDate = DateFormatUtils.ISO_DATETIME_FORMAT.format(DateUtils.truncate(dt, Calendar.DAY_OF_MONTH));

expectedData.put(new Ledger("51782891", "51782891", "E17599", " ", strDate, strDate, " ", "10.00",
"Some narrative test", "0", "GBP", "B001712512", "D", strDate, "110"),

new String[] { "51782891", "17599", null, "17599~TAX", strDate, "N", "10",
"Some narrative test", "GBP", "INC", "B001712512", null, "D", null, null,
strDate, "Y", strDate, "-10", null});

for (int i = 0; i < ledgerArray.size(); i++)
{
Ledger led = (Ledger) ledgerArray.get(i);

System.out.println("ledgerid........... " + led.ledgerId);
}
}
}


ledgerid........... null

anyone see what im doing wrong? should be printing "51782891"

Actually they're all null. you reversed your setters in the constructor

this refers to the instance of the object. you must do


this.legderid = ledgerid
...
...
//etc etc etc


you are replacing all the constructor parameters with what is in the object (null).

keang
February 3rd, 2010, 05:10 PM
This shows why the names of constructor/method parameters shouldn't be the same as the names of instance or class attributes. I've seen so many mistakes when people use this naming convention.

treuss
February 4th, 2010, 06:31 AM
This shows why the names of constructor/method parameters shouldn't be the same as the names of instance or class attributes. I've seen so many mistakes when people use this naming convention.Really? I can't recall having seen that mistake before (maybe because most programmers, whose code I review would never handwrite a constructor or getter/setter but have them generated by the IDE).

But let me ask you: Why do you think a different convention would help. The following is imho a mistake as easily or even easier made: private static class Ledger
{
public String ledgerId, narrativeOwner;

public Ledger(String pLedgerId, String pNarrativeOwner)
{
pLedgerId = ledgerId;
pNarrativeOwner = narrativeOwner;
}
}but it for sure is more difficult to spot by a reviewer than the mistake in the OP.

dlorde
February 4th, 2010, 07:15 AM
It's a matter of preference (or coding standards). Personally, I prefer to use the same names with the 'this' qualifier.

It is a bad plan that admits of no modification...
P. Syrus

keang
February 4th, 2010, 10:52 AM
Really? I can't recall having seen that mistake before Sorry my post wasn't very clear. I don't like this naming convention for novice programmers not just because of this mistake (which incidently I have seen a few times with novice programmers) but also because of mistakes where they change the value of the parameter and then use the instance variable by mistake and/or visa versa. Now I know this is bad practice but people do it and really struggle to find out what has gone wrong. With a missing (or extra) 'this.' it is harder for a novice to understand the consequences of the code than if different names are used.

But let me ask you: Why do you think a different convention would help. The following is imho a mistake as easily or even easier made:I would imagine the OP is not confused about which way around to write the assignment but rather, is confused with what 'this.' does.

nuzzle
February 4th, 2010, 07:16 PM
I don't know about Netbeans but Eclipse allows you to enable 50 or so meta-compiler warnings/errors for constructs that are not violations of the Java language itself but still potentially dangerous.

It's consider bad practice to change parameters in methods and constructors so in principle all parameters should be declared final. But this is tedious and looks ugly so Eclipse allows you to consider parameters to be implicitly final using a meta-compiler setting.


class MyClass {
private int var;
MyClass(int var) { // The var parameter is considered final by Eclipse because I said so in a setting
var = this.var; // Gives warning/error because the var parameter is treated as final and cannot be changed
}
}


So a best practice rule, namely that all parameters should be final, would've saved the OP from this bug (and potentially many others). To enforce this rule with little effort Eclipse offers a meta-compiler check. When I start an Eclipse project I enable most meta-compiler checks. It's a great help in enforcing good style. I only wish there were more checks available and that they could handle more complex best practices.

keang
February 5th, 2010, 06:39 AM
I only wish there were more checks available and that they could handle more complex best practices.try Find Bugs (http://findbugs.sourceforge.net/). You can also integrate it in to Eclipse

nuzzle
February 5th, 2010, 07:52 AM
try Find Bugs (http://findbugs.sourceforge.net/). You can also integrate it in to Eclipse

Yes I see now that there are several static code analyzers available for Java and Eclipse, even free ones. I'll have a closer look. Thank you.