Exception in thread main java.lang.NullPointerException while fetching value from database

StackOverflow https://stackoverflow.com/questions/21813084

  •  12-10-2022
  •  | 
  •  

Question

I just trying to run a problem java program on eclipse, but getting NullPointerException. My code is posted below.Please tell me what I did wrong.

Rect.java

package org.spring.model;

public class Rect {

    private int id;
    private String name;

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    public Rect(int rectId, String rectName)
    {
        setId(rectId);
        setName(rectName);
    }


}

JdbcDaoImplement.java

    package org.spring.dao;

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

    import org.spring.model.Rect;

public class JdbcDaoImplement {

    public Rect getRect(int rectId)
    {
        Connection conn=null;

        try
        {
            Class.forName("oracle.jdbc.OracleDriver");
            String jdbcUrl = "jdbc:oracle:thin:@localhost:1521:ORCL";
            conn = DriverManager.getConnection(jdbcUrl, "system", "abcd");
            PreparedStatement ps=conn.prepareStatement("SELECT * FROM RECTANGLE WHERE id=?");
            ps.setInt(1, rectId);

            Rect rect=null;
            ResultSet rs=ps.executeQuery();
            if(rs.next()==true)
            {
                rect=new Rect(rectId,rs.getString("name"));
            }
            rs.close();
            ps.close();
            return rect;
        }
        catch(Exception e)
        {
            throw new RuntimeException();
        }
        finally
        {
            try {
                conn.close();
            } catch (SQLException e) {
                //e.printStackTrace();

            }
        }
    }

}

And finally, JdbcDemo.java

package org.spring;

import org.spring.dao.JdbcDaoImplement;
import org.spring.model.Rect;

public class JdbcDemo {

    public static void main(String[] args) {
        Rect rect=new JdbcDaoImplement().getRect(1);
        System.out.println(rect.getName());
    }

}

And the stack trace is :

Exception in thread "main" java.lang.NullPointerException
    at org.spring.JdbcDemo.main(JdbcDemo.java:10)
Was it helpful?

Solution

very likely in your main method rect is null, and you called rect.getName(), then Bang! NPE.

because in your getRect(int rectId) method, you handled exception in the method, and then return a rect, if there is something going wrong or rs.next() is false, you will return a null rect object.

what you could consider to do is, either redesign the getRect() method, let it never return null, E.g if no record found, or exception occured, throw exception(s); or in your main method, check the rect, make sure it is not null.

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