Question

I have this method but in run-time it will be thrown nullpointerexception,why?

my method:

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;




}

stacktrace:

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)

ALSO these are in my class:

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

Solution

Given you didn't provide line number 164 I'll take a guess that it is:

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)) 

First off that line makes me want to cry.

Let's fix it:

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;
}

That will at least show you the line that has the null pointer on it (assuming my guess is correct).

Also, a-e are terrible names... you should pick better ones than I did.

The real solution here is to use Object Oriented programming as it is intended... let's make a Person class:

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());
    }
}

Then in your method you would have code something like:

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 ...
    }

OTHER TIPS

First of all, even if it were working, that code wouldn't do what you wanted it to. Not even close.

Second, it would help if we knew what line the exception was on. In lieu of that, though, we can narrow it down to one of the following:

  1. conn is null.
  2. conn.createStatement(); returns null.
  3. stmt.executeQuery() returns null.
  4. One of the rst.getString()s returns null.

I'm sure you can figure it out from there.

I think the likely cause is that the "getString" method is returning null.
The API documentation of this method indicates that it can return null.

String getString(int columnIndex) [...]

Returns: the column value; if the value is SQL NULL, the value returned is null

The above API doc reference is from: ResultSet

For your program to be less prone to NullPointerExceptions, you could assume that the your input variables are not null (or ensure that in the beginning of your method), and the invert the comparation:

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

The other alternative, which I don't have any problems with, is to use the method ObjectUtils.equals(obj1, obj2) from the commons-lang library, it will return true only if both objects are null or if obj1.equals(obj2), every other possibility will return false. The classes ObjectUtils and StringUtils have a lot of methods that are null-safe, it's worth taking a look.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top