CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Apr 2006
    Posts
    587

    problem with null value reading an ArrayList

    Code:
    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"

  2. #2
    Join Date
    Apr 2007
    Posts
    425

    Re: problem with null value reading an ArrayList

    Quote Originally Posted by pouncer View Post
    Code:
    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

    Code:
    this.legderid = ledgerid 
    ...
    ...
    //etc etc etc
    you are replacing all the constructor parameters with what is in the object (null).
    ------
    If you are satisfied with the responses, add to the user's rep!

  3. #3
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: problem with null value reading an ArrayList

    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.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  4. #4
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: problem with null value reading an ArrayList

    Quote Originally Posted by keang View Post
    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:
    Code:
      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.
    More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf

    Premature optimization is the root of all evil --Donald E. Knuth


    Please read Information on posting before posting, especially the info on using [code] tags.

  5. #5
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: problem with null value reading an ArrayList

    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
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  6. #6
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: problem with null value reading an ArrayList

    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.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  7. #7
    Join Date
    May 2009
    Posts
    2,413

    Re: problem with null value reading an ArrayList

    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.

    Code:
    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.
    Last edited by nuzzle; February 5th, 2010 at 03:45 AM.

  8. #8
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: problem with null value reading an ArrayList

    I only wish there were more checks available and that they could handle more complex best practices.
    try Find Bugs. You can also integrate it in to Eclipse
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  9. #9
    Join Date
    May 2009
    Posts
    2,413

    Re: problem with null value reading an ArrayList

    Quote Originally Posted by keang View Post
    try Find Bugs. 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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured