我的控制器:
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时出错。显示条件报告 在启用“调试”的情况下重新运行您的应用程序。错误 o.s.boot.SpringApplication-应用程序运行失败 org.springframework.beans.factory.UnsatisfiedDependencyException: 创建名称为'controllerx'的bean时出错:不满意的依赖关系 通过字段“框架”表示;嵌套异常为 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(本机方法)位于 sun.reflect.NativeMethodAccessorImpl.invoke(未知来源)位于 sun.reflect.DelegatingMethodAccessorImpl.invoke(未知源)位于 java.lang.reflect.Method.invoke(来源未知) org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) 引起原因: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.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:在时为null 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个通用框架
答案 0 :(得分:0)
您的名称为JTextField
的{{1}}变量为空(它是字段,您没有对其进行初始化),这就是为什么这样做时要使用NPE的原因
jLogin
看看loginForm.add(jLogin);
类的documentation-JPanel
方法部分有一个信息
add
抛出:NullPointerException-如果comp为null
似乎在添加public Component add(Component comp)
顺便说一句,您应该仔细阅读日志,因为所有内容都在其中
由以下原因引起:java.lang.NullPointerException:null
在java.awt.Container.addImpl(未知来源)
在java.awt.Container.add(未知来源)
在com.market.View.initComponents( View.java:56 )
位于com.market.View。( View.java:39 )位于