最近在学习Mysql和表格,今天在创建Object数组时犯了一个很低级的错误分享一下,讲一下我自己的理解,放代码 :
public class sql{
//声明Object二维数组
private Object data[][];
//获取表格
public Object[][] getTable() {
String querySql = "SELECT * FROM dormitory";
String querySql_count = "SELECT count(*)Id FROM dormitory";
try {
ResultSet result= state.executeQuery(querySql_count);
result.next();
int idCount = result.getInt("Id");
result= state.executeQuery(querySql);
int i=0;
/* 问题出现在这里这里先注释掉然后跑代码
* date = new Object[idCount][];
*/
while(result.next()) {
data[i] = new Object[2];
data[i][0] = result.getString("Id");
data[i][1] = result.getString("Dong");
i++;
}
result.close();
} catch (SQLException e) {
e.printStackTrace();
}
return data;
}
代码跑起来之后就抛出了空指针异常,一个低级的错误,让我找了好久。
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: Cannot store to object array because "this.date" is null
at com.izelin.dormitorysystem.sql.getTable(sql.java:81)
at com.izelin.dormitorysystem.system.<init>(system.java:53)
at com.izelin.dormitorysystem.Land$1.actionPerformed(Land.java:91)
at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1972)
at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2313)
at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405)
at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262)
at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:279)
at java.desktop/java.awt.Component.processMouseEvent(Component.java:6617)
at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3342)
at java.desktop/java.awt.Component.processEvent(Component.java:6382)
at java.desktop/java.awt.Container.processEvent(Container.java:2264)
at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:4993)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2322)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4825)
at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4934)
at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4563)
at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4504)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2308)
at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2773)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4825)
at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:772)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:95)
at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:745)
at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:743)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:742)
at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
我的理解是 在给指定数组元素个数之前没有指定数组的个数,数组未分配内存,所以在跑代码时出现了空指针异常。然后我加上了这一串代码,异常就不再出现了:
//idCount是我从数据库获取的一个长度
data = new Object[idCount][];
空指针异常不再出现,所以我总结出我创建数组的正确方法为
Object arr[][];
arr = new Object[i][];//i是正整数(数组个数)
arr[i] = new Object[j]; //j是正整数(元素个数)
这个数组的创建方法适用于二维数组。
在写代码时要细心,可能一个很低级的错误也会让你烦恼半天,也可能只有我找了大半天。
我是自学java语言,理解可能有误,以上所说的仅供参考。理解错误的地方请各位大神指出,谢谢!