Spring自动装配:引起:java.lang.IllegalArgumentException:无法设置null TableModel

问题描述 投票:0回答:1

我想从主GUI中打开一个带有JTable的JDialog。为了在这个JTable中显示数据,我有一个名为“PvsTTableModel”的TableModel ..

尝试在类“KPI_PvsT”(第二个代码 - 此问题中的文本)中设置tablemodel“model”时会抛出异常,尽管它应该自动装配。

Dialog被称为“KPI_PvsT”,当有人点击menuItem“itemPvsT”时会调用它。

Main-GUI中有更多的对象都能正常工作(他们能够成功创建具有TableModels的JDialogs - 我已经将它们与我的相比)。

我找不到任何由“新”关键字创建的关键字,这也可能是here所描述的问题。所有内容都注明“@Component”和“@Autowiring”。

这是我的桌型:

@Component
public class PvsTTableModel extends AbstractTableModel {

    @Autowired
    private TicketRepository ticketRepository;

    @Autowired
    private ResidentRepository residentRepository;

    @Autowired
    private ResidentSchichtRespository residentSchichtRepository;

    @Autowired
    private SchichtRepository schichtRepository;

    private final String[] HEADER = {"Stunde", "Anzahl Mitarbeiter", "Anzahl offene Tickets"};

    private Map<Integer, Integer> numberOfResidents = new HashMap<>();
    private Map<Integer, Integer> numberOfOpenTickets = new HashMap<>();

    @Override
public int getRowCount() {
    return numberOfOpenTickets.size();
}

@Override
public int getColumnCount() {
    return HEADER.length;
}

@Override
public Object getValueAt(int rowIndex, int columnIndex) {
    switch(columnIndex){
        case 0: return rowIndex;
        case 1: return this.numberOfResidents.get(rowIndex);
        case 2: return this.numberOfOpenTickets.get(rowIndex);
    }

    return null;
}

这是JDialog:

@Component
public class KPI_PvsT extends JDialog {
    private JPanel contentPane;
    private JButton buttonCancel;
    private JTable table;
    private JTextField tfDatum;
    private JButton btOk;

    @Autowired
    private PvsTTableModel model;

    public KPI_PvsT() {
        setContentPane(contentPane);
        setModal(true);

        buttonCancel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onCancel();
            }
        });

        // call onCancel() when cross is clicked
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                onCancel();
            }
        });

        // call onCancel() on ESCAPE
        contentPane.registerKeyboardAction(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onCancel();
            }
        }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

        table.setModel(model);

        btOk.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String s = tfDatum.getText();
                LocalDate date = null;
                try{
                    date = LocalDate.parse(s, dtfLocalDate);
                }catch(Exception ex){
                    JOptionPane.showMessageDialog(null, "Bitte geben Sie das Datum im richtigen Format ein!");
                }

                if(date != null){
                    model.fillMaps(date);
                }
            }
        });
    }

    private void onOK() {
        // add your code here
        dispose();
    }

    private void onCancel() {
        // add your code here if necessary
        dispose();
    }

    public void init()
    {
        this.pack();
        this.setVisible(true);
    }
}

这是调用JDialog的GUI:

@Component
public class MainGUI extends JFrame{

@Autowired
private KPI_PvsT kpi_pvsT;

private JMenu menuHotline = new JMenu();
private JMenuItem itemPvsT = new JMenuItem();
public MainGUI() {
        addListeners();
}

private void addListeners()
{
    itemPvsT.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                kpi_pvsT.init();

            }
        });
}
public void starte(){

 menuHotline.setText("Hotline");
 itemPvsT.setText("Berechne Gegenüberstellung von Ticket und Personal");

}
}

错误堆栈跟踪

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'KPI_PvsT' defined in file [D:\HTBLA_Kaindorf\5AHIF\Diplomarbeit\Projekt\chantal_master\chantal\out\production\classes\com\chantal\gui\KPI_PvsT.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.chantal.gui.KPI_PvsT]: Constructor threw exception; nested exception is java.lang.IllegalArgumentException: Cannot set a null TableModel
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1155) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1099) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867) ~[spring-context-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543) ~[spring-context-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) ~[spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) ~[spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) ~[spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE]
    at org.springframework.boot.builder.SpringApplicationBuilder.run(SpringApplicationBuilder.java:134) [spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE]
    at com.chantal.ChantalApplication.main(ChantalApplication.java:22) [classes/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_161]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_161]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_161]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_161]
    at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-1.5.9.RELEASE.jar:1.5.9.RELEASE]
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.chantal.gui.KPI_PvsT]: Constructor threw exception; nested exception is java.lang.IllegalArgumentException: Cannot set a null TableModel
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:154) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:89) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1147) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    ... 21 common frames omitted
Caused by: java.lang.IllegalArgumentException: Cannot set a null TableModel
    at javax.swing.JTable.setModel(JTable.java:3678) ~[na:1.8.0_161]
    at com.chantal.gui.KPI_PvsT.<init>(KPI_PvsT.java:49) ~[classes/:na]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_161]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_161]
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_161]
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_161]
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:142) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    ... 23 common frames omitted
java spring swing spring-boot
1个回答
3
投票

正如@kdowbecki指出的那样,你在构造函数中使用自动装配值“model”,但是自动装配的值是在对象被实例化之后设置的,所以在构造时它仍然是null。你应该做的是将@Autowired注释设置为构造函数,并将模型作为构造函数参数。

//NOT Autowired
private PvsTTableModel model;

@Autowired
public KPI_PvsT(PvsTTableModel model) {
    this.model = model;
    [...]
}

仅供参考,构造器自动装配是Spring现在推荐的注射方式,在最新版本的Spring中,甚至不再需要@Autowired on构造函数,因此类可以完全不知道DI。

© www.soinside.com 2019 - 2024. All rights reserved.