Passing data from ArrayList in back end to JComboBox GUI front end - Java Swing -


this fileiomanagement class want handle of reading text files etc grabs data display in gui.

this code current fileiomanagement class:

package swinging;

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();          }      }    } 

this class need arraylist data passed to. can see attempting populate combobox1 , combobox2 data attempting via arraylist:

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 program details");     private jbutton viewprograms = new jbutton("view programs , majors associated course");      private jbutton viewtaughtcourses = new jbutton("view courses examiner teaches");      private jlabel courselabel = new jlabel("select course: ");     private jlabel examinerlabel = new jlabel("select 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);       }     } 

however when attempt compile, nullpointerexception in below image: enter image description here

what doing wrong here?

what did trying filename.getnamelist() filename never instantiated result return null.

problem:

fileiomanagement filename; //was not instantiated arraylist<string> names = new arraylist<string>(filename.getnamelist()); //filename.getnamelist() null arraylist<string> courses = new arraylist<string>(filename.getcourselist()); //filename.getcourselist() null 

solution:

instantiate fileiomanagement filename before getting list of it.


Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -