有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java NullPointerException ArrayList

因此,由于nullpointer异常,我在向数组列表添加项时遇到问题。 我试图向驻留在主类中的ArrayList添加一个新帐户。因此,我将ArrayList从主类传递到AddAcct类,以便设置新帐户并将其添加到列表中。我只是对变量在初始化时如何为null感到困惑

下面是调用此函数的方法。这就是节目开始的地方。我需要将此ArrayList传递给AddAcct类,以便在添加新帐户时,可以将该帐户添加到帐户的主列表中

//class variables
ArrayList<Account> accounts;

public static void main(String[] args) {
    new Window();
    ArrayList<Account> accounts = new ArrayList<Account>();




}

// inside a method that gets invoked on button click
if (e.getSource() == btnAddAcct){
        newAcct = new AddAcct(accounts);

    } 

这就是我在AddAcct类中设置ArrayList变量以接收传递的主ArrayList的地方

//class variables
ArrayList<Account> accounts;


//sets up initial window
public AddAcct(ArrayList<Account> accounts){
    super("Add Account");
    setupGUI();
    registerListeners();
    this.accounts = new ArrayList<Account>();
    this.accounts = accounts;

    //updateScreen();
} // end constructor

下面是发生异常的后一种方法

if(success){
            Account account = new Account(fName,lName);
            this.accounts.add(account);//exception is happening on this line
            String accountNum = Integer.toString(account.getAcctNum());
            lblInfo.setText(accountNum);
        }

对于那些想要完整代码的人。以下是该项目的起点

package LibraryAccount;




import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import LibraryAccount.AddAcct;
import LibraryAccount.CheckInOut;


public class Window extends JFrame implements ActionListener {

    private static final long serialVersionUID = 1L;

    JButton btnAddAcct;
    JButton btnBook;
    JButton btnReport;
    AddAcct newAcct;
    CheckInOut checkInOut;
    ArrayList<Account> accounts;


    public static void main(String[] args) {
        new Window();
        ArrayList<Account> accounts = new ArrayList<Account>();




    }
    //sets up initial window
    public Window(){
        super("Library Manager");
        setupGUI();
        registerListeners();

        //updateScreen();
    } // end constructor

    public void setupGUI(){

        JPanel pnlControls = new JPanel();
        btnAddAcct = new JButton("Add Account");
        btnBook = new JButton("Check out/in book");
        btnReport = new JButton("Reports");


        //add components to Panels



        pnlControls.setLayout(new FlowLayout());
        pnlControls.add(btnAddAcct);
        pnlControls.add(btnBook);
        pnlControls.add(btnReport);


        //set up layout
        Container mainPanel = this.getContentPane();
        mainPanel.setLayout(new GridLayout(1,0));

        mainPanel.add(pnlControls);


        this.setSize(500, 300);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    } // end setup

    public void registerListeners(){


        btnAddAcct.addActionListener(this);
        btnBook.addActionListener(this);
        btnReport.addActionListener(this);


    } // end registerListeners

public void actionPerformed(ActionEvent e){

        //check all button presses for action


      //back button
        if (e.getSource() == btnAddAcct){
            newAcct = new AddAcct(accounts);

        } 
        //checkout/in book
        else if(e.getSource() ==btnBook){
            checkInOut = new CheckInOut();
        }

        //report pressed
        else if(e.getSource() == btnReport){

        }


        else {


        } // end if


    } // end actionPerformed

}

这是AddAcct类

package LibraryAccount;

import java.awt.Container;
import java.awt.GridLayout;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;

public class AddAcct extends JFrame implements ActionListener {

    private static final long serialVersionUID = 1L;

    JButton btnAddAcct;
    JLabel lblAddress;
    JLabel lblCity;
    JLabel lblZip;
    JLabel lblState;
    JLabel lblFName;
    JLabel lblLName;
    JLabel lblInfo;
    JTextArea txtFName;
    JTextArea txtLName;
    JTextArea txtAddress;
    JTextArea txtCity;
    JTextArea txtZip;
    JTextArea txtState;
    ArrayList<Account> accounts;







    //sets up initial window
    public AddAcct(ArrayList<Account> accounts){
        super("Add Account");
        setupGUI();
        registerListeners();
        this.accounts = new ArrayList<Account>();
        this.accounts = accounts;

        //updateScreen();
    } // end constructor

    public void setupGUI(){

        JPanel pnlControls = new JPanel();
        btnAddAcct = new JButton("Add Account");
        lblAddress = new JLabel("Address");
        lblCity = new JLabel("City");
        lblZip = new JLabel("Zip");
        lblState = new JLabel("State");
        lblFName = new JLabel("First Name");
        lblLName = new JLabel("Last Name");
        lblInfo = new JLabel("");
        txtFName = new JTextArea(30,1);
        txtLName = new JTextArea(30,1);
        txtAddress = new JTextArea(30,1);
        txtCity = new JTextArea(30,1);
        txtZip = new JTextArea(30,1);
        txtState = new JTextArea(30,1);







        //add components to Panels

        pnlControls.setLayout(new GridLayout(0,1));
        pnlControls.add(lblInfo);
        pnlControls.add(lblFName);
        pnlControls.add(txtFName);
        pnlControls.add(lblLName);
        pnlControls.add(txtLName);
        pnlControls.add(lblAddress);
        pnlControls.add(txtAddress);
        pnlControls.add(lblCity);
        pnlControls.add(txtCity);
        pnlControls.add(lblZip);
        pnlControls.add(txtZip);
        pnlControls.add(lblState);
        pnlControls.add(txtState);
        pnlControls.add(btnAddAcct);

        //set up layout
        Container mainPanel = this.getContentPane();
        mainPanel.setLayout(new GridLayout(1,0));

        mainPanel.add(pnlControls);

        this.setSize(500, 300);
        this.setVisible(true);


    } // end setup

    public void registerListeners(){


        btnAddAcct.addActionListener(this);



    } // end registerListeners

public void actionPerformed(ActionEvent e){

        //check all button presses for action


      //back button
        if (e.getSource() == btnAddAcct){
            boolean success = true;
            int zip;
            String fName = "";
            String lName ="";
            String address;
            String city;
            String state;
            try{
                zip = Integer.parseInt(txtZip.getText());
                fName = txtFName.getText();
                lName = txtLName.getText();
                address = txtAddress.getText();
                city = txtCity.getText();
                state = txtState.getText();


            }
            catch(NumberFormatException e1){
                lblInfo.setText("Please enter number for zip");
                success = false;
            }
            catch(NullPointerException e1){
                lblInfo.setText("All fields required");
                success = false;
            }
            if(success){
                Account account = new Account(fName,lName);
                this.accounts.add(account);
                String accountNum = Integer.toString(account.getAcctNum());
                lblInfo.setText(accountNum);
            }


        } 


        else {


        } // end if


    } // end actionPerformed

}

共 (1) 个答案

  1. # 1 楼答案

    问题在于这两条线:

    this.accounts = new ArrayList<Account>();
    this.accounts = accounts;
    

    你可以分配这个。accounts到一个新的ArrayList,但在这之后,您会将其重新分配到accounts参数。您创建的ArrayList将在某个时刻被垃圾收集,因为您实际上没有在调用代码中创建列表,所以代码会抛出NullPointerException