我制作了一个简单的地址簿应用。我有一个DatabaseUtility类,它有一个方法,connectToDatabase()负责从嵌入式数据库(Java DB)中提取信息并从中构建Contact对象。
然后将这些Contact对象放入ArrayList中,然后返回整个ArrayList。是的,我知道这是糟糕的编程。使用单独的连接和构造对象的方法更具逻辑性,但这是我为练习做的一个快速的小项目,我想我可以顺利进行,对吧?
无论如何,我还有一个ContactControl类,它包含一个DatabaseUtility类的实例作为它的一个字段,以及一个私有的ContactsList作为其中一个字段。
我想要的是ContactControl类中的ArrayList由connectToDatabase()方法的返回值实例化(正如我已经提到的那样,返回一个ArrayList)。
但是,我一直有例外。它没有连接到数据库。它在我运行我放在DatabaseUtility类中的main方法时连接,但是当我从ContactControl类运行main方法时,我得到一个异常。
我的代码如下:
联系班级:
package contactbook;
import java.io.IOException;
import java.io.ObjectStreamException;
import java.util.Date;
public class Contact {
private int contactId;
private String lastName;
private String firstName;
private String address;
private String city;
private String state;
private String zip;
private String picture;
private String dob;
public Contact()
{
contactId = 0;
lastName = "Doe";
firstName = "John";
dob = "01/01/1997";
address = "123 ABC Dr.";
city = "Pensacola";
state = "FL";
zip = "12345";
picture = "default1.gif";
}
public Contact(int contactId, String lastName, String firstName, String address, String city, String state, String zip, String picture, String dob)
{
this.contactId = contactId;
this.lastName = lastName;
this.firstName = firstName;
this.address = address;
this.city = city;
this.state = state;
this.zip = zip;
this.picture = picture;
this.dob = dob;
}
//setters
public void setContactId(int contactId)
{
}
public void setLastName(String lastName)
{
this.lastName = lastName;
}
public void setFirstName(String firstName)
{
this.firstName = firstName;
}
public void setAddress(String address)
{
this.address = address;
}
public void setCity(String city)
{
this.city = city;
}
public void setState(String state)
{
this.state = state;
}
public void setZip(String zip)
{
this.zip = zip;
}
public void setPicture(String picture)
{
this.picture = picture;
}
public void setDob(String dob)
{
this.dob = dob;
}
//getters
public int getContactId()
{
return contactId;
}
public String getLastName()
{
return lastName;
}
public String getFirstName()
{
return firstName;
}
public String getAddress()
{
return address;
}
public String getCity()
{
return city;
}
public String getState()
{
return state;
}
public String getZip()
{
return zip;
}
public String getPicture()
{
return picture;
}
public String getDob()
{
return dob;
}
}
DatabaseUtility类:
package contactbook;
import java.io.IOException;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.DatabaseMetaData;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.util.ArrayList;
import java.util.Properties;
public class DataBaseUtility {
public ArrayList<Contact> connectToDatabase() throws Exception {
ArrayList<Contact> contacts = new ArrayList<Contact>();
try
{
// Step 1: "Load" the JDBC driver
Class.forName("org.apache.derby.jdbc.ClientDriver");
// Step 2: Establish the connection to the database
String url = "jdbc:derby://localhost:1527/ContactBook";
Connection conn = DriverManager.getConnection(url,"app","app");
System.out.println("Connected!");
Statement stat = null;
stat = conn.createStatement();
ResultSet rs = stat.executeQuery("SELECT * FROM PERSON");
int id = 1;
while(rs.next())
{
Contact contact = new Contact();
contact.setContactId(id);
System.out.println(id);
String lastName = rs.getString("lastName");
System.out.println(lastName);
contact.setLastName(lastName);
String firstName = rs.getString("firstName");
System.out.println(firstName);
contact.setFirstName(firstName);
String address = rs.getString("address");
System.out.println(address);
contact.setAddress(address);
String city = rs.getString("city");
System.out.println(city);
contact.setCity(city);
String state = rs.getString("state");
System.out.println(state);
contact.setState(state);
String zip = rs.getString("zip");
System.out.println(zip);
contact.setZip(zip);
String picture = rs.getString("picture");
System.out.println(picture);
contact.setPicture(picture);
Date dob = rs.getDate("dob");
System.out.println(dob);
contact.setDob("" + dob);
contacts.add(contact);
System.out.println("");
contacts.add(contact);
id++;
}
}
catch (Exception e)
{
System.err.println("D'oh! Got an exception!");
System.err.println(e.getMessage());
}
return contacts;
}
public static void main(String[] args)
{
DataBaseUtility dbu = new DataBaseUtility();
try
{
dbu.connectToDatabase();
}
catch(Exception e)
{
e.getMessage();
}
}
}
ContactControl类:
package contactbook;
import java.util.ArrayList;
public class ContactControl {
private DataBaseUtility dbu;
private ArrayList<Contact> contacts;
public ArrayList<Contact> createContacts() throws Exception
{
try
{
contacts = dbu.connectToDatabase();
}
catch(Exception e)
{
System.out.println("Error!");
}
return contacts;
}
public Contact getContact(int id)
{
Contact tact = new Contact();
for(Contact c : contacts)
{
if(id == c.getContactId())
{
tact = c;
}
}
return tact;
}
public static void main(String[] args)
{
ContactControl cc = new ContactControl();
ArrayList<Contact> tacts = new ArrayList<>();
try
{
tacts = cc.createContacts();
}
catch(Exception e)
{
System.out.println("Uh oh! Problem!");
}
}
}
当我运行ContactControl类的main方法时,我得到了&#34;错误!&#34;您在try-catch块中看到的消息。
答案 0 :(得分:3)
我认为问题在于您在ContactControl
课程中调用了空对象。
contacts = dbu.connectToDatabase();
dbu未初始化,基本上它为null,因此为NullPointerException。由于您使用某些自定义消息隐藏了自己的实际异常消息,因此您无法看到它。