打印数组条目时出现空指针异常

时间:2015-07-30 05:05:58

标签: java arrays jdbc nullpointerexception

我创建了一个类:visited,它计算数据库中与用户ID相对应的条目数(将每个条目称为电子邮件)。我使用Test作为id,数据库中与此对应的条目数为2. 11120059函数的输出工作正常,但因为我从countMail函数返回一个数组并取它进入新数组,它显示我的空指针异常。请帮助我卡在中间。代码是:

getMail()

输出是:

package src.service;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import model.Email;
import model.User;

public class Test {

public int countMail(User user){

    try{

        Class.forName("com.mysql.jdbc.Driver").newInstance();

        // Get a connection to the database
        Connection myConn = DriverManager.getConnection("jdbc:mysql://localhost:3306/chillmaarodb", "root", "rsystems");

        PreparedStatement myStatement = myConn.prepareStatement("select * from complaints where RID=? ORDER BY date desc");


        myStatement.setString(1, user.getId());

        ResultSet rs = myStatement.executeQuery();

        int count=0;

        while(rs.next())
        {
        count++;
        }

        return count;
    }

    catch(Exception e){
        e.printStackTrace();
    }

    return 0;

}

public Email[] getMail(User user){

    try{

        Class.forName("com.mysql.jdbc.Driver").newInstance();

        // Get a connection to the database
        Connection myConn = DriverManager.getConnection("jdbc:mysql://localhost:3306/chillmaarodb", "root", "rsystems");

        PreparedStatement myStatement = myConn.prepareStatement("select * from complaints where RID=? ORDER BY date desc");


        myStatement.setString(1, user.getId());

        ResultSet rs = myStatement.executeQuery();

        Home home= new Home();

        int length = home.countMail(user);

        Email[] mail = new Email[length];

        int i=0;

        while(rs.next())
        {
        mail[i].setMessage((String) rs.getString(5));
        mail[i].setTitle((String) rs.getString(4)); 
        mail[i].setSender((String) rs.getString(2));
        mail[i].setReceiver((String) rs.getString(1));
        i++;
        }

        return mail;

        }
    catch (Exception e){

        e.printStackTrace();

    }

    Email[] dummyMail = new Email[1];

    return dummyMail;



}

public static void main(String[] args){

    Test test = new Test();

    User user = new User();

    user.setId("11120059");

    System.out.println(test.countMail(user));

    Email[] email = test.getMail(user);

    for (int i=0 ; i<test.countMail(user) ; i++){

        System.out.println(email[i].getSender());

    }


}

}

1 个答案:

答案 0 :(得分:6)

当您编写Email[] mail = new Email[length];时,您将创建一个包含空引用的给定长度的数组。您无法通过此命令自动为该阵列创建所有对象。添加mail[i] = new Email()声明:

    Email[] mail = new Email[length];

    int i=0;

    while(rs.next())
    {
    mail[i] = new Email(); // or use appropriate constructor parameters
    mail[i].setMessage((String) rs.getString(5));
    mail[i].setTitle((String) rs.getString(4)); 
    mail[i].setSender((String) rs.getString(2));
    mail[i].setReceiver((String) rs.getString(1));
    i++;
    }

另请注意,当您创建dummyMail数组时,它也是一个包含单null - 引用的数组,此处也不会创建Email对象。可能你需要:

Email[] dummyMail = new Email[] {new Email()};