Hello, So I am working on this project and I am having an issue with passing references to an object between classes, in order to fill an array[7][24] of objects I have made(simulating the different hours of the week for a planner), information is put in on a GUI and then saved in the respective object types
This is my first post on a forum, and I just started programming this year, so please forgive me for the learning curve im sure exists regarding this whole interaction
code is as follows
Data organizer(to make the array to virtually represent the calendar)
The Actual Homework GUI, where data is put in (action listener in here is what is bugged)
Main program is here
and finally the GUI side of the calendar(which isn't as issue as far as i can tell, but is needed for testing)
Here is a representation of the homework class(for the object in memory)
the error message I am getting is as follows
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at HomeworkGUI.actionPerformed(HomeworkGUI.java:157)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
This same thing is replicated for each button, THE ABOVE IS ONLY FOR THE ADD HOMEWORK BUTTON, the rest have almost exactly the same structure just with different parameters
Thank you all in advance for any help and again I apologize for how foolishly simple im sure this solution is, as well as how sloppily this is written but hey we have to learn sometime, and to make an omelet u gotta crack a few eggs
/>
-Blake
This is my first post on a forum, and I just started programming this year, so please forgive me for the learning curve im sure exists regarding this whole interaction
code is as follows
Data organizer(to make the array to virtually represent the calendar)
import java.io.*; import java.util.ArrayList; import javax.swing.JOptionPane; public class OrganizeData { public Event Calendar[][] = new Event[7][24]; ArrayList<String> phoneB = new ArrayList<String>(); public void savePhoneBookData() throws IOException { // create stream objects for conversion FileOutputStream outStream = new FileOutputStream("PhoneBook"); ObjectOutputStream objectOutputFile; objectOutputFile = new ObjectOutputStream(outStream); { //write objects to file try{ for(int i=0; i<100;i++) { objectOutputFile.writeObject(phoneB.get(i)); } objectOutputFile.close(); } catch (IOException e) { System.out.println("Exception caught"); } } } public void saveCalendarData() throws IOException { // create stream objects for conversion FileOutputStream outStream = new FileOutputStream("schedule"); ObjectOutputStream objectOutputFile; objectOutputFile = new ObjectOutputStream(outStream); //write objects to file try{ for(int i=0; i<7;i++) { for(int j=0;j<24;j++) { objectOutputFile.writeObject(Calendar[i][j]); } } objectOutputFile.close(); } catch (IOException e) { System.out.println("error in saving file, try an resave"); } } public void readPhoneBookData() throws Exception { // Create the stream FileInputStream input = new FileInputStream("PhoneBook"); ObjectInputStream objectInputFile = new ObjectInputStream(input); // Read the serialized objects from the file. tried to change it as per your advice but couldn't get it to compile. for (int i = 0; i < phoneB.size(); i++) { phoneB.set(i, (String) objectInputFile.readObject()); } // Close the file. objectInputFile.close(); } public void readCalendarData() throws Exception { FileInputStream inputStream = new FileInputStream("schedule"); ObjectInputStream objectinput = new ObjectInputStream(inputStream); try{ for(int i=0; i<7;i++) { for(int j=0;j<24;j++) { Calendar[i][j]= (Event) objectinput.readObject(); } } objectinput.close(); } catch (FileNotFoundException e) { System.out.println("Cannot Find File"); } catch(IOException i) { JOptionPane.showMessageDialog(null, "IOE Exception Occurred during file reading"); } catch(ClassNotFoundException c) {JOptionPane.showMessageDialog(null, "Error reading saved contact list, please try again"); } } }
The Actual Homework GUI, where data is put in (action listener in here is what is bugged)
import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextField; public class HomeworkGUI extends JFrame implements ActionListener { // entry fields JTextField textDate = new JTextField(10); JTextField textTime = new JTextField(10); JTextField textLength = new JTextField(10); JTextField textDescription = new JTextField(40); JScrollPane scrollDescription = new JScrollPane(textDescription); JTextField textLocation = new JTextField(20); JScrollPane scrollLocation = new JScrollPane(textLocation); JTextField textDue = new JTextField(10); JTextField textPartners = new JTextField(40); JScrollPane scrollPartners = new JScrollPane(textPartners); JTextField textClass = new JTextField(10); JTextField textType = new JTextField(10); // save button to save and go back JButton save = new JButton("Save"); //pass Event[][] Calendar; Calendar CalendarMain; OrganizeData Organize; public HomeworkGUI() { // title text setTitle("Enter New Homework"); // set size setSize(1900,900); // let us put in a grid layout, because i like those setLayout(new GridLayout(2,5)); // ewww these attributes are GUI, hehehehe XD, panels first JPanel panelDate = new JPanel(); JPanel panelTime = new JPanel(); JPanel panelLength = new JPanel(); JPanel panelDescription = new JPanel(); JPanel panelLocation = new JPanel(); JPanel panelDue = new JPanel(); JPanel panelPartners = new JPanel(); JPanel panelClass = new JPanel(); JPanel panelType = new JPanel(); JPanel panelButton = new JPanel(); // label names JLabel labelDate = new JLabel("Date: "); JLabel labelTime = new JLabel("Time: "); JLabel labelLength = new JLabel("length in hours: "); JLabel labelDescription = new JLabel("Description: "); JLabel labelLocation = new JLabel("Location: "); JLabel labelDue = new JLabel("Due on: "); JLabel labelPartners = new JLabel("With: "); JLabel labelClass = new JLabel("Class: "); JLabel labelType = new JLabel("Type: "); // assemble GUI panelDate.add(labelDate); panelTime.add(labelTime); panelLength.add(labelLength); panelDescription.add(labelDescription); panelLocation.add(labelLocation); panelDue.add(labelDue); panelPartners.add(labelPartners); panelClass.add(labelClass); panelType.add(labelType); panelDate.add(textDate); panelTime.add(textTime); panelLength.add(textLength); panelDescription.add(scrollDescription); panelLocation.add(scrollLocation); panelDue.add(textDue); panelPartners.add(scrollPartners); panelClass.add(textClass); panelType.add(textType); panelButton.add(save); save.addActionListener((ActionListener) this); add(panelTime); add(panelDate); add(panelLength); add(panelLocation); add(panelDescription); add(panelDue); add(panelPartners); add(panelClass); add(panelType); add(panelButton); //set visibility setVisible(true); } public int getDate() { String dateInput; dateInput = textDate.getText(); int dateInt; dateInt = Integer.parseInt(dateInput); return dateInt; } public int getTime() { String timeInput; timeInput = textTime.getText(); int timeInt; timeInt = Integer.parseInt(timeInput); return timeInt; } public int getLength() { String lengthInput; lengthInput = textLength.getText(); int lengthInt; lengthInt = Integer.parseInt(lengthInput); return lengthInt; } public String getDescription() {return textDescription.getText(); } public String getLocal() {return textLocation.getText(); } public String getPartners() {return textPartners.getText(); } public String getDue() {return textDue.getText(); } public String getclass() {return textClass.getText(); } public String gettype() {return textType.getText(); } // ActionListener here will not work!!!!!! 5 lines down public void actionPerformed(ActionEvent e) {Homework hmwrk1 = new Homework(getDate(),getTime(),getLength(),true, getDescription(),getLocal(),getDue(),getPartners(),getclass(),gettype()); Organize.Calendar[getDate()][getTime()]=hmwrk1; CalendarMain.readObjects(); } }
Main program is here
import java.io.FileNotFoundException; import java.io.IOException; public class MainProgram { public static void main(String[] args) throws IOException, FileNotFoundException { OrganizeData Organize = new OrganizeData(); Calendar CalendarMain = new Calendar(); } }
and finally the GUI side of the calendar(which isn't as issue as far as i can tell, but is needed for testing)
import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.IOException; import javax.swing.*; public class Calendar extends JFrame implements ActionListener { // dont forget about actionlistener visibility JButton editPersonal = new JButton(" Personal Information "); JButton phoneBook = new JButton(" PhoneBook "); JButton appointment = new JButton(" Add Appointment "); JButton food = new JButton(" Add Meal "); JButton gym = new JButton(" Add Gym "); JButton schoolClass = new JButton(" Add Class "); JButton homework = new JButton(" Add Homework "); JButton schoolEvent = new JButton(" Add school event "); JButton work = new JButton (" Add Work Shift "); JButton save = new JButton(" Save Data "); JButton load = new JButton(" Load Data "); //so I can invoke save and load OrganizeData Organize; JLabel[][] CalLabel = new JLabel[7][24]; public void readObjects() { for(int i=0;i<7;i++) { for(int l=0;l<24;l++) { // label loop invoking toString method CalLabel[i][l].setText(Organize.Calendar[i][l].toString()); } } } public Calendar() { setTitle("Your Week"); // set size setSize(1900,900); //close op setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // let us put in a grid layout, because i like those setLayout(new GridLayout(1,8)); //panels JPanel panelSun = new JPanel(); JPanel panelMon = new JPanel(); JPanel panelTue = new JPanel(); JPanel panelWed = new JPanel(); JPanel panelThu = new JPanel(); JPanel panelFri = new JPanel(); JPanel panelSat = new JPanel(); JPanel panelButton = new JPanel(); //labels JLabel labelSun = new JLabel("Sunday"); JLabel labelMon = new JLabel("Monday"); JLabel labelTue = new JLabel("Tuesday"); JLabel labelWed = new JLabel("Wednesday"); JLabel labelThu = new JLabel("Thursday"); JLabel labelFri = new JLabel("Friday"); JLabel labelSat = new JLabel("Saturday"); // panels for week panelSun.add(labelSun); panelMon.add(labelMon); panelTue.add(labelTue); panelWed.add(labelWed); panelThu.add(labelThu); panelFri.add(labelFri); panelSat.add(labelSat); // add listeners editPersonal.addActionListener(this); phoneBook.addActionListener(this); appointment.addActionListener(this); food.addActionListener(this); gym.addActionListener(this); schoolClass.addActionListener(this); homework.addActionListener(this); schoolEvent.addActionListener(this); work.addActionListener(this); save.addActionListener(this); load.addActionListener(this); // button pain for navigation panelButton.add(editPersonal); panelButton.add(phoneBook); panelButton.add(appointment); panelButton.add(food); panelButton.add(gym); panelButton.add(schoolClass); panelButton.add(homework); panelButton.add(schoolEvent); panelButton.add(work); panelButton.add(save); panelButton.add(load); // panels to gridlayout add(panelSun); add(panelMon); add(panelTue); add(panelWed); add(panelThu); add(panelFri); add(panelSat); add(panelButton); setVisible(true); } OrganizeData organize = new OrganizeData(); public void actionPerformed(ActionEvent e) { Object src = e.getSource(); if(src == editPersonal) {PersonalInformationGUI inputpi = new PersonalInformationGUI();} else if (src == phoneBook) {PhoneBookGUI inputpb = new PhoneBookGUI();} else if(src == appointment) {AppointmentGUI inputapp = new AppointmentGUI();} else if(src== food) {FoodGUI inputfood = new FoodGUI();} else if(src== gym) {GymGUI inputgym = new GymGUI();} else if(src==schoolClass) {SchoolClassGUI inputsc = new SchoolClassGUI();} else if(src==homework) {HomeworkGUI inputhmwrk = new HomeworkGUI();} else if(src==schoolEvent) {SchoolEventGUI inputse = new SchoolEventGUI();} else if(src==work) {WorkGUI inputwrk = new WorkGUI();} else if(src==save) {try{Organize.saveCalendarData(); Organize.savePhoneBookData(); } catch (IOException p) { JOptionPane.showMessageDialog(null, "Error in saving operation"); }} else if(src==load) {try{Organize.readCalendarData(); Organize.readPhoneBookData(); } catch (Exception q) { JOptionPane.showMessageDialog(null, "Error Loading data, Start from Scratch or try again"); }} } }
Here is a representation of the homework class(for the object in memory)
public class Homework extends Event { int homeworkDate; int homeworkTime; int homeworkLength; String homeworkDescription; String homeworkLocation; String dueDate; String studyPartners; String homeworkClass; String homeworkType; // construction public Homework(int dateIn, int timeIn,int lengthIn, boolean stationaryIn, String descriptionIn, String locationIn, String dueDateIn, String studyPartnersIn, String classIn, String typeIn) {super(dateIn, timeIn,lengthIn, stationaryIn, descriptionIn); homeworkDate = super.date; homeworkTime = super.time; homeworkLength = super.length; homeworkDescription = super.description; homeworkLocation = locationIn; dueDate = dueDateIn; studyPartners = studyPartnersIn; homeworkClass = classIn; homeworkType = typeIn; } public String toString() { return homeworkType + " for class " + homeworkClass + " at "+ homeworkTime + " on " + homeworkDate + " with " + "due by" + dueDate; } public String toCal() { return "Homework for " + homeworkClass + "type: " + homeworkType + " due by: " + dueDate; } }
the error message I am getting is as follows
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at HomeworkGUI.actionPerformed(HomeworkGUI.java:157)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
This same thing is replicated for each button, THE ABOVE IS ONLY FOR THE ADD HOMEWORK BUTTON, the rest have almost exactly the same structure just with different parameters
Thank you all in advance for any help and again I apologize for how foolishly simple im sure this solution is, as well as how sloppily this is written but hey we have to learn sometime, and to make an omelet u gotta crack a few eggs

-Blake