0

我正在尝试用对象填充 jComboBox。我让它在一个类中工作,但在这个类中它给出了一个NullPointerException但代码几乎相同。我在这里想念什么?

我用来填充组合框的代码:

我已将每个变量都翻译成英文并删除了一些不必要的东西。我希望你们现在更清楚:

package unive.billing.boundary.clientmanager.frames;

import unive.billing.control.ClientsManager;
import unive.billing.control.InsuranceManager;

/**
 *
 * @author Forza
 */

public class ClientFrame extends javax.swing.JFrame {

/**
 * Creates new form AddClientGUI
 */
private ClientsManager clientmanager;
private InsuranceManager insurancemanager;

public ClientFrame() {
    initComponents();
    clientmanager = new ClientsManager();
    clientmanager.printList();
    updateComboBoxCompany();
    updateComboBoxInsurance();
}

private ClientsManager clientmanager;
private InsuranceManager insurancemanager;

public ClientFrame() {
    initComponents();
    clientmanager = new ClientsManager();
    clientmanager.printList();
    updateComboBoxCompany();
    updateComboBoxInsurance();
}

public void updateComboBoxCompany() 
{
    for (Object object : insurancemanager.getCompanyNames()) 
    {
        companyComboBox.addItem(object);
    }   
}

public void updateComboBoxInsurance() 
{
    for (Object object : insurancemanager.getPolicyNames()) 
    {
        insuranceComboBox.addItem(object);
    }   
}

以下是使用的方法:

public Object[] getCompanyNames() 
{
    ArrayList<String> cnames = new ArrayList<String>();
    for (InsurancesCompany company : insurancecompanyList) 
    {
        cnames.add(company.getCompanyName());
    }
    return cnames.toArray();
}

public Object[] getPolicyNames() 
{
    ArrayList<String> vnames = new ArrayList<String>();
    for (Insurance insurance : insuranceList) 
    {
        vnames.add(insurance.getPolicyName());
    }
    return vnames.toArray();
}

这就是我的列表的初始化方式:

public class InsuranceManager {

private String insurancePath;
private String insurancecompanyenPath;
private static List<InsurancesCompany> insurancecompanyList;
private static List<Insurance> insuranceList;
private Insurance currentInsurance;

public InsuranceManager() {
    insurancecompanyenPath = "Files/company.txt";
    insurancePath = "Files/insurance.txt";
    insuranceList = new List<>();
}

public void createNewList() 
{
    insurancecompanyList = new List<>();
    System.out.println("Creates list");
}

public Object[] getCompanyNames() 
{
    ArrayList<String> cnames = new ArrayList<String>();
    for (InsurancesCompany company : insurancecompanyList) 
    {
        cnames.add(company.getCompanyName());
    }
    return cnames.toArray();
}

public Object[] getPolicyNames() 
{
    ArrayList<String> vnames = new ArrayList<String>();
    for (Insurance insurance : insuranceList) 
    {
        vnames.add(insurance.getPolicyName());
    }
    return vnames.toArray();
}

编辑:这是调用 createNewList (maakLijstAan) 的 MainGUI

private ClientsManager clientsmanager;
private BillingManager billingmanager;
private InsuranceManager insurancemanager;            

public MainGUI() {
    clientsmanager = new ClientsManager();  
    clientsmanager.CreateNewList();
    insurancemanager = new InsuranceManager();
    insurancemanager.CreateNewList();
    insurancemanager.loadInsuranceCompanyList();
    initComponents();
    jMenuItem1.setText("Save clients"); 
    jMenuItem2.setText("Load clients"); 
    jMenuItem3.setText("Exit");
}
4

3 回答 3

1

您永远不会初始化verzekeringBeheer,因此NullPointerException当您尝试调用该变量的方法时会得到一个。

您的构造函数中应该有某个地方,如下所示:

verzekeringbeheer = new VerzekeringBeheer();

此外,请尽量避免使您的代码与代码的其他部分耦合。例如:

public VerzekeringBeheer() {
    ...
    //verzekeringmaatschappijLijst is never initialized!!!
}

public void maakLijstAan() 
{
    verzekeringmaatschappijLijst = new Lijst<>();
    System.out.println("Maak lijst aan");
}

public Object[] getMaatschappijNamen() 
{
    ArrayList<String> mnamen = new ArrayList<String>();
    // Here you use verzekeringmaatschappijLijst without checking that is not null!!!
    for (VerzekeringsMaatschappij maatschappij : verzekeringmaatschappijLijst) 
    {
        mnamen.add(maatschappij.getMaatschappijNaam());
    }
    return mnamen.toArray();
}

如果没有人打电话,maakLijstAan你会得到一个NullPointerExceptiongetMaatschappijNamen尽量避免如此依赖外部代码的代码,以便顺利运行。

于 2012-10-12T11:58:37.470 回答
1
于 2012-10-12T10:54:37.657 回答
0

我只看到你使用变量,但对我来说它们是初始化的。所以它们是空的,你得到一个 NPE。

verzekeringmaatschappijLijst那么又 是如何verzekeringLijst初始化的呢?

于 2012-10-12T10:24:52.740 回答