0

您好,在创建动态 textArea 方法时,我返回了

Exception in thread "main" java.lang.NullPointerException
at WindowCreation.setContentsParams(WindowCreation.java:121)
at Main.main(Main.java:38)

我需要能够返回用户输入并将其发送到数据库中,但目前我只是试图获取单个 textField/textArea ID 以便能够从它们返回文本

WindowCreation 类如下

import javax.swing.*;
import javax.swing.event.DocumentListener;

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

public class WindowCreation {

private int mainHeight = 0;
private int mainWidth = 0;
private JButton button;
private ArrayList<JTextField> textField;
private ArrayList<JTextArea> textArea;
private JLabel label;
private JFrame frame;
private Toolkit tk = Toolkit.getDefaultToolkit();
private Dimension screenSize = tk.getScreenSize();
private JPanel jlabelEmpty = new JPanel();
private DefaultComboBoxModel<String> model;
private JComboBox<String> comboBox;

     public void setWindowParam(int width, int height, String name, int x,
        int y, boolean center) {
    frame = new JFrame(name);
    int screenHeight = screenSize.height;
    int screenWidth = screenSize.width;

    final int CENTER_X = ((screenWidth / 2) - (width / 2));
    final int CENTER_Y = ((screenHeight / 2) - (height / 2));

    frame.setResizable(false);
    if (center) {
        frame.setLocation(CENTER_X, CENTER_Y);
    } else {

        frame.setLocation(x, y);

    }
    frame.setSize(width, height);

    frame.addWindowListener(new WindowAdapter() {

        public void windowClosing(WindowEvent e) {
            System.exit(0);

        }
    });

    jlabelEmpty.setPreferredSize(new Dimension(width, height));
    jlabelEmpty.setOpaque(true);
    jlabelEmpty.setLayout(null);
    mainWidth = width;
    mainHeight = height;
}

public void setContentsParams(int width, int height, String x, String y,
        String content, String type,String[] choice) {
    boolean xIsNum;
    int wdthOffset;
    boolean yIsNum;
    int hightOffset;

    try {
        Integer.parseInt(x);
        xIsNum = true;
    } catch (Exception e) {
        xIsNum = false;
    }
    if (x.equals("center")) {
        wdthOffset = ((getWidth() / 2) - (width / 2));

    } else if (xIsNum) {

        wdthOffset = Integer.parseInt(x);

    }else{

        wdthOffset = 0;

    }

    try {
        Integer.parseInt(y);
        yIsNum = true;
    } catch (Exception e) {
        yIsNum = false;
    }
    if (y.equals("center")) {
        hightOffset = ((getHeight() / 2) - (height / 2));

    } else if (yIsNum) {

        hightOffset = Integer.parseInt(y);

    }else{

        hightOffset = 0;

    }
    switch (type) {
    case "button":
        button = new JButton(content);
        button.setSize(new Dimension(width, height));
        button.setLocation(wdthOffset, hightOffset);
        jlabelEmpty.add(button);
        button.setFont(new Font("name1", Font.BOLD, 20));
        ActionListener buttonListener = new ButtonListener();
        button.addActionListener(buttonListener);
        break;
    case "textfield":
        textField.add(new JTextField(content));
        textField.get(textField.size()-1).setSize(new Dimension    (width,height));
        textField.get(textField.size()-1).setLocation(wdthOffset, hightOffset);
        jlabelEmpty.add(textField.get(textField.size()-1));
        break;
    case "textarea":
        textArea.add(new JTextArea(content));
        textArea.get(textArea.size()-1).setSize(new Dimension(width,height));
        textArea.get(textArea.size()-1).setLocation(wdthOffset, hightOffset); 

        jlabelEmpty.add(textArea.get(textArea.size()-1));
        break;
    case "textdisplay":
        label = new JLabel(content);
        label.setSize(new Dimension(width,height));
        label.setLocation(wdthOffset, hightOffset);
        jlabelEmpty.add(label);
        break;
    case "dropmenu":
         model = new DefaultComboBoxModel<String>();
         int i = 0; 
         while (i < choice.length){

             model.addElement(choice[i]);
             i++;
         }
         comboBox = new JComboBox<String>(model);
         comboBox.setSize(new Dimension(width,height));
         comboBox.setLocation(wdthOffset,hightOffset);
         jlabelEmpty.add(comboBox);

    }
    frame.revalidate();
    frame.repaint();

}

public void displayInput(int compNum){

    System.out.println(textArea.get(compNum));

}

public void removePanel(int panel){

    jlabelEmpty.remove(panel);

}

public int getWidth() {

    return (mainWidth);

}

public int getHeight() {

    return (mainHeight);

}

public void DisplayWindow() {
    frame.getContentPane().add(jlabelEmpty, BorderLayout.CENTER);
    frame.pack();
    frame.setVisible(true);
}

}

以及调用它的方法

st = con.createStatement();
        ResultSet rs = st.executeQuery("select * from HR.employees");
        System.out.println("attempting to import Table...");
        window.removePanel(0);
        window.setContentsParams(200, 30, "center", "101", "First Name", "textarea",null);
        window.setContentsParams(200, 30, "center", "131", "Last Name", "textfield",null);
        window.setContentsParams(200, 30, "center", "161", "Email", "textfield",null);
        window.setContentsParams(200, 30, "center", "191", "PhoneNumber", "textfield",null);
        window.setContentsParams(200, 30, "center", "221", "Start Date", "textfield",null);
        String[] position = {"AD_PRES","AD_VP","AD_ASST","FI_MGR","FI_ACCOUNT","SA_MAN","SA_REP","PU_MAN","PU_MAN","PU_CLERK","ST_MAN","SH_CLERK","IT_PROG","MK_MAN","MK_REP","HR_REP","PR_REP"};
        window.setContentsParams(200, 30, "center", "251", "position", "dropmenu",position);
        window.setContentsParams(200, 30, "center", "281", "Salary (Yearly)", "textfield",null);
        String[] managerCodes = {"100","103","108","114","121","145","200","201","203","204","205"};
        window.setContentsParams(200, 30, "center", "311", "Manager Number", "dropmenu",managerCodes);
        String[] departmentCode = {"10","20","30","40","50","60","70","80","90","100","110","120","130","140","150","160","170","180","190","200","210","220","230","240","250","260","270"};
        window.setContentsParams(200, 30, "center", "341", "Department ID", "dropmenu",departmentCode);
        window.setContentsParams(100, 1000, "center", "541", "confirm", "button", null);
        while (rs.next()) {
            String outputTable = String
                    .format("%-6s | %-11s | %-11s | %-9s | %-18s | %-14s | %-10s | %-7s | %-4s | %-4s | %-4s ",
                            rs.getString("EMPLOYEE_ID"),
                            rs.getString("FIRST_NAME"),
                            rs.getString("LAST_NAME"),
                            rs.getString("EMAIL"),
                            rs.getString("PHONE_NUMBER"),
                            rs.getString("HIRE_DATE"),
                            rs.getString("JOB_ID"), rs.getString("SALARY"),
                            rs.getString("COMMISSION_PCT"),
                            rs.getString("MANAGER_ID"),
                            rs.getString("DEPARTMENT_ID"));
            System.out.println(outputTable);
        }

如果我不使用和数组来识别文本字段,它工作正常,但是我无法获取单个字段的输入

4

1 回答 1

1
private ArrayList<JTextArea> textArea;

您的 textArea 为空。代码应该是:

private ArrayList<JTextArea> textArea = new ArrayList<JTextArea>();

这将创建一个empty您现在可以操作的 ArrayList。

于 2013-11-07T15:49:24.177 回答