线程“ AWT-EventQueue-0”中的异常java.lang.NullPointerException数据库应用程序时的解决方案

问题描述

ProjectDetails类:

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;

public class ProjectDetails extends JFrame {
    private JPanel contentPane;
    private JTextField pid;
    private JTextField compName;
    private JTextField X;
    private JTextField Y;
    private String radioResponse="";
    private String id="";
    private String comp="";
    private String x="";
    private String y="";
    private String r="";
    private DBManager db;
    private JTextArea remarks;
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokelater(new Runnable() {
            public void run() {
                try {
                    ProjectDetails frame = new ProjectDetails();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printstacktrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public ProjectDetails() {
        setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100,100,450,456);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5,5,5));
        setContentPane(contentPane);
        contentPane.setLayout(null);
        
        JLabel label = new JLabel("Project_Id");
        label.setFont(new Font("Tahoma",Font.PLAIN,12));
        label.setBounds(10,74,81,14);
        contentPane.add(label);
        
        pid = new JTextField();
        pid.setBounds(101,71,282,20);
        contentPane.add(pid);
        pid.setColumns(10);
        
        JLabel lblNewLabel_1 = new JLabel("Company Name");
        lblNewLabel_1.setFont(new Font("Tahoma",12));
        lblNewLabel_1.setBounds(10,113,91,14);
        contentPane.add(lblNewLabel_1);
        
        compName = new JTextField();
        compName.setBounds(101,111,20);
        contentPane.add(compName);
        compName.setColumns(10);
        
        JLabel lblNewLabel_2 = new JLabel("Project Details");
        lblNewLabel_2.setFont(new Font("Tahoma",Font.BOLD,16));
        lblNewLabel_2.setBounds(134,11,121,32);
        contentPane.add(lblNewLabel_2);
        
        JLabel lblNewLabel_3 = new JLabel("New label");
        lblNewLabel_3.setFont(new Font("Tahoma",12));
        lblNewLabel_3.setBounds(10,156,14);
        contentPane.add(lblNewLabel_3);
        
        X = new JTextField();
        X.setBounds(101,154,20);
        contentPane.add(X);
        X.setColumns(10);
        
        JLabel lblNewLabel_4 = new JLabel("New label");
        lblNewLabel_4.setFont(new Font("Tahoma",12));
        lblNewLabel_4.setBounds(10,204,14);
        contentPane.add(lblNewLabel_4);
        
        Y = new JTextField();
        Y.setBounds(101,202,20);
        contentPane.add(Y);
        Y.setColumns(10);
        
        
        
        JRadioButton YesButton = new JRadioButton("Yes");
        YesButton.setBounds(101,253,47,23);
        contentPane.add(YesButton);
        
        JRadioButton NoButton = new JRadioButton("No");
        NoButton.setBounds(231,23);
        contentPane.add(NoButton);
        
        ButtonGroup YesNo = new ButtonGroup();
        YesNo.add(YesButton);
        YesNo.add(NoButton);
        
        JLabel lblNewLabel_5 = new JLabel("Invoice Sent");
        lblNewLabel_5.setFont(new Font("Tahoma",12));
        lblNewLabel_5.setBounds(10,257,14);
        contentPane.add(lblNewLabel_5);
        
        JButton NextButton = new JButton("Next");
        NextButton.setEnabled(false);
        NextButton.setBounds(203,383,89,23);
        contentPane.add(NextButton);
        
        remarks = new JTextArea();
        remarks.setEditable(false);
        remarks.setEnabled(false);
        remarks.setBounds(101,301,61);
        contentPane.add(remarks);
        ActionListener listener = new ActionListener() {

            public void actionPerformed(ActionEvent event) {
                JRadioButton button = (JRadioButton) event.getSource();
         
                if (YesButton.isSelected()) {
                    radioResponse="Yes";
                    // option Yes is selected
                    NextButton.setEnabled(true);
                    remarks.setEditable(false);
                    remarks.setEnabled(false);
         
                } else if (NoButton.isSelected()) {
                    radioResponse="No";
                    // option No is selected
                    remarks.setEditable(true);
                    remarks.setEnabled(true);
                    NextButton.setEnabled(false);
         
                } 
                
            }
        };
        YesButton.addActionListener(listener);
        NoButton.addActionListener(listener);
        
        JLabel lblNewLabel_6 = new JLabel("Remarks");
        lblNewLabel_6.setFont(new Font("Tahoma",12));
        lblNewLabel_6.setLabelFor(remarks);
        lblNewLabel_6.setBounds(10,306,14);
        contentPane.add(lblNewLabel_6);
        
        JButton AddButton = new JButton("Add");
        AddButton.setBounds(59,23);
        contentPane.add(AddButton);
        
        AddButton.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                // Todo Auto-generated method stub
                id=pid.getText();
                comp=compName.getText();
                x=X.getText();
                y=Y.getText();
                r=remarks.getText();
                db.addProject(id,comp,x,y,radioResponse,r);
                
                    
            
            }
        });
        
    }
}

DBManager:

import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.sqlException;
import java.sql.Statement;

import javax.swing.JOptionPane;


public class DBManager {
    private String connectionString="jdbc:MysqL://localhost/techno_control_database";
    private Connection conn=null;
    private Statement st=null;
    private ResultSet rs=null;
    private int status;
    public DBManager() {
        
    }
    private void createConnection() {
        try {
            conn=DriverManager.getConnection(connectionString,"root","");
            st=conn.createStatement();
        } catch (sqlException e) {
            // Todo Auto-generated catch block
            e.printstacktrace();
        }
    }

    public void addProject(String id,String company,String x,String y,String invoice,String r) {
        createConnection();
        String query="Insert into project_details(Project_Id,CompanyName,X,Y,InvoiceSent,Remarks) VALUES ('"+id+"','"+company+"','"+x+"','"+y+"','"+invoice+"','"+r+"')";
        try {
            st.execute(query);
            JOptionPane.showMessageDialog(null,"Added Project :) ");
            st.close();
            conn.close();
        } catch (sqlException e) {
            // Todo Auto-generated catch block
            e.printstacktrace();
        }
    }
    

public static void main(String args[]) {
    DBManager test=new DBManager();
}
}

我要解决错误: 我似乎找不到问题: //////////////////////////////////////////////////// //////////////////////////////////////////////////// //////////////////////////////////////////////////// //////////////////////////////////////////////////// ///////////////////////////////////

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at ProjectDetails$3.actionPerformed(ProjectDetails.java:177)
    at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1967)
    at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2308)
    at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405)
    at java.desktop/javax.swing.DefaultButtonModel.setpressed(DefaultButtonModel.java:262)
    at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:279)
    at java.desktop/java.awt.Component.processMouseEvent(Component.java:6636)
    at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3342)
    at java.desktop/java.awt.Component.processEvent(Component.java:6401)
    at java.desktop/java.awt.Container.processEvent(Container.java:2263)
    at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:5012)
    at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2321)
    at java.desktop/java.awt.Component.dispatchEvent(Component.java:4844)
    at java.desktop/java.awt.Lightweightdispatcher.retargetMouseEvent(Container.java:4918)
    at java.desktop/java.awt.Lightweightdispatcher.processMouseEvent(Container.java:4547)
    at java.desktop/java.awt.Lightweightdispatcher.dispatchEvent(Container.java:4488)
    at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2307)
    at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2762)
    at java.desktop/java.awt.Component.dispatchEvent(Component.java:4844)
    at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:772)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:95)
    at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:745)
    at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:743)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
    at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:742)
    at java.desktop/java.awt.EventdispatchThread.pumpOneEventForFilters(EventdispatchThread.java:203)
    at java.desktop/java.awt.EventdispatchThread.pumpEventsForFilter(EventdispatchThread.java:124)
    at java.desktop/java.awt.EventdispatchThread.pumpEventsForHierarchy(EventdispatchThread.java:113)
    at java.desktop/java.awt.EventdispatchThread.pumpEvents(EventdispatchThread.java:109)
    at java.desktop/java.awt.EventdispatchThread.pumpEvents(EventdispatchThread.java:101)
    at java.desktop/java.awt.EventdispatchThread.run(EventdispatchThread.java:90)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)