创建名为“controllerx”的 bean 时出错:

Posted

技术标签:

【中文标题】创建名为“controllerx”的 bean 时出错:【英文标题】:Error creating bean with name 'controllerx': 【发布时间】:2019-12-10 12:01:42 【问题描述】:

我的控制器:

package com.market;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;




@Controller 
public class Controllerx 

    @Autowired
    private View frame;


/*
    private void init() 
        this.frame.addWindowListener(this);
        registerAction(frame.getbEnter(), new AbstractAction() 
            private List<Funcionarios> list; 

            @Override
            public void action() 
                if (!frame.getjLogin().getText().trim().isEmpty() || !frame.getjPassword().getText().trim().isEmpty()) 

                else 
                    JOptionPane.showMessageDialog(frame, null, "Campo vázio", JOptionPane.INFORMATION_MESSAGE);
                
            

            @Override
            public void posAction() 
                cleanUp();
            
        );
    
    */
    public void show() 
        frame.setVisible(true);
    



我的主要:

@SpringBootApplication
@EnableAutoConfiguration
public class Market1Application 

    public static void main(String[] args) 
        ConfigurableApplicationContext context = new SpringApplicationBuilder(Market1Application.class).headless(false).run(args);
        Controllerx appLogin = context.getBean(Controllerx.class);
    


我的看法:

package com.market;

import java.awt.FlowLayout;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.springframework.stereotype.Component;


@Component
public class View extends JFrame

    private static final int FRAME_WIDTH = 400;
    static final int FRAME_HEIGHT = 350;
    static final int FRAME_X_ORIGIN = 150;
    static final int FRAME_Y_ORIGIN = 150;
    static final int BUTTON_WIDTH = 90;
    static final int BUTTON_HEIGHT = 30;
    private JTextField jLogin;
    private JTextField jPassword;
    private JButton bEnter;
    private JButton bRegister;
    private JPanel loginForm;
    private JLabel loginText;
    private JLabel passwordText;

    public View() 
        setTitle("Login System");
        setSize         (FRAME_WIDTH, FRAME_HEIGHT);
        setLocation     (FRAME_X_ORIGIN, FRAME_Y_ORIGIN);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new FlowLayout());
        setResizable(false);
        initComponents();
    
    private void initComponents() 
        loginForm = new JPanel(new GridLayout(3,1));
        //buttons
        bRegister = new JButton("Registrar");
        bRegister.setBounds(195, 285, BUTTON_WIDTH, BUTTON_HEIGHT);
        bEnter = new JButton("Registrar");
        bEnter.setBounds(195, 285, BUTTON_WIDTH, BUTTON_HEIGHT);
        //bEnter.setMnemonic(NativeKeyEvent.VC_ENTER);
        //labels
        loginText = new JLabel();
        loginText.setText("Username:");
        passwordText = new JLabel();
        passwordText.setText("Password:");
        //
        loginForm.add(loginText);
        loginForm.add(jLogin);
        loginForm.add(passwordText);
        loginForm.add(jPassword);
        loginForm.add(bEnter);
        loginForm.add(bRegister);
        setVisible(true);
    

    public void resetForm() 
        jLogin.setText("");
        jPassword.setText("");
    
    public JTextField getjLogin() 
        return jLogin;
    
    public void setjLogin(JTextField jLogin) 
        this.jLogin = jLogin;
    
    public JTextField getjPassword() 
        return jPassword;
    
    public void setjPassword(JTextField jPassword) 
        this.jPassword = jPassword;
    
    public JButton getbEnter() 
        return bEnter;
    
    public void setbEnter(JButton bEnter) 
        this.bEnter = bEnter;
    


我有这些错误编译我不知道我哪里出错了我只有这 3 个类

启动 ApplicationContext 时出错。显示条件报告 在启用“调试”的情况下重新运行您的应用程序。错误 os.boot.SpringApplication - 应用程序运行失败 org.springframework.beans.factory.UnsatisfiedDependencyException: 不满足的依赖关系 通过字段“框架”表示;嵌套异常是 org.springframework.beans.factory.BeanCreationException:错误 创建文件中定义的名称为“view”的bean [C:\Users\Gabriel\eclipse-workspace\gabrielestudo\market-1\target\classes\com\market\View.class]: bean 实例化失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:失败 实例化 [com.market.View]:构造函数抛出异常;嵌套的 异常是 java.lang.NullPointerException 在 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:596) 在 org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90) 在 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:374) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1411) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:592) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515) 在 org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) 在 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) 在 org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) 在 org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) 在 org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:845) 在 org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:877) 在 org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549) 在 org.springframework.boot.SpringApplication.refresh(SpringApplication.java:742) 在 org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:389) 在 org.springframework.boot.SpringApplication.run(SpringApplication.java:311) 在 org.springframework.boot.builder.SpringApplicationBuilder.run(SpringApplicationBuilder.java:139) 在 com.market.Market1Application.main(Market1Application.java:16) 在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 在 sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(未知来源)在 java.lang.reflect.Method.invoke(未知来源)在 org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) 引起:org.springframework.beans.factory.BeanCreationException: 创建文件中定义的名称为“视图”的 bean 时出错 [C:\Users\Gabriel\eclipse-workspace\gabrielestudo\market-1\target\classes\com\market\View.class]: bean 实例化失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:失败 实例化 [com.market.View]:构造函数抛出异常;嵌套的 异常是 java.lang.NullPointerException 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1303) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1197) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515) 在 org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) 在 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) 在 org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) 在 org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) 在 org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:277) 在 org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1251) 在 org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1171) 在 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:593) ... 省略了 22 个常见的帧 原因: org.springframework.beans.BeanInstantiationException:失败 实例化 [com.market.View]:构造函数抛出异常;嵌套的 异常是 java.lang.NullPointerException 在 org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:184) 在 org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:87) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1295) ... 省略了 33 个常见框架 原因: java.lang.NullPointerException:空在 java.awt.Container.addImpl(未知来源)在 java.awt.Container.add(未知来源)在 com.market.View.initComponents(View.java:56) 在 com.market.View.(View.java:39) 在 sun.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法) 在 sun.reflect.NativeConstructorAccessorImpl.newInstance(未知 来源)在 sun.reflect.DelegatingConstructorAccessorImpl.newInstance(未知 来源)在 java.lang.reflect.Constructor.newInstance(未知来源) 在 org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:172) ... 省略了 35 个常用帧

【问题讨论】:

【参考方案1】:

你的名为 jLoginJTextField 变量为空(它是字段,你没有初始化它),这就是为什么你在做时有 NPE

loginForm.add(jLogin);

看看JPanel类的documentation——在add方法部分有一个信息

public Component add(Component comp)

抛出:NullPointerException - 如果 comp 为空

在添加jPassword时,您至少会在58行遇到同样的问题


顺便说一句,您应该仔细阅读日志,因为一切都在那里

原因:java.lang.NullPointerException: null

在 java.awt.Container.addImpl(Unknown Source)

在 java.awt.Container.add(未知来源)

在 com.market.View.initComponents(View.java:56)

在 com.market.View.(View.java:39) 在

【讨论】:

天哪,我忘了说非常感谢你,兄弟你能帮我一把吗?当我尝试添加更多类时出现 bean 错误 如果你能帮助我

以上是关于创建名为“controllerx”的 bean 时出错:的主要内容,如果未能解决你的问题,请参考以下文章

创建名为“springSecurityFilterChain”的 bean 时出错

创建名为“dietaController”的 bean 时出错

org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为“entityManagerFactory”的bean时出错

org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为“batchConfiguration”的bean时出错

org.springframework.beans.factory.BeanCurrentlyInCreationException:创建名为“sessionFactory”的bean时出错

Spring Boot - “创建名为 'entityManagerFactory' 的 bean 时出错” - 开始