为什么addBirth方法返回null?

时间:2009-12-16 03:49:35

标签: java nullpointerexception

我有这个方法但是在运行时会抛出nullpointerexception,为什么?

我的方法:

public static boolean isAddBirth(String name, String family, String fatherName, String mName, String dOfBirth, String pOfBirth) {
    ResultSet rst;
    boolean bool = false;
    Statement stmt;
    try {
        stmt = conn.createStatement();



        rst = stmt.executeQuery("SELECT * FROM birthtable");


        while (rst.next()) {
            if (rst.getString(2).equals(name) && rst.getString(3).equals(family) && rst.getString(4).equals(fatherName) && rst.getString(5).equals(mName) && rst.getString(6).equals(dOfBirth) && rst.getString(7).equals(pOfBirth)) {
                bool = false;
            } else {
                bool = true;
            }
        }
    } catch (SQLException ex) {
        Logger.getLogger(Manager.class.getName()).log(Level.SEVERE, null, ex);
    }
    return bool;




}

堆栈跟踪:

java.lang.NullPointerException
    at database.Manager.isAddBirth(Manager.java:164)
    at AdminGUI.AddNewBornInformation.submit(AddNewBornInformation.java:356)
    at AdminGUI.AddNewBornInformation.setButtonActionPerformed(AddNewBornInformation.java:283)
    at AdminGUI.AddNewBornInformation.access$800(AddNewBornInformation.java:28)
    at AdminGUI.AddNewBornInformation$9.actionPerformed(AddNewBornInformation.java:140)
    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)

这些也在我班上:

Logger logger = Logger.getLogger(this.getClass().getName());
private static Connection conn = DBManager.getConnection();

4 个答案:

答案 0 :(得分:2)

首先,即使它正在运行,该代码也不会按照您的意愿执行。甚至没有关闭。

其次,如果我们知道异常是什么行,那将会有所帮助。但是,我们可以将其缩小到以下之一:

  1. conn为空。
  2. conn.createStatement();返回null。
  3. stmt.executeQuery()返回null。
  4. 其中一个rst.getString()返回null。
  5. 我相信你可以从那里弄明白。

答案 1 :(得分:2)

鉴于你没有提供第164号行,我会猜测它是:

if (rst.getString(2).equals(name) && rst.getString(3).equals(family) && rst.getString(4).equals(fatherName) && rst.getString(5).equals(mName) && rst.getString(6).equals(dOfBirth) && rst.getString(7).equals(pOfBirth)) 

首先,这条线让我想哭。

让我们解决它:

String a;
String b;
String c;
String d;
String e;
String f;

a = rst.getString(2);
b = rst.getString(3);
c = rst.getString(4);
d = rst.getString(5);
e = rst.getString(6);
f = rst.getString(7);

if (!(a.equals(name))
{
    bool = false;
}

if(!(b.equals(family))
{
    bool = false;
}

if(!(c.equals(fatherName))
{
    bool = false;
}

if(!(d.equals(mName))
{
    bool = false;
}

if(!(e.equals(dOfBirth))
{
    bool = false;
}

if(!(f.equals(pOfBirth))
{
    bool = false;
}

这至少会显示出有空指针的行(假设我的猜测是正确的)。

另外,a-e是可怕的名字......你应该选择比我更好的名字。

这里的真正解决方案是使用面向对象的编程,因为它是...让我们创建一个Person类:

public class Person
{
    private final String firstName;
    private final String lastName;
    private final String middleName; // guessing that is what mName is...
    private final String fathersName;
    private final String dateOfBirth;
    private final String placeOfBirth; // guessing that is what pOfBirth is...

    public Person(final String firstName,
                  final String lastName,
                  final String middleName,
                  final String fathersName,
                  final String dateOfBirth,
                  final String placeOfBirth)
    {
        if(firstName == null)
        {
            throw new IllegalArgumentException("firstName cannot be null");
        }

        if(lastName == null)
        {
            throw new IllegalArgumentException("lastName cannot be null");
        }

        ... etc for all of the other arguments ...

        // I would never do the this.fristName thing.. .I would name the parameter different than the instance vairable...
        this.firstName = firstName;
        this.lastName  = lastName;

        ... etc for all of the other arguments ... 
    }

    public boolean equals(final Object o)
    {
        final Person person;

        if(!(o instanceof Person))
        {
            return (false);
        }

        other = (Person)o;

        // the code you I put above + your code for checking if they are equal
    }

    public int hashCode()
    {
        // this is probably good enough
        return (firstName.hashCode() + lastName.hashCode());
    }
}

然后在你的方法中你会得到类似的代码:

rst = stmt.executeQuery("SELECT * FROM birthtable");

    while (rst.next()) 
    {
        final Person person;

        // I would use temp variables rather than passing in the result of getString directly...
        person = new Person(rst.getString(2),
                            rst.getString(3),
                            rst.getString(4),
                            rst.getString(5),
                            rst.getString(6),
                            rst.getString(7));

        // otherPerson would be passed into the method instead of the String you are passing now
        bool = person.equals(otherPerson);

        ... etc ...
    }

答案 2 :(得分:1)

我认为可能的原因是“getString”方法返回null 此方法的API文档表明它可以返回null。

String getString(int columnIndex) [...]

返回: 列值;如果值为SQL NULL,则返回的值为 null

以上API文档参考来自: ResultSet

答案 3 :(得分:0)

为了使您的程序不太容易出现NullPointerExceptions,您可以假设您的输入变量不为空(或确保在方法的开头),而颠倒了比较:

if (name.equals(rst.getString(1)) && ...

另一个我没有遇到任何问题的替代方法是使用commons-lang库中的ObjectUtils.equals(obj1,obj2)方法,只有当两个对象都为null或者obj1时它才会返回true。 .equals(obj2),其他所有可能性都会返回false。 ObjectUtils和StringUtils类有很多无效的方法,值得一看。