This question already has answers here:
What is a NullPointerException, and how do I fix it?

(12个答案)


4年前关闭。






package com.schinbehn.model;

import java.util.ArrayList;

public class CustomerReg {

	private ArrayList<Customer> customerList;




	public ArrayList<Customer> getCustomers() {
		return customerList;
	}

	public void setCustomers(ArrayList<Customer> newCustomers) {
		customerList = newCustomers;
	}

	public void addCustomer(Customer c) {
		customerList.add(c);
	}

	public Customer findCustomer(String orgNummber) {
		for (Customer tmp : customerList) {
			if (orgNummber.equals(tmp.getOrgNumber())) {
				return tmp;
			}
		}
		return null;
	}

}





嗨,我很难掌握MVC设置。我正在学校的项目上工作,并使用swing / windowbuilder创建UI。现在的问题是,当我按下jbutton“ Spara”时,我得到了一个nullpointerexception。这应该创建一个类Customer的新对象,并从文本框中获取所有信息,然后将该对象添加到我在名为CustomerReg的类中拥有的数组列表中。以及我客户列表的名称。我还包括控制器类。

控制者

package com.schinbehn.controller;

import com.schinbehn.model.Customer;
import com.schinbehn.model.CustomerReg;

public class Controller {

  private CustomerReg customers;
  private Customer customer;


  public Controller() {
    customers = new CustomerReg();

  }

  public Customer findCustomer(String orgNumber) {
    return customers.findCustomer(orgNumber);

  }

  public void addCustomer(Customer k) {
    customers.addCustomer(k);
  }



}


视图

package com.schinbehn.view;

import java.awt.EventQueue;

import javax.swing.JFrame;
import java.awt.CardLayout;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JLabel;
import javax.swing.JTextField;

import com.schinbehn.model.Customer;
import com.schinbehn.controller.Controller;
import com.schinbehn.model.CustomerReg;
import com.schinbehn.model.Test;
import javax.swing.JTextArea;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JPasswordField;
import java.awt.Font;
import javax.swing.JSeparator;
import javax.swing.JToolBar;
import javax.swing.JTabbedPane;
import javax.swing.JInternalFrame;
import javax.swing.JProgressBar;
import org.eclipse.wb.swing.FocusTraversalOnArray;

import java.awt.Component;
import java.awt.Color;

public class ApplicationView {


  private Controller controller;
  private CustomerReg customerReg;
  private Customer customer;
  private JFrame frame;
  private JButton btnCustomerButton;
  private JPanel panelMenu;
  private JPanel Customer;
  private JPanel panelBuy;
  private JTextField txtKundsOrgnummer;
  private JTextField textFieldOrgNr;
  private JTextField textFieldComanyName;
  private JTextField textFieldBillingAdress;
  private JTextField textFieldCompanyContact;
  private JTextField textFieldEmail;
  private JTextField textFieldPhoneNumber;
  private JTextField textFieldAgreement;


  /**
   * Launch the application.
   */
  public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
      public void run() {
        try {
          ApplicationView window = new ApplicationView();
          window.frame.setVisible(true);
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    });
  }

  /**
   * Create the application.
   */

  public ApplicationView() {
    initialize();
  }

  /**
   * Initialize the contents of the frame.
   */
  private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 1024, 600);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(new CardLayout(0, 0));

    JPanel panelMenu = new JPanel();
    frame.getContentPane().add(panelMenu, "name_502949178913681");
    panelMenu.setLayout(null);
    panelMenu.setVisible(true);

    JPanel panelCustomer = new JPanel();
    frame.getContentPane().add(panelCustomer, "name_502951876763802");
    panelCustomer.setLayout(null);
    panelCustomer.setVisible(false);

    JPanel panelBuy = new JPanel();
    frame.getContentPane().add(panelBuy, "name_509394152157827");
    panelBuy.setLayout(null);

    JPanel panelNewCustomer = new JPanel();
    frame.getContentPane().add(panelNewCustomer, "name_520111794431871");
    panelNewCustomer.setLayout(null);

    JLabel lblNewLabel = new JLabel("Org.Nummer:");
    lblNewLabel.setBounds(54, 61, 78, 14);
    panelNewCustomer.add(lblNewLabel);

    JLabel lblFirmaNamn = new JLabel("Firmanamn:");
    lblFirmaNamn.setBounds(54, 101, 78, 14);
    panelNewCustomer.add(lblFirmaNamn);

    JLabel lblFakturaadress = new JLabel("Fakturaadress:");
    lblFakturaadress.setBounds(54, 142, 96, 14);
    panelNewCustomer.add(lblFakturaadress);

    JLabel lblKontaktperson = new JLabel("Kontaktperson:");
    lblKontaktperson.setBounds(54, 192, 96, 14);
    panelNewCustomer.add(lblKontaktperson);

    JLabel lblNewLabel_1 = new JLabel("Emailadress:");
    lblNewLabel_1.setBounds(54, 223, 78, 14);
    panelNewCustomer.add(lblNewLabel_1);

    JLabel lblTelnummer = new JLabel("Tel.Nummer:");
    lblTelnummer.setBounds(54, 248, 78, 14);
    panelNewCustomer.add(lblTelnummer);

    JLabel lblAvtalMedKund = new JLabel("Avtal med kund:");
    lblAvtalMedKund.setBounds(54, 357, 96, 14);
    panelNewCustomer.add(lblAvtalMedKund);

    textFieldOrgNr = new JTextField();
    textFieldOrgNr.setBounds(142, 58, 138, 20);
    panelNewCustomer.add(textFieldOrgNr);
    textFieldOrgNr.setColumns(10);

    textFieldComanyName = new JTextField();
    textFieldComanyName.setBounds(142, 98, 138, 20);
    panelNewCustomer.add(textFieldComanyName);
    textFieldComanyName.setColumns(10);

    textFieldBillingAdress = new JTextField();
    textFieldBillingAdress.setBounds(142, 139, 138, 20);
    panelNewCustomer.add(textFieldBillingAdress);
    textFieldBillingAdress.setColumns(10);

    textFieldCompanyContact = new JTextField();
    textFieldCompanyContact.setBounds(142, 189, 138, 20);
    panelNewCustomer.add(textFieldCompanyContact);
    textFieldCompanyContact.setColumns(10);

    textFieldEmail = new JTextField();
    textFieldEmail.setBounds(142, 220, 138, 20);
    panelNewCustomer.add(textFieldEmail);
    textFieldEmail.setColumns(10);

    textFieldPhoneNumber = new JTextField();
    textFieldPhoneNumber.setBounds(142, 245, 138, 20);
    panelNewCustomer.add(textFieldPhoneNumber);
    textFieldPhoneNumber.setColumns(10);

    textFieldAgreement = new JTextField();
    textFieldAgreement.setBounds(142, 354, 279, 171);
    panelNewCustomer.add(textFieldAgreement);
    textFieldAgreement.setColumns(10);

    JButton btnNewCustomerSpara = new JButton("Spara");
    btnNewCustomerSpara.addActionListener(new ActionListener() {

      @
      Override
      public void actionPerformed(ActionEvent e) {
        Customer c = new Customer();
        c.setOrgNumber(textFieldOrgNr.getText());
        c.setCompanyName(textFieldComanyName.getText());
        c.setBillingAdress(textFieldBillingAdress.getText());
        c.setCompanyContact(textFieldCompanyContact.getText());
        c.setEmail(textFieldEmail.getText());
        c.setAgreement(textFieldAgreement.getText());
        c.setPhoneNumber(textFieldPhoneNumber.getText());

        **customerReg.addCustomer(c);**

      }
    });
    btnNewCustomerSpara.setBounds(431, 461, 89, 23);
    panelNewCustomer.add(btnNewCustomerSpara);

    JButton btnNewCustomerBackButton = new JButton("Tillbaka");
    btnNewCustomerBackButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        panelNewCustomer.setVisible(false);
        panelCustomer.setVisible(true);
      }
    });
    btnNewCustomerBackButton.setBounds(431, 502, 89, 23);
    panelNewCustomer.add(btnNewCustomerBackButton);

    JLabel lblSchinBehn_2 = new JLabel("Schin & Behn L\u00E4gg till ny kund");
    lblSchinBehn_2.setFont(new Font("Tahoma", Font.PLAIN, 16));
    lblSchinBehn_2.setBounds(10, 11, 301, 45);
    panelNewCustomer.add(lblSchinBehn_2);

    JPanel panelFindCustomer = new JPanel();
    frame.getContentPane().add(panelFindCustomer, "name_520115016572819");
    panelFindCustomer.setLayout(null);

    JLabel lblSkKund = new JLabel("S\u00F6k kund");
    lblSkKund.setBounds(20, 67, 66, 14);
    panelFindCustomer.add(lblSkKund);

    txtKundsOrgnummer = new JTextField();
    txtKundsOrgnummer.setForeground(Color.GRAY);
    txtKundsOrgnummer.setText("kunds orgnummer");
    txtKundsOrgnummer.setBounds(83, 64, 194, 20);
    panelFindCustomer.add(txtKundsOrgnummer);
    txtKundsOrgnummer.setColumns(10);

    JButton btnFindKundFindButton = new JButton("S\u00F6k");
    btnFindKundFindButton.setBounds(287, 63, 89, 23);
    panelFindCustomer.add(btnFindKundFindButton);

    JLabel lblSchinBehn_3 = new JLabel("Schin & Behn S\u00F6k kund");
    lblSchinBehn_3.setFont(new Font("Tahoma", Font.PLAIN, 16));
    lblSchinBehn_3.setBounds(10, 11, 301, 45);
    panelFindCustomer.add(lblSchinBehn_3);

    JButton btnFindCustomerBackButton = new JButton("Tillbaka");
    btnFindCustomerBackButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        panelFindCustomer.setVisible(false);
        panelCustomer.setVisible(true);
      }
    });
    btnFindCustomerBackButton.setBounds(422, 489, 89, 23);
    panelFindCustomer.add(btnFindCustomerBackButton);

    JButton btnBuyBackButton = new JButton("Tillbaka");
    btnBuyBackButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        panelBuy.setVisible(false);
        panelMenu.setVisible(true);

      }
    });
    btnBuyBackButton.setBounds(909, 502, 89, 23);
    panelBuy.add(btnBuyBackButton);

    JLabel lblVlkommenTillSchin = new JLabel("V\u00E4lkommen till Schin & Behn");
    lblVlkommenTillSchin.setFont(new Font("Tahoma", Font.PLAIN, 42));
    lblVlkommenTillSchin.setBounds(223, 11, 550, 78);
    panelMenu.add(lblVlkommenTillSchin);

    JLabel lblSchinBehn = new JLabel("Schin & Behn Webbshop");
    lblSchinBehn.setFont(new Font("Tahoma", Font.PLAIN, 16));
    lblSchinBehn.setBounds(10, 11, 195, 45);
    panelBuy.add(lblSchinBehn);
    panelBuy.setVisible(false);

    JLabel lblSchinBehn_1 = new JLabel("Schin & Behn Kundhantering");
    lblSchinBehn_1.setFont(new Font("Tahoma", Font.PLAIN, 16));
    lblSchinBehn_1.setBounds(10, 11, 213, 45);
    panelCustomer.add(lblSchinBehn_1);

    JButton btnNewCustomerButton = new JButton("Lägg till ny kund");
    btnNewCustomerButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        panelCustomer.setVisible(false);
        panelNewCustomer.setVisible(true);
      }
    });
    btnNewCustomerButton.setBounds(57, 88, 130, 38);
    panelCustomer.add(btnNewCustomerButton);

    JButton btnFindCustomerButton = new JButton("Sök kund");
    btnFindCustomerButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        panelCustomer.setVisible(false);
        panelFindCustomer.setVisible(true);
      }
    });
    btnFindCustomerButton.setBounds(214, 88, 137, 38);
    panelCustomer.add(btnFindCustomerButton);
    panelCustomer.setFocusTraversalPolicy(new FocusTraversalOnArray(
      new Component[] {
        lblSchinBehn_1, btnNewCustomerButton, btnFindCustomerButton
      }));

    JMenuBar menuBar = new JMenuBar();
    frame.setJMenuBar(menuBar);

    btnCustomerButton = new JButton("Kundhanteing");
    menuBar.add(btnCustomerButton);

    JButton btnBuyButton = new JButton("Köp");
    menuBar.add(btnBuyButton);
    btnBuyButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        panelBuy.setVisible(true);
        panelMenu.setVisible(false);
        panelCustomer.setVisible(false);
      }
    });
    btnCustomerButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        panelCustomer.setVisible(true);
        panelMenu.setVisible(false);
        panelBuy.setVisible(false);
      }
    });

  }

  public JButton getBtnCustomerButton() {
    return btnCustomerButton;
  }
}

最佳答案

NPE的原因是您的customerReg从未在ApplicationView类中创建:

将其写在ApplicationView的成员部分中:

private CustomerReg customerReg = new CustomerReg();


甚至更好。让控制器执行此工作。但是也必须创建controller(在您的代码中不是这样):

private Controller controller = new Controller();

  @Override
  public void actionPerformed(ActionEvent e) {
    Customer c = new Customer();
    c.setOrgNumber(textFieldOrgNr.getText());
    c.setCompanyName(textFieldComanyName.getText());
    c.setBillingAdress(textFieldBillingAdress.getText());
    c.setCompanyContact(textFieldCompanyContact.getText());
    c.setEmail(textFieldEmail.getText());
    c.setAgreement(textFieldAgreement.getText());
    c.setPhoneNumber(textFieldPhoneNumber.getText());

    controller.addCustomer(c);

  }
});

09-25 20:15