0

再会!

我是使用 Java DB (Derby) 的新手。我想将它嵌入到我的应用程序中。我找到了这个网站 (http://java.sun.com/developer/technicalArticles/J2SE/Desktop/javadb/) 并按照说明进行操作。我复制了部分示例代码并进行了编辑以适合我的班级。

当我运行该类时,它返回此错误:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at java.util.Properties$LineReader.readLine(Properties.java:435)
at java.util.Properties.load0(Properties.java:354)
at java.util.Properties.load(Properties.java:342)
at recordbook.ui.RecordBookDAO.loadDBProperties(RecordBookDAO.java:40)
at recordbook.ui.RecordBookDAO.<init>(RecordBookDAO.java:28)
at recordbook.ui.RecordBook.<init>(RecordBook.java:34)
at recordbook.ui.RecordBookUI.<init>(RecordBookUI.java:23)
at recordbook.ui.RecordBookUI$3.run(RecordBookUI.java:214)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:226)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:647)
at java.awt.EventQueue.access$000(EventQueue.java:96)
at java.awt.EventQueue$1.run(EventQueue.java:608)
at java.awt.EventQueue$1.run(EventQueue.java:606)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:105)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:617)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:275)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:200)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:190)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:185)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:177)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:138)
BUILD SUCCESSFUL (total time: 3 seconds)

这是获取 NullPointerException 的类的一部分:

    private Properties dbProperties = null;
    private final String dbName;

    public RecordBookDAO(String recordBookName)  {
        this.dbName = recordBookName;
        setDBSystemDir();
        dbProperties = loadDBProperties();
        String driverName = dbProperties.getProperty("derby.driver"); 
        loadDatabaseDriver(driverName);
        createDatabase();
    }

    private Properties loadDBProperties() {
        InputStream dbPropInputStream = null;
        dbPropInputStream = RecordBookDAO.class.getResourceAsStream("Configuration.properties");
        dbProperties = new Properties();
        try
        {
            dbProperties.load(dbPropInputStream);
        }
        catch (IOException ex)
        {
            ex.printStackTrace();
        }
        return dbProperties;
    }

该课程的完整代码位于:http ://dl.dropbox.com/u/34926392/RecordBookDAO.java 非常感谢您的帮助!先感谢您。

4

1 回答 1

2

你得到 NULL 在

dbPropInputStream = RecordBookDAO.class.getResourceAsStream("Configuration.properties");

检查 configuration.properties 是否存在。

如果您的属性文件与您用来调用“getResourceAsStream("")”的类位于同一目录中,那么您只需要文件的名称,而不需要它的绝对路径。

于 2012-04-11T08:22:10.767 回答