java - Method getRowCount() return error when tring to sort Jtable -


i'm novice in java swing , please me this. have simple app receives data 2 jtextfileds (name , occupation) , introduce them in jtable after button pressed. data stored in list<person> person(string name, string occupation). app works fine till i'm tring sort table using tablerowsorter. i'll give tablepanel , persontablemodel code .

tablepanel class

import model.person;  public class tablepanel extends jpanel {      private jtable table;     private persontablemodel tablemodel;     private tablelistener tablelistener;     private rowsorter<persontablemodel> sorter;      public tablepanel(){         tablemodel = new persontablemodel();         table = new jtable(tablemodel);           //addsorter(); --- > comment see problem           setlayout(new borderlayout());         add(new jscrollpane(table),borderlayout.center);       }      private void addsorter() {         if (tablemodel.getrowcount() != 0){         sorter = new tablerowsorter<persontablemodel>(tablemodel);         table.setrowsorter(sorter);         refresh();         }     }      public void setdata(list<person> db){         tablemodel.setdata(db);     }      public void refresh(){         tablemodel.firetabledatachanged();     }      public void addtablelistener(tablelistener tablelistener) {         this.tablelistener = tablelistener;      } } 

persontablemodel class

public class persontablemodel extends abstracttablemodel {      private list<person> db = null;      private string[] colnames = { "id", "name", "occupation" };      public void setdata(list<person> db) {         this.db = db;     }      public string getcolumnname(int column) {         return colnames[column];     }      public int getrowcount() {         return db.size();     }      @override     public int getcolumncount() {         return 3;     }      @override     public object getvalueat(int rowindex, int columnindex) {         person person = db.get(rowindex);          switch (columnindex) {         case 0:             return person.getid();         case 1:             return person.getname();         case 2:             return person.getoccupation();         }         return null;     } } 

why receive nullpointerexception in method getrowcount() in persontablemodel if activate method addsorter() ?


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -