gpt4 book ai didi

java - 将数据从后端的 ArrayList 传递到 JComboBox GUI 前端 - Java Swing

转载 作者:行者123 更新时间:2023-12-01 12:52:43 24 4
gpt4 key购买 nike

这是我的 FileIOManagement 类,我想处理从文本文件等读取数据以在 GUI 中显示的所有读取操作。

这是我当前的 FileIOManagement 类的代码:

包裹 Swing ;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Scanner;

public class FileIOManagement {

private ArrayList<String> nameList = new ArrayList<String>();
private ArrayList<String> courseList = new ArrayList<String>();
private ArrayList<String> semesterList = new ArrayList<String>();
private ArrayList<String> moderatorList = new ArrayList<String>();
private ArrayList<String> programList = new ArrayList<String>();
private ArrayList<String> majorList = new ArrayList<String>();


public FileIOManagement(){
readTextFile();
}

private void readTextFile(){
try{
Scanner scan = new Scanner(new File("Course.txt"));

while(scan.hasNextLine()){
String line = scan.nextLine();
String[] tokens = line.split("~");
String course = tokens[0].trim();
String examiner = tokens[1].trim();
String moderator = tokens[2].trim();
String semester = tokens[3].trim();
String program = tokens[4].trim();
String major = tokens[5].trim();


courseList.add(course);
semesterList.add(semester);
nameList.add(examiner);
moderatorList.add(moderator);
programList.add(program);
majorList.add(major);
HashSet hs = new HashSet();
hs.addAll(nameList);
nameList.clear();
nameList.addAll(hs);
Collections.sort(nameList);

}
scan.close();
}
catch (FileNotFoundException e){
e.printStackTrace();
}
}



}

这是我需要将 ArrayList 数据传递到的类。正如您所看到的,我正在尝试使用我试图通过 ArrayList 获取的数据来填充comboBox1和comboBox2:

package swinging;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.border.EmptyBorder;


public class ReportGUI extends JFrame{
//Fields
private JButton viewAllReports = new JButton("View All Program Details");
private JButton viewPrograms = new JButton("View Programs and Majors Associated with this course");
private JButton viewTaughtCourses = new JButton("View Courses this Examiner Teaches");
private JLabel courseLabel = new JLabel("Select a Course: ");
private JLabel examinerLabel = new JLabel("Select an Examiner: ");
private JPanel panel = new JPanel(new GridLayout(6,2,4,4));
FileIOManagement fileName;
ArrayList<String> names = new ArrayList<String>(fileName.getNameList());
ArrayList<String> courses = new ArrayList<String>(fileName.getCourseList());








public ReportGUI(){
reportInterface();

allReportsBtn();
// fileRead();

comboBoxes();
}




private void reportInterface(){
setTitle("Choose Report Specifications");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel panel = new JPanel(new FlowLayout());
add(panel, BorderLayout.CENTER);
setSize(650,200);
setVisible(true);
setResizable(false);
setLocationRelativeTo(null);
}
private void allReportsBtn(){
JPanel panel = new JPanel(new GridLayout(1,1));
panel.setBorder(new EmptyBorder(70, 50, 70, 25));
panel.add(viewAllReports);
viewAllReports.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
new AllDataGUI();
}
});
add(panel, BorderLayout.LINE_END);
}
private void comboBoxes(){

panel.setBorder(new EmptyBorder(0, 5, 5, 10));
String[] comboBox1Array = names.toArray (new String[names.size()]);
JComboBox comboBox1 = new JComboBox(comboBox1Array);
panel.add(examinerLabel);
panel.add(comboBox1);
panel.add(viewTaughtCourses);
viewTaughtCourses.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new ViewCourseGUI();
}
});
String[] comboBox2Array = courses.toArray(new String[courses.size()]);
JComboBox comboBox2 = new JComboBox(comboBox2Array);
panel.add(courseLabel);
panel.add(comboBox2);
panel.add(viewPrograms);
viewPrograms.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new ViewProgramGUI();
}
});
add(panel, BorderLayout.LINE_START);

}

}

但是,当我尝试编译时,我得到一个 NullPointerException ,如下图所示: enter image description here

我在这里做错了什么?

最佳答案

您所做的是尝试获取 fileName.getNameList()哪里fileName从未被实例化,因此它将返回 null .

问题:

FileIOManagement fileName; //was not instantiated
ArrayList<String> names = new ArrayList<String>(fileName.getNameList()); //fileName.getNameList() is null
ArrayList<String> courses = new ArrayList<String>(fileName.getCourseList()); //fileName.getCourseList() is null

解决方案:

实例化您的FileIOManagement fileName在获取它的列表之前。

关于java - 将数据从后端的 ArrayList 传递到 JComboBox GUI 前端 - Java Swing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24101799/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com