列出数组并放入jComboBox

列出数组并放入jComboBox

AdministratorFrame(Administrator account) {
    AdministratorFrameLocked(true);
    administrator = account;
    EntityManagerFactory entityManagerFactory
            = Persistence.createEntityManagerFactory("KwestionariuszForRoot");
    EntityManager entityManager
            = entityManagerFactory.createEntityManager();
    List<Company> companies
            = entityManager.createNamedQuery("Company.findByAdministratorId")
            .setParameter("administrator_id", administrator).getResultList();
    entityManager.close();
    System.out.println(companies);
    List<String> companyNames = new ArrayList<>();
    for (Company company : companies) {
        companyNames.add(company.getName().toString());
    }
    System.out.println(companyNames);
    String[] companiesArray = new String[companyNames.size()];
    companiesArray = companyNames.toArray(companiesArray);
    System.out.println(companiesArray);
    jComboBoxCompaniesByName
            .setModel(new javax.swing.DefaultComboBoxModel(companiesArray));
    initComponents();
    AdministratorFrameLocked(false);
}


返回值(请注意,printlns返回一个值,因此不为null):

[model.Company[ id=1 ]]
[Mentor]
[Ljava.lang.String;@139fcc0
Exception in thread "AWT-EventQueue-0"
java.lang.NullPointerException at
frames.AdministratorFrame.<init>(AdministratorFrame.java:54)


第54行是:

jComboBoxCompaniesByName
                .setModel(new javax.swing.DefaultComboBoxModel(companiesArray));


我究竟做错了什么?

最佳答案

您在哪里初始化jComboBoxCompaniesByName
应该在某个地方像这样初始化:

jComboBoxCompaniesByName = new JCombobox();


只是一个猜测...尝试将initComponents()移动到第54行之前。

关于java - 列出数组并放入jComboBox,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10860987/

10-15 18:54