0
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 返回一个值,所以不为空):

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

我究竟做错了什么?

4

1 回答 1

1

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

jComboBoxCompaniesByName = new JCombobox();

只是猜测...尝试initComponents()在第 54 行之前移动。

于 2012-06-02T08:50:53.800 回答