0

I'm trying to fill a jComboBox with objects. I have it working in one class, but in this class it's giving a NullPointerException but the code is almost the same. What am I missing here?

The code I'm using to fill the comboboxes:

I have translated every variable to English and removed some unnescessary stuff. I hope it's more clear for you guys now:

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);
    }   
}

Here are the methods used:

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();
}

This is how my lists are initialized:

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();
}

Edit: Here's the MainGUI which calls createNewList (maakLijstAan)

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");
}
Forza
  • 1,619
  • 3
  • 28
  • 49
  • 1
    if you tell us where exactly you get the NPE this would help too. – Timo Hahn Oct 12 '12 at 10:25
  • 1
    what in the heck's name are those names in your variables ? use some CamelCase and shorter names!2 – Matei Suica Oct 12 '12 at 10:33
  • I'm getting the NPE with this line: for (Object object : verzekeringbeheer.getMaatschappijNamen()) – Forza Oct 12 '12 at 10:41
  • 1
    1) For better help sooner, post an [SSCCE](http://sscce.org/). 2) Since English is the default language of both programming and the site, and the variable names are intended to be instructive, make the variable names in English (and use CamelCase etc.). – Andrew Thompson Oct 12 '12 at 10:53
  • @Forza: if you get an NPE, verzekeringbeheer is null. Since we don't have that part of your code, it is impossible to tell what's wrong – Guillaume Polet Oct 12 '12 at 11:42
  • I'm sorry for not being constructive. I have added verzekeringbeheer to the OP, above the combobox update methods. Also It is difficult to rename all variable names to english because I don't know the english words for them.. It's about insurances. Verzekering = Insurance. Verzekeringbeheer = InsuranceManager – Forza Oct 12 '12 at 11:49
  • @Forza http://translate.google.com – Guillaume Polet Oct 12 '12 at 11:53
  • I have translated the variables to English and cleaned up some stuff. Please check the OP for me, I hope it is more clear for you now ;) – Forza Oct 12 '12 at 12:10

3 Answers3

1
Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
1

You never initialize verzekeringBeheer, therefore you get a NullPointerException when you try to invoke methods on that variable.

You should have somewhere in your constructor, something like this:

verzekeringbeheer = new VerzekeringBeheer();

Also, try to avoid making your code coupled with other parts of your code. For example:

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();
}

If nobody calls maakLijstAan, you will get a NullPointerException in getMaatschappijNamen. Try to avoid code that is so dependent of external code, in order to run without problems.

Guillaume Polet
  • 47,259
  • 4
  • 83
  • 117
  • I have a MainGUI class which calls maakLijstAan (createNewList). Added it to the OP now. – Forza Oct 12 '12 at 12:25
  • @Forza I am sure you have somewhere a call to that method. I am just saying that by choosing such a code pattern, you are looking for troubles, tightly couple code and hard-to-maintain code. Anyway, your original problem is still the same, you never instantiate `verzekeringBeheer` – Guillaume Polet Oct 12 '12 at 12:38
  • @Forza also drop those `static` keywords in your InsuranceManager class. They are needless. – Guillaume Polet Oct 12 '12 at 12:41
  • Daaamn all those trouble for not initializing verzekeringBeheer.. I want to jump out the window now... – Forza Oct 12 '12 at 12:46
  • One more thing: static keywords are necessary because if I leave them I get another NPE. And now I got one combobox working and one not. InsuranceComboBox isn't loading any values – Forza Oct 12 '12 at 12:47
  • I will mark your answer as accepted ;) Initial question is solved now! Thank you very much for your patience with me ;) – Forza Oct 12 '12 at 12:57
  • @Forza no problem. Still, forgive me for repeating myself, but you should really remove those `static` keywords. They are the sign of a problematic design which eventually will force you to refactor a lot of code. `static` is the root of all evil in Java when it is not used to declare a constant value or for some methods. It goes against all Object-Oriented principles. – Guillaume Polet Oct 12 '12 at 14:37
0

I only see you useing variables but for me they are nit initialized. So they are null and you get a NPE.

So how are verzekeringmaatschappijLijst and verzekeringLijst initialized?

Timo Hahn
  • 2,466
  • 2
  • 19
  • 16