Results 1 to 10 of 10
  1. #1
    Manny123 is offline Member
    Join Date
    Oct 2012
    Posts
    10
    Rep Power
    0

    Exclamation Array list problems :s

    Java Code:
    public class Account
    
    {
        
        private ArrayList<String> accountList;
       
        public Account()
        {
            // initialise instance variables
          
            accountList = new ArrayList<String>();
            
            
        }
        
        
        public void viewAvailableAccounts(){
            
            accountList = new ArrayList<String>();
            //Account accounts = new Account();
            accountList.add("Savings Account");
            accountList.add("Current Account");
            System.out.println(accountList);
           
           
        }

    I am just trying to display the records in the Array list in the viewAvailableAccounts method and i get an error of "java.lang.NullPointerException
    at Account.accountTypes(Account.java:30)". I have no idea why i get this error.

    Any ideas please?

  2. #2
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    392
    Rep Power
    4

    Default Re: Array list problems :s

    Which of the lines is 30? From a quick glance, it looks okay to me... Try moving the System.out.println(accountList) to above the accountList.adds. Shouldn't be a problem though...

  3. #3
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    392
    Rep Power
    4

    Default Re: Array list problems :s

    Which of the lines is 30? From a quick glance, it looks okay to me... Try moving the System.out.println(accountList) to above the accountList.adds. Shouldn't be a problem though...

    Although the line "public class Accountpublic class Account" looks odd to me. How did that even compile?

  4. #4
    Manny123 is offline Member
    Join Date
    Oct 2012
    Posts
    10
    Rep Power
    0

    Default Re: Array list problems :s

    line 30 is simply me using the } symbol to close my other method and the "public class Accountpublic class Account" was jus a copy and paste mistake made by me.

  5. #5
    Manny123 is offline Member
    Join Date
    Oct 2012
    Posts
    10
    Rep Power
    0

    Default Re: Array list problems :s

    Java Code:
    import java.util.ArrayList;
    /**
     * Write a description of class Account here.
     * 
     * @author (your name) 
     * @version (a version number or a date)
     */
    public class Account
    {
        // instance variables - replace the example below with your own
        private String accountName;
        private Integer accountNumber;
        private Integer sortCode;
        private Bank bank;
        private ArrayList<String> accountList;
        
        /**
         * Constructor for objects of class Account
         */
        public Account(String accountName, Integer accountNumber, Integer sortCode, Bank bankName)
        {
            // initialise instance variables
            this.accountName = accountName;
            this.accountNumber = accountNumber;
            this.sortCode = sortCode;
            bank = bankName;
            accountList = new ArrayList<String>();
            
            
        }
        
        
        public void viewAvailableAccounts(){
            
            accountList = new ArrayList<String>();
            //Account accounts = new Account();
            accountList.add("Savings Account");
            accountList.add("Current Account");
            System.out.println(accountList);
           
           
        }
       
    
        /**
         * An example of a method - replace this comment with your own
         * 
         * @param  y   a sample parameter for a method
         * @return     the sum of x and y 
         */
        
        
        
        }

    this is the original code

  6. #6
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    392
    Rep Power
    4

    Default Re: Array list problems :s

    Well, there's definitely something missing in the example... The exception says it should be in the accountTypes method in the Account class.

  7. #7
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    392
    Rep Power
    4

    Default Re: Array list problems :s

    Could you post your entire stacktrace? It would look something like this:

    Java Code:
    Exception in thread "main" java.lang.NullPointerException
            at test.splittest(test.java:19)
            at test.<init>(test.java:14)
            at test.main(test.java:9)

  8. #8
    Manny123 is offline Member
    Join Date
    Oct 2012
    Posts
    10
    Rep Power
    0

    Default Re: Array list problems :s

    stacktrace? Sorry i'm kinda new what is a stacktrace?

  9. #9
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    392
    Rep Power
    4

    Default Re: Array list problems :s

    Basically, the whole text you get when you get an exception like that. It should look a little like what I posted just above.

  10. #10
    Manny123 is offline Member
    Join Date
    Oct 2012
    Posts
    10
    Rep Power
    0

    Default Re: Array list problems :s

    Don't worry, its working now. Cheers

Similar Threads

  1. Problems inserting 1d array to 2d array grid
    By jumpgirl in forum New To Java
    Replies: 1
    Last Post: 03-26-2012, 03:04 AM
  2. performance problems with JList/default list model
    By prescott in forum AWT / Swing
    Replies: 3
    Last Post: 11-16-2011, 04:06 AM
  3. Linked List, Array List time complexity
    By Rick99771977 in forum New To Java
    Replies: 4
    Last Post: 08-18-2011, 05:37 AM
  4. List problems
    By ZeCute in forum New To Java
    Replies: 0
    Last Post: 04-10-2011, 06:13 PM
  5. Replies: 2
    Last Post: 09-06-2010, 01:03 AM

Posting Permissions

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