我尝试在另一个类中创建公共静态列表并将它们设置为与私有类似,但是我得到了一个庞大的staptrace,告诉我我无法清除列表并且我无法设置列表。请帮忙
public void setLists() {
General.programmeStatsListT.clear();
General.programmeStatsListT.addAll(this.programmeStatsList);
General.fightersListT.clear();
General.fightersListT.addAll(this.fightersList);
General.userTableListT.clear();
General.userTableListT.addAll(this.userTableList);
General.userStatsListT.clear();
General.userStatsListT.addAll(this.userStatsList);
}
这是staptrace:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Database.Database_Frame.setLists(Database_Frame.java:379)
at Database.Database_Frame.<init>(Database_Frame.java:41)
at GUI.G_Start_Frame.<init>(G_Start_Frame.java:14)
at GUI.G_Start_Frame$4.run(G_Start_Frame.java:204)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:733)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:703)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
在我用这个类扩展javax.swing.JFrame然后让我的其他类扩展这个类
public class General extends javax.swing.JFrame {
////////////////////////////DATABASE CODE////////////////////////////////
public static EntityManager DBWORKS;
Database_Frame database;
//List used as imterdetiaries between private table lists and the rest of the programme
public static List<Database.Fighters> fightersListT;
public static List<Database.ProgrammeStats> programmeStatsListT;
public static List<Database.UserStats> userStatsListT;
public static List<Database.UserTable> userTableListT;
//Iterators for intermediatory lists
public static ListIterator<Database.Fighters> fightersT_Iterator;
public static ListIterator<Database.ProgrammeStats> programmeST_Iterator;
public static ListIterator<Database.UserStats> userST_Iterator;
public static ListIterator<Database.UserTable> userTT_Iterator;
///////////////////MAPS, ARRAYS, DATABASE INTERATION//////////////////////
protected Map<String, User> userT_TreeMap = new TreeMap<>();
protected ArrayList<String> userT_TreeTemp = new ArrayList<>();
protected boolean validateNewUser(Boolean valid, Boolean format, String username, String password, String name, String surname, String rights, String pic_Dir) {
if (name.isEmpty()) {
JOptionPane.showMessageDialog(null, "Please enter your name", "Missing Field", JOptionPane.ERROR_MESSAGE);
} else if (password.isEmpty()) {
JOptionPane.showMessageDialog(null, "Please enter your password", "Missing Field", JOptionPane.ERROR_MESSAGE);
} else if (surname.isEmpty()) {
JOptionPane.showMessageDialog(null, "Please enter your surname", "Missing Field", JOptionPane.ERROR_MESSAGE);
} else if (password.trim().length() < 8 || password.trim().length() > 30) {
JOptionPane.showMessageDialog(null, "Password must be between 8 and 30 characters long", "Input Error", JOptionPane.ERROR_MESSAGE);
G_Register_Frame.pwf_Password.setText(null);
A_Add_User.pwf_Password.setText(null);
} else if (name.trim().length() > 50) {
JOptionPane.showMessageDialog(null, "Name can only be 50 characters long", "Input Error", JOptionPane.ERROR_MESSAGE);
G_Register_Frame.txtf_Name.setText(null);
} else if (surname.trim().length() > 50) {
JOptionPane.showMessageDialog(null, "Surname can only be 50 characters long", "Input Error", JOptionPane.ERROR_MESSAGE);
G_Register_Frame.txtf_Surname.setText(null);
A_Add_User.txtf_Surname.setText(null);
} else {
for (int i = 0; i < name.length(); i++) {
if (!Character.isLetter(name.charAt(i))) {
format = false;
if (format == false) {
JOptionPane.showMessageDialog(null, "Name can only consist of letters", "Input Error", JOptionPane.ERROR_MESSAGE);
G_Register_Frame.txtf_Name.setText(null);
A_Add_User.txtf_Name.setText(null);
break;
}
}
}
for (int i = 0; i < surname.length(); i++) {
if (!Character.isLetter(surname.charAt(i))) {
format = false;
JOptionPane.showMessageDialog(null, "Surname can only consist of letters", "Input Error", JOptionPane.ERROR_MESSAGE);
G_Register_Frame.txtf_Surname.setText(null);
A_Add_User.txtf_Surname.setText(null);
break;
}
}
if (format == true) {
valid = true;
}
}
return valid;
}
protected void addUser(String username, String password, String name, String surname, String rights, String pic_Dir) {
userT_TreeMap.put(username, new User(username, password, name, surname, rights, pic_Dir));
}
protected void writeDB_UserTBL() {
for (String temp : userT_TreeTemp) {
try {
DBWORKS.find(UserTable.class, temp);
DBWORKS.getTransaction().begin();
DBWORKS.remove(temp);
DBWORKS.getTransaction().commit();
DBWORKS.clear();
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Database update failed.\nPlease report this error to the administrator", "ERROR " + e, JOptionPane.ERROR_MESSAGE);
}
}
for (Map.Entry<String, User> entry : userT_TreeMap.entrySet()) {
UserTable user = new UserTable();
user.setId(entry.getKey());
user.setFirstName(entry.getValue().getName());
user.setSurname(entry.getValue().getSurname());
user.setPassword(entry.getValue().getPassword());
user.setRights(entry.getValue().getRights());
user.setPicDir(entry.getValue().getPic_Dir());
DBWORKS.getTransaction().begin();
DBWORKS.persist(user);
DBWORKS.getTransaction().commit();
}
database.clearLists();
database.addAll();
//database.setLists();
database.getLists(programmeStatsListT, fightersListT, userStatsListT, userTableListT);
userTT_Iterator = userTableListT.listIterator(0);
database.setUserT(database.userT);
userT_TreeMap.clear();
database.addAll();
database.clearLists();
database.addAll();
fillUserT_TreeMap();
}
protected void fillUserT_TreeMap() {
for (UserTable temp : userTableListT) {
userT_TreeMap.put(temp.getId(), new User(temp.getId(), temp.getPassword(), temp.getFirstName(), temp.getSurname(), temp.getRights(), temp.getPicDir()));
}
userT_TreeTemp.clear();
for (Map.Entry<String, User> entry : userT_TreeMap.entrySet()) {
userT_TreeTemp.add(entry.getKey());
}
}
///////////////////////GENERAL METHODS////////////////////////////////////
protected void safeEnd(boolean end) {
System.exit(0);
}
protected void backToMain() {
new G_Main_Menu_Frame().setVisible(true);
this.setVisible(false);
}
protected void backToAdmin_Menu() {
new A_Admin_Menu_Frame().setVisible(true);
this.setVisible(false);
}
protected void backToUser_Menu() {
new U_User_Menu().setVisible(true);
this.setVisible(false);
}
protected void backToPredict() {
new U_Predict_Frame().setVisible(true);
this.setVisible(false);
}
protected void backToDef_Res() {
new A_Define_Frame().setVisible(true);
this.setVisible(false);
}
protected void goToDef_Frame() {
new A_Def_Res().setVisible(true);
this.setVisible(false);
}
protected void backToUser_Man() {
new A_User_Frame().setVisible(true);
this.setVisible(false);
}
protected void backToA_Stats() {
new A_Statistics_Frame().setVisible(true);
this.setVisible(false);
}
protected void printList() {
for(int i = 0; 0 < userTableListT.size(); i++) {
System.out.println("" + userStatsListT.get(i));
}
}
protected void setIterators() {
fightersT_Iterator = fightersListT.listIterator(0);
programmeST_Iterator = programmeStatsListT.listIterator(0);
userST_Iterator = userStatsListT.listIterator(0);
userTT_Iterator = userTableListT.listIterator(0);
}
}
请注意,我在程序中未使用的一些代码尚未完成
我的数据库类
public final class Database_Frame extends javax.swing.JFrame{
public Fighters fighters;
public ProgrammeStats programme;
public UserStats userS;
public UserTable userT;
public ListIterator<Database.Fighters> fightersListIterator;
public ListIterator<Database.ProgrammeStats> programmeListIterator;
public ListIterator<Database.UserStats> userStatsListIterator;
public ListIterator<Database.UserTable> userTableListIterator;
public Database_Frame() {
initComponents();
setEntityMan(General.DBWORKS);
setUserS(userS);
setUserT(userT);
setProgramme(programme);
setFighters(fighters);
fightersListIterator = fightersList.listIterator(0);
programmeListIterator = programmeStatsList.listIterator(0);
userTableListIterator = userTableList.listIterator(0);
userStatsListIterator = userStatsList.listIterator(0);
setLists();
//setLists(programmeStatsListT, fightersListT, userStatsListT, userTableListT);
public Fighters getFighters() {
return fighters;
}
public void setFighters(Fighters fighters) {
Fighters oldFighters = this.fighters;
this.fighters = fighters;
firePropertyChange("fighters", oldFighters, fighters);
}
public ProgrammeStats getProgramme() {
return programme;
}
public void setProgramme(ProgrammeStats programme) {
ProgrammeStats oldProgramme = this.programme;
this.programme = programme;
firePropertyChange("programme", oldProgramme, programme);
}
public UserStats getUserS() {
return userS;
}
public void setUserS(UserStats userS) {
UserStats oldUserStats = this.userS;
this.userS = userS;
firePropertyChange("userS", oldUserStats, userS);
}
public UserTable getUserT() {
return userT;
}
public void setUserT(UserTable userT) {
UserTable oldUserTable = this.userT;
this.userT = userT;
firePropertyChange("userT", oldUserTable, userT);
}
public void setLists() {
General.programmeStatsListT.clear();
General.programmeStatsListT.addAll(this.programmeStatsList);
General.fightersListT.clear();
General.fightersListT.addAll(this.fightersList);
General.userTableListT.clear();
General.userTableListT.addAll(this.userTableList);
General.userStatsListT.clear();
General.userStatsListT.addAll(this.userStatsList);
}
public void getLists(List programmeStatsList, List fightersList, List userStatsList, List userTableList) {
this.programmeStatsList = programmeStatsList;
this.fightersList = fightersList;
this.userStatsList = userStatsList;
this.userTableList = userTableList;
}
public void clearLists() {
programmeStatsList.clear();
fightersList.clear();
userStatsList.clear();
userTableList.clear();
}
public void addAll() {
programmeStatsList.addAll(programmeStatsQuery.getResultList());
fightersList.addAll(fightersQuery.getResultList());
userStatsList.addAll(userStatsQuery.getResultList());
userTableList.addAll(userTableQuery.getResultList());
}
public void setEntityMan(EntityManager eMan) {
eMan = FairyTail_DBPUEntityManager;
}
拿出无用的自动代码
答案 0 :(得分:0)
你的静态List
绝对没有被初始化。只要我在General
课程中看到,这些List
获得初始化的唯一机会就在database.getLists
内,除非直接从另一个类初始化,因为它们是公共成员。
请检查代码中您认为可以初始化列表的任何一点,并确保在调用setLists
之前执行这些代码段。
修改
看起来您的getLists
方法没有初始化静态列表。无论如何,你的方法名称有点误导,因为getLists
实际上在类实例中设置了列表,而setLists
实际上试图将列表的内容复制到另一个类的静态成员。
同样,我找不到您发布的代码中列表的任何初始化,例如
programmeStatsListT = new ...