java - 线程 AWT-EventQueue-0 (File.<init>) 出现 NullPointerException

标签 java macos

我正在尝试自学 Java 网络。我尝试编写一个小的 WebChat-Application,当我试图在 Eclipse 中运行它时它运行得很好,但是当我试图在 Debug模式下运行它时,我不断收到此错误:

Thread [AWT-EventQueue-0] (Suspended (exception NullPointerException))  
owns: Object  (id=39)   
File.<init>(String) line: 251   
LoadNativeBundleAction.run() line: 79   
AccessController.doPrivileged(PrivilegedExceptionAction<T>) line: not available [native method] 
MacOSXResourceBundle.getMacResourceBundle(String, String) line: 48  
MacOSXResourceBundle.getMacResourceBundle(String) line: 43  
AquaLookAndFeel.initResourceBundle(UIDefaults) line: 249    
AquaLookAndFeel.initComponentDefaults(UIDefaults) line: 264 
AquaLookAndFeel.getDefaults() line: 231 
UIManager.setLookAndFeel(LookAndFeel) line: 536 
UIManager.setLookAndFeel(String) line: 580  
UIManager.initializeDefaultLAF(Properties) line: 1345   
UIManager.initialize() line: 1455   
UIManager.maybeInitialize() line: 1422  
UIManager.getUI(JComponent) line: 1003  
JPanel.updateUI() line: 126 
JPanel.<init>(LayoutManager, boolean) line: 86  
JPanel.<init>(boolean) line: 109    
JPanel.<init>() line: 117   
JRootPane.createGlassPane() line: 545   
JRootPane.<init>() line: 365    
Login(JFrame).createRootPane() line: 277    
Login(JFrame).frameInit() line: 258 
Login(JFrame).<init>() line: 181    
Login.<init>() line: 28 
Login$2.run() line: 111 
InvocationEvent.dispatch() line: 251    
EventQueue.dispatchEventImpl(AWTEvent, Object) line: 727    
EventQueue.access$200(EventQueue, AWTEvent, Object) line: 103   
EventQueue$3.run() line: 688    
EventQueue$3.run() line: 686    
AccessController.doPrivileged(PrivilegedAction<T>, AccessControlContext) line: not available [native method]    
ProtectionDomain$1.doIntersectionPrivilege(PrivilegedAction<T>, AccessControlContext, AccessControlContext) line: 76    
EventQueue.dispatchEvent(AWTEvent) line: 697    
EventDispatchThread.pumpOneEventForFilters(int) line: 242   
EventDispatchThread.pumpEventsForFilter(int, Conditional, EventFilter) line: 161    
EventDispatchThread.pumpEventsForHierarchy(int, Conditional, Component) line: 150   
EventDispatchThread.pumpEvents(int, Conditional) line: 146  
EventDispatchThread.pumpEvents(Conditional) line: 138   
EventDispatchThread.run() line: 91  

我试图修复它,但我失败了很多次......

这是我的代码:

package tk.kaes3kuch3n.webchat;

import java.awt.EventQueue;

public class Login extends JFrame {

    private static final long serialVersionUID = 1L;
    private JPanel contentPane;
    private JTextField txtName;
    private JTextField txtAddress;
    private JLabel lblIp;
    private JTextField txtPort;
    private JLabel lblPort;
    private JLabel lblBspIp;
    private JLabel lblBspPort;

    public Login() {

        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException | InstantiationException
                | IllegalAccessException | UnsupportedLookAndFeelException e) {
            e.printStackTrace();
        }

        setResizable(false);
        setTitle("Chat Login");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(250, 400);
        setLocationRelativeTo(null);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        //Username
        JLabel lblUsername = new JLabel("Username:");
        lblUsername.setBounds(89, 25, 71, 16);
        contentPane.add(lblUsername);

        txtName = new JTextField();
        txtName.setBounds(25, 45, 200, 28);
        contentPane.add(txtName);
        txtName.setColumns(10);

        //IP-Address
        lblIp = new JLabel("IP-Adresse:");
        lblIp.setBounds(84, 100, 81, 16);
        contentPane.add(lblIp);

        txtAddress = new JTextField();
        txtAddress.setBounds(25, 120, 200, 28);
        contentPane.add(txtAddress);
        txtAddress.setColumns(10);

        lblBspIp = new JLabel("(z.B. 127.0.0.1)");
        lblBspIp.setBounds(78, 150, 94, 16);
        contentPane.add(lblBspIp);

        //Port
        lblPort = new JLabel("Port:");
        lblPort.setBounds(106, 200, 38, 16);
        contentPane.add(lblPort);

        txtPort = new JTextField();
        txtPort.setBounds(25, 220, 200, 28);
        contentPane.add(txtPort);
        txtPort.setColumns(10);

        lblBspPort = new JLabel("(z.B. 55656)");
        lblBspPort.setBounds(84, 250, 81, 16);
        contentPane.add(lblBspPort);

        //Login-Button
        JButton btnLogin = new JButton("Login");
        btnLogin.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String name = txtName.getText();
                String address = txtAddress.getText();
                int port = Integer.parseInt(txtPort.getText());
                login(name, address, port);
            }
        });
        btnLogin.setBounds(66, 325, 117, 29);
        contentPane.add(btnLogin);

        this.getRootPane().setDefaultButton(btnLogin);
    }

    private void login(String user, String address, int port) {
        dispose();
        System.out.println("User: " + user + "\nIP-Adresse: " + address + "\nPort: " + port);
        new Client(user, address, port);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Login frame = new Login();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
}

最佳答案

好的,我现在在 Windows 上试过了,它工作得很好。 OS X 似乎有错误。

关于java - 线程 AWT-EventQueue-0 (File.<init>) 出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27527016/

相关文章:

cocoa - 使 OSX 应用程序在未获得焦点时响应第一次鼠标单击

css - 如何使用子字体

java - 如何确保 Neo4j 节点在同一事务内添加到索引中?

java - 使用哪个阵列?

java - 为什么在实现自定义身份验证提供程序时无法从其他类调用方法

macos - 恢复终端日志mac打印输出

macos - NSPopover + NSBox 的深色模式色调/ Gamma 问题

java - openjdk8源码缺少java_lang_String.h文件

java - Spring Batch CommandLineJobRunner 在由企业调度程序运行时挂起

objective-c - 手动让 NSPanel/NSWindow 退出键或不成为键