Results 1 to 5 of 5
  1. #1
    Nilla is offline Member
    Join Date
    Jun 2012
    Posts
    13
    Rep Power
    0

    Default get accountNumber problem

    I have five classes: Account (abstract), SavingAccount, CreditAccount, GUI and Logic. My problem is the showSelecteAccountNumber method, and the problem is in this line: if(selectedAccountNr==konto.getAccountNumber()){

    selectedAccountNr is no problem. It comes from the accountNrField. It is konto.getAccountNumber() which is the problem. Compilation is no problem but it gives the following error message

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at GUI.showSelectedAccount(GUI.java:556)
    at GUI.access$16(GUI.java:546)
    at GUI$ShowSelectedAccountListener.actionPerformed(GU I.java:173)

    I think I donīt communicate correct between the classes, but I really donīt know how to get the accountNumber from the account list. Can someone help me to get it work??

    Logic:

    Java Code:
    public class Logic{
    	private ArrayList<Customer> customers = new ArrayList<Customer>();
            private ArrayList<Account> accounts = new ArrayList<Account>();
    			
    	public Logic()
    	{
    		customers = new ArrayList<Customer>();
    		accounts = new ArrayList<Account>();
    	}
    	
    	public void addCustomer(String fName, String eName, String pNumber){
    		customers.add(new Customer(fName, eName, pNumber));
    	}
    	
    	public void addSavingAccount(String pNumber) {
    		accounts.add(new SavingAccounts(pNumber));
    	}
    		
    	public void addCreditAccount(String pNumber) {
    		accounts.add(new CreditAccounts(pNumber));
    	}
    	
    	// Accessors
    	
            public ArrayList<Customer> getAllCustomers(){
    		return customers;
    	}
    	
    	public ArrayList<Account> getAllAccounts(int acountNumber, double StartingBalance, String kontoTyp, String PNumber){
    		return accounts;
    	}
    	
    	public String getFirstNameForCustomerAt(int position){
    		return (customers.get(position)).getFirstName();
    	}
    	
    	public String getSecondNameForCustomerAt(int position){
    		return (customers.get(position)).getSecondName();
    	}
    	
    	public String getPNumberForCustomerAt(int position){
    		return (customers.get(position)).getPNumber();
    	}
    
            public int getAccountNumberForCustomerAt(int position){
    		return (accounts.get(position)).getAccountNumber();
    	}
    		
    	public ArrayList<Account> getAccounts() {
    		return accounts;
    	}
    
           .......
    }
    GUI:

    Java Code:
    public class GUI extends JFrame {
            private Customer kund;	
            private Logic logic;
    	private Account konto;
    	....	
    	private JTextField firstNameField;
    	private JTextField secondNameField;
    	private JTextField pNumberField;
    	private JTextField accountNrField;
    	....	
    	
            private ArrayList<Customer> customers;
    
            //JLists
            kunder=new JList(new DefaultListModel());
            konton = new JList<Object>();
    	
            // Listerners
            // Menu
             ....
    		
    	private void addSAccount(int accountNumber, double balance, String kontoTyp, String valdKundPNr){
    		logic.addSavingAccount(selectedKundPNr);
    		konton.setListData(logic.getAllAccounts(accountNumber, startingBalance, kontoTyp, valdKundPNr).toArray());
    		clear();
    	}
    	
    	private void addCAccount(int accountNumber, double balance, String kontoTyp, String valdKundNr){
    		logic.addCreditAccount(selectedKundPNr);
    		konton.setListData(logic.getAllAccounts(accountNumber, startingBalance, kontoTyp, valdKundPNr).toArray());
    		clear();
    	}
    	
    	private int showSelectedAccount(String selectedCustomerNr){
    		int position = kunder.getSelectedIndex();
    		displayJTextArea.setText("");
    		if(position > -1){
    			firstNameField.setText(logic.getFirstNameForCustomerAt(position));
    			secondNameField.setText(logic.getSecondNameForCustomerAt(position));
    			selectedAccountNr= Integer.parseInt(accountNrField.getText());
    			
    				for(int j=0; j< konton.getModel().getSize(); j++){
    						if(selectedAccountNr==konto.getAccountNumber()){
    						Object item = konton.getModel().getElementAt(j);            
    						displayJTextArea.append("\nKonto med " + item + " \n");
    						}
    				}
    			}
    		else {
    			JOptionPane.showMessageDialog(null, "You have to select an account in the list!");
    		}
    		clear();
    		return selectedAccountNr;
    	}

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: get accountNumber problem

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at GUI.showSelectedAccount(GUI.java:556)
    Have you found the variable with the null value on line 556? You need to find which variable has the null value and then backtrack in the code to find out why that variable does not have a valid non-null value.

    problem is in this line: if(selectedAccountNr==konto.getAccountNumber()){
    Does the variable: konto have a valid value when line 556 is executed?
    Last edited by Norm; 09-21-2012 at 11:22 PM.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Nilla is offline Member
    Join Date
    Jun 2012
    Posts
    13
    Rep Power
    0

    Default Re: get accountNumber problem

    I think the problem is private Account konto; in the Account class.
    I donīt know how to get the right accountNumber from the arrayList accounts.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: get accountNumber problem

    Does the variable: konto have a value when line 556 is executed?

    Where is it assigned a value?
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    ostkanten is offline Member
    Join Date
    Jul 2012
    Posts
    37
    Rep Power
    0

    Default Re: get accountNumber problem

    I think I have just solved the problem by changing
    private ArrayList<Account> accounts;
    to
    List<Account> accounts = new ArrayList();
    in the GUI class

    Thanks for your interest in my problem.

Similar Threads

  1. Problem to get accountNumber
    By ostkanten in forum New To Java
    Replies: 3
    Last Post: 09-19-2012, 01:44 PM
  2. Problems getting an unique accountNumber
    By ostkanten in forum New To Java
    Replies: 11
    Last Post: 08-14-2012, 12:08 PM
  3. Small problem with problem with Java, C++ parse program.
    By dragstang86 in forum New To Java
    Replies: 4
    Last Post: 10-30-2011, 03:43 AM
  4. Replies: 9
    Last Post: 09-21-2010, 04:15 PM

Posting Permissions

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