NullPointerException in java (javafx) -
this main.java
public class main extends application { radiobutton chksort; radiobutton chkorder; public static void main(string[] args) { launch(args); } public static comparator<student> studentidcomparator = new comparator<student>() { public int compare(student std1, student std2) { string id1 = std1.getid(); string id2 = std2.getid(); return id1.comparetoignorecase(id2); } }; public static comparator<student> studentnamecomparator = new comparator<student>() { public int compare(student std1, student std2) { string name1 = std1.getname(); string name2 = std2.getname(); return name1.comparetoignorecase(name2); } }; public static comparator<student> studentlastnamecomparator = new comparator<student>() { public int compare(student std1, student std2) { string last1 = std1.getlastname(); string last2 = std2.getlastname(); //ascending order return last1.comparetoignorecase(last2); } //return fruitname2.compareto(fruitname1); //descending order }; public static comparator<student> studentgpacomparator = new comparator<student>() { public int compare(student std1, student std2) { string gpa1 = std1.getgpa(); string gpa2 = std2.getgpa(); return gpa1.compareto(gpa2); } }; @override public void start(stage primarystage) throws exception{ primarystage.settitle("student sorting"); splitpane splitpane = new splitpane(); gridpane grid1 = new gridpane(); gridpane grid2 = new gridpane(); grid1.setalignment(pos.top_left); grid1.setvgap(10); grid1.sethgap(10); grid1.setpadding(new insets(25,25,25,25)); grid2.setpadding(new insets(25,25,25,25)); scene scene = new scene(splitpane, 700, 500); primarystage.setscene(scene); text title = new text("student sorting"); title.setfont(font.font("comic sans ms", fontweight.bold, 20)); text sortby = new text("sort :"); sortby.setfont(font.font("calibri", fontweight.bold, 14)); final togglegroup toselect = new togglegroup(); final togglegroup toorder = new togglegroup(); radiobutton idbtn = new radiobutton("id"); idbtn.settogglegroup(toselect); radiobutton namebtn = new radiobutton("name"); namebtn.settogglegroup(toselect); radiobutton lastnamebtn = new radiobutton("last name"); lastnamebtn.settogglegroup(toselect); radiobutton gpabtn = new radiobutton("gpa"); gpabtn.settogglegroup(toselect); text order = new text("order :"); order.setfont(font.font("calibri", fontweight.bold, 14)); radiobutton ascbtn = new radiobutton("ascending"); ascbtn.settogglegroup(toorder); ascbtn.setuserdata("asc"); radiobutton desbtn = new radiobutton("descending"); desbtn.settogglegroup(toorder); desbtn.setuserdata("des"); button start = new button("sort"); start.setprefsize(170, 20); final textarea text = new textarea(); text.setminsize(500, 400); grid1.add(title, 0, 0); grid1.add(sortby, 0, 2); grid1.add(idbtn, 0, 3); grid1.add(namebtn, 0, 4); grid1.add(lastnamebtn, 0, 5); grid1.add(gpabtn, 0, 6); grid1.add(order, 0, 8); grid1.add(ascbtn, 0, 9); grid1.add(desbtn, 0, 10); grid1.add(start, 0, 13); grid2.add(text, 2, 2); splitpane.getitems().addall(grid1, grid2); splitpane.setdividerpositions(0.3); toselect.selectedtoggleproperty().addlistener(new changelistener<toggle> (){ @override public void changed(observablevalue<? extends toggle> value, toggle t1, toggle t2) { chksort = (radiobutton) t2.gettogglegroup().getselectedtoggle(); } }); toorder.selectedtoggleproperty().addlistener(new changelistener<toggle> (){ @override public void changed(observablevalue<? extends toggle> value, toggle t1, toggle t2) { chkorder = (radiobutton) t2.gettogglegroup().getselectedtoggle(); } }); start.setonaction(new eventhandler<actionevent>(){ @override public void handle(actionevent arg0) { string temp =text.gettext().tostring(); string[] splitline = temp.split("\n"); student[] stddata = new student[splitline.length]; for(int i=0; i<splitline.length; i++){ string[] stddatastr = splitline[i].split(" "); if(stddatastr[i].length() <4 || stddatastr[i].equals(null)){ text.settext("invalid input"); break; } else { stddata[i].setid(stddatastr[0]); stddata[i].setname(stddatastr[1]); stddata[i].setlastname(stddatastr[2]); stddata[i].setgpa(stddatastr[3]); } } if(chksort.gettext().tostring().equals("id")){ if(chkorder.gettext().tostring().equals("ascending")){ student.sort(stddata,studentidcomparator , true); } else{ student.sort(stddata, studentidcomparator, false); } } else if(chksort.gettext().tostring().equals("name")){ if(chkorder.gettext().tostring().equals("ascending")){ student.sort(stddata, studentnamecomparator, true); } else{ student.sort(stddata, studentnamecomparator, false); } } else if(chksort.gettext().tostring().equals("last name")){ if(chkorder.gettext().tostring().equals("ascending")){ student.sort(stddata, studentlastnamecomparator, true); } else{ student.sort(stddata, studentlastnamecomparator, false); } } else if(chksort.gettext().tostring().equals("gpa")){ if(chkorder.gettext().tostring().equals("ascending")){ student.sort(stddata, studentgpacomparator, true); } else{ student.sort(stddata, studentgpacomparator, false); } } for(int i=0 ; i<stddata.length; i++){ text.settext(stddata[i].getid()+" "+stddata[i].getname()+" "+stddata[i].getlastname()+" "+stddata[i].getgpa()); } } }); primarystage.show(); } }
this student.java
public class student { private string stdid; private string stdname; private string stdlastname; private string stdgpa; public student(string id, string name, string lastname, string gpa) { super(); this.stdid = id; this.stdname = name; this.stdlastname = lastname; this.stdgpa = gpa; } public string getid() {return stdid;} public void setid(string id) { this.stdid =id; } public string getname() {return stdname;} public void setname(string name) { this.stdname= name; } public string getlastname() {return stdlastname;} public void setlastname(string lastname) { this.stdlastname = lastname; } public string getgpa() {return stdgpa;} public void setgpa(string gpa) { this.stdgpa = gpa; } public static void sort(student[] std, comparator<student> c, boolean asc){ student tmp; (int i=0; i<std.length-1;i++){ for(int j=i+1;j<std.length;j++) if((c.compare(std[i],std[j]) >0 && asc==true) ||(c.compare(std[i],std[j]) <0 && asc==false)) { tmp = std[i]; std[i] = std[j]; std[j] = tmp; } } } }
this nullpointerexception appear
else { stddata[i].setid(stddatastr[0]); stddata[i].setname(stddatastr[1]); stddata[i].setlastname(stddatastr[2]); stddata[i].setgpa(stddatastr[3]); }
this java src when try run nullpointerexception when try setid, setname, setlastname , setgpa should do?
thanks all
you're initializing stddata
array of students don't create new student in each entry , try access student in entry doesn't exist.
what should is, after lines:
student[] stddata = new student[splitline.length]; for(int i=0; i<splitline.length; i++){
add initialization:
stddata[i] = new student();
and continue - later in code you'll able stuff like:
stddata[i].setid(stddatastr[0]);
tip:
when post question - try post relevant code, add stacktrace , mark line triggers compilation/run-time error.
Comments
Post a Comment