java - DAO loading data -
i'm trying use dao access data mysql database. want output data array, have java.lang.nullpointerexception. please me solve problem.
playerswindow.java
public static void main(string[] args) { eventqueue.invokelater(new runnable() { public void run() { try { playerswindow window = new playerswindow(); window.frame.setvisible(true); } catch (exception e) { e.printstacktrace(); } } }); } public playerswindow() throws instantiationexception, illegalaccessexception, classnotfoundexception, sqlexception { initialize(); } private void initialize() throws sqlexception, instantiationexception, illegalaccessexception, classnotfoundexception { idao dao = daofactory.getdaoinstance(typedao.mysql); player mas[] = dao.loadplayers(); //here gives nullpointerexception object[] headers = { "id", "name", "playing pos", "height", "weight", "nationality", "date birth" }; model = new defaulttablemodel(headers, 0); model.addrow(mas); playerstable = new jtable(model); }
mesqldao.java
public class mysqldao implements idao{ private static string get_players = "select * fbdb.players"; connection con = null; public mysqldao() throws instantiationexception, illegalaccessexception, classnotfoundexception, sqlexception { class.forname("com.mysql.jdbc.driver").newinstance(); string url = "jdbc:mysql://localhost/fbdb"; con = drivermanager.getconnection(url,"root","root"); } public player[] loadplayers() throws sqlexception{ player [] mas = null; preparedstatement st = con.preparestatement(get_players); resultset rs = st.executequery(); rs.last(); mas = new player[rs.getrow()]; rs.first(); int =0; while (rs.next()){ mas[i] = new player (rs.getint(1), rs.getstring(2), rs.getstring(3), rs.getint(4), rs.getint(5), rs.getstring(6), rs.getdate(7)); i++; } return mas; } }
daofactory.java
public class daofactory { private static idao dao = null; public static idao getdaoinstance(typedao type){ if(type == typedao.mysql){ if(dao == null){ dao = new mysqldao(); } else return dao; } return null; } }
idao.java
public interface idao { player[] loadplayers() throws sqlexception; public boolean saveplayer(player p) throws sqlexception; public boolean deleteplayer(player p) throws sqlexception; player editplayer(player p) throws sqlexception; }
typedao.java
public enum typedao { mysql; }
exceptions:
java.lang.nullpointerexception @ fbapp.playerswindow.initialize(playerswindow.java:119) @ fbapp.playerswindow.<init>(playerswindow.java:91) @ fbapp.playerswindow$1.run(playerswindow.java:70) @ java.awt.event.invocationevent.dispatch(unknown source) @ java.awt.eventqueue.dispatcheventimpl(unknown source) @ java.awt.eventqueue.access$400(unknown source) @ java.awt.eventqueue$3.run(unknown source) @ java.awt.eventqueue$3.run(unknown source) @ java.security.accesscontroller.doprivileged(native method) @ java.security.protectiondomain$1.dointersectionprivilege(unknown source) @ java.awt.eventqueue.dispatchevent(unknown source) @ java.awt.eventdispatchthread.pumponeeventforfilters(unknown source) @ java.awt.eventdispatchthread.pumpeventsforfilter(unknown source) @ java.awt.eventdispatchthread.pumpeventsforhierarchy(unknown source) @ java.awt.eventdispatchthread.pumpevents(unknown source) @ java.awt.eventdispatchthread.pumpevents(unknown source) @ java.awt.eventdispatchthread.run(unknown source)
the problem returning null in daofactory. here how fix it:
public class daofactory { private static idao dao = null; public static idao getdaoinstance(typedao type){ if(type == typedao.mysql){ if(dao == null){ dao = new mysqldao(); } } return dao; } }
edit: more compact form:
public class daofactory { private static idao dao = null; public static idao getdaoinstance(typedao type){ if((type == typedao.mysql)&&(dao == null))dao = new mysqldao(); return dao; } }
Comments
Post a Comment