I have created a class which name is Manager and I have a Frame which name is BirthList and this frame has a table.I work with MySQL and I have entered some data in the "birthtable" in MySQL.and i want to add those data from MySQL table in to the table which is in my frame .
HINT: birthList is a list of Birth objects.
but I will find this exception why?
Please help me:(
Manager class:
public Manager class{
Logger logger = Logger.getLogger(this.getClass().getName());
private static Connection conn = DBManager.getConnection();
private static Admin admin;
public static void addToBirthListFromMySQL() throws SQLException {
try{
Statement stmt = conn.createStatement();
ResultSet rs= stmt.executeQuery("SELECT * FROM birthtable");
Birth list1;
while (rs.next()) {
String s1 = rs.getString(2);
if (rs.wasNull()) {
s1 = null;
}
String s2 = rs.getString(3);
if (rs.wasNull()) {
s2 = null;
}
String s3 = rs.getString(4);
if (rs.wasNull()) {
s3 = null;
}
String s4 = rs.getString(5);
if (rs.wasNull()) {
s4 = null;
}
String s5 = rs.getString(6);
if (rs.wasNull()) {
s5 = null;
}
String s6 = rs.getString(7);
if (rs.wasNull()) {
s6 = null;
}
list1 = new Birth(s1, s2, s3, s4, s5, s6);
admin.birthList.add(list1);
}
}
catch(SQLException e){
}
}
My Frame:
public class BirthList extends javax.swing.JFrame {
private Admin admin;
/** Creates new form BirthList */
public BirthList(Admin admin) {
initComponents();
this.admin = admin;
try {
Manager.addToBirthListFromMySQL();
} catch (SQLException ex) {
Logger.getLogger(BirthList.class.getName()).log(Level.SEVERE, null, ex);
}
fillTable();
}
public void fillTable() {
String[] columNames = {"name", "family", "father's name", "mother's name", "date of birth", "place of birth"};
List<Birth> birth = admin.getBirthList();
Object[][] data = new Object[birth.size()][columNames.length];
for (int i = 0; i < data.length; i++) {
Birth birth1 = birth.get(i);
data[i][0] = birth1.getName();
data[i][1] = birth1.getFamily();
data[i][2] = birth1.getFatherName();
data[i][3] = birth1.getMotherName();
data[i][4] = birth1.getDateOfBirth();
data[i][5] = birth1.getPlaceOfBirth();
}
DefaultTableModel model = new DefaultTableModel(data, columNames);
jTable1.setModel(model);
}
public boolean isCellEditable(int row, int col) {
return true;
}
}
stacktrace:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at database.Manager.addToBirthListFromMySQL(Manager.java:272)
at AdminGUI.BirthList.(BirthList.java:35)
at AdminGUI.BirthFrame.newButton1ActionPerformed(BirthFrame.java:127)
at AdminGUI.BirthFrame.access$000(BirthFrame.java:21)
at AdminGUI.BirthFrame$1.actionPerformed(BirthFrame.java:58)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6038)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
at java.awt.Component.processEvent(Component.java:5803)
at java.awt.Container.processEvent(Container.java:2058)
at java.awt.Component.dispatchEventImpl(Component.java:4410)
at java.awt.Container.dispatchEventImpl(Container.java:2116)
at java.awt.Component.dispatchEvent(Component.java:4240)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)
at java.awt.Container.dispatchEventImpl(Container.java:2102)
at java.awt.Window.dispatchEventImpl(Window.java:2429)
at java.awt.Component.dispatchEvent(Component.java:4240)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)
line 272 is :admin.birthList.add(list1);
**I also debug my project and the result was :
debug:
Listening on javadebug
User program running
Debugger stopped on uncompilable source code.
*I also print my object admin with system.out.println(admin) and the result was:
classes.Admin@20be79*
Best Answer
Instead of hunting the root cause in the dark, I think it's better to explain how and why NPE's are caused and how they can be avoided, so that the OP can apply the newly gained knowledge to hunt his/her own trivial problem.
Look, object references (the variables) can contain either a fullworthy
Object
or simply nothing, which isnull
.Now, if you're trying to access nothing (
null
), then you will undoubtely get aNullPointerException
, simply becausenull
doesn't have any variables or methods.Workarounding this is fairly simple. It can be done in basically two ways: either by instantiating it or just by ignoring it.
or
In case of a NPE, the first line number of the stacktrace points the exact line where the it is been caused. You told literally "line 272 is
admin.birthList.add(list1);
". This line contains two places where object references are been accessed/invoked (using the dot.
operator). The first beingadmin.birthList
and the second beingbirthList.add(list1)
. It's up to you to find out if one or both caused the NPE. If it is the first invocation, thenadmin
is simplynull
. If it is the second invocation, thenbirthList
is simplynull
. You can fix it by instantiating it with a fullworthy object.Edit: If you have a hard time in determining the root cause (as turns out from comments), then you need to learn debugging. Run a debugger or just do "poor man's debugging" with help of a
System.out.println()
of every variable before accessing/invoking them. First look at line where the NPE is caused. If this is for examplethen you need to change it as follows to nail down the root cause:
check if any of them prints
null
. Alternatively you can also do:you can also separate the individual invocations over separate lines so that you have enough to the line number to know which reference is null.