1

I have this program

private Recordset readExcelData()
{
    Recordset rs;
    try
    {
     //some logic here          
    }
    catch (Exception e) 
    {
        e.printStackTrace();
    }
    return rs;
}

I get this error that rs should be initialized. I understand the reason is because unless it is initialized there wont be any space allocated to rs.

I fix this by

Recordset rs=null;

My question is, how does this fix the issue that was there earlier? Even now, rs has no space allocated. Isn't it? Or, Am I missing something?

ItsMeGokul
  • 403
  • 1
  • 5
  • 16
  • 2
    *"unless it is initialized there wont be any space allocated"* Wrong. If you initialize to `null`, no object is allocated. --- Without initialization, it has no value, so what would `return rs` be returning? – Andreas Jan 24 '18 at 19:48
  • Your fix is most probably not the right one. If you have an exception, instead of swallowing it and returning null (and thus probably cause anther NullPointerException, because the caller expects to receive a Recordset), you should **throw** an exception (either the original one, or a new one), to signal the problem to the caller. Catch the exception only if you can really handle it. Usually, that's at the topmost level, which handles user interaction. – JB Nizet Jan 24 '18 at 19:51
  • You may want to read up on pointers and Java objects here [https://stackoverflow.com/questions/1750106/how-can-i-use-pointers-in-java](https://stackoverflow.com/questions/1750106/how-can-i-use-pointers-in-java) – R.Laney Jan 24 '18 at 19:53
  • This should help you - https://softwareengineering.stackexchange.com/questions/257857/what-is-the-difference-between-uninitialized-object-variable-and-object-variable – isaace Jan 24 '18 at 20:10

0 Answers0