-3

I am curious to know what difference does it make when I use

Scan scan = new Scan(); instead of Scan scan = null;

    public Scan getScanByColumn1(byte[] columnFamily,Map<String,String> columnMap){
        Scan scan = null;
        scan.addColumn(columnFamily, ForesightConstants.QUALIFIER_SOFTWARE_VERSION);
        scan.addColumn(columnFamily, ForesightConstants.QUALIFIER_CNUM);
        scan.addColumn(columnFamily, ForesightConstants.QUALIFIER_SID);
        scan.addColumn(columnFamily, ForesightConstants.QUALIFIER_HIERARCHY);
        return scan;
    }

public Scan getScanByColumn2(byte[] columnFamily,Map<String,String> columnMap){
        Scan scan = new Scan();
        scan.addColumn(columnFamily, ForesightConstants.QUALIFIER_SOFTWARE_VERSION);
        scan.addColumn(columnFamily, ForesightConstants.QUALIFIER_CNUM);
        scan.addColumn(columnFamily, ForesightConstants.QUALIFIER_SID);
        scan.addColumn(columnFamily, ForesightConstants.QUALIFIER_HIERARCHY);
        return scan;
    }

When I used Scan scan = null; It gives warning when i add first column to the scan object. And the warning is Null pointer access: The variable scan can only be null at this location.

So, someone please help me find out why am i getting this warning, My program is running fine with Second one but, just curious to find out the reason behind the warning.

Thanks in advance.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Yogesh Patel
  • 41
  • 1
  • 10

2 Answers2

0

Although it is basic java fundamentals, but I will try to explain it in simple way-

Scan scan = new Scan();

With above line, you are declaring a variable 'scan' of type 'Scan' and at the same time you are initializing it with the instance of 'Scan' class. So, now in memory, your 'scan' object exist and refer to some memory location. So, now you can call methods on 'scan' object and use its variables and methods.

In another statement -

Scan scan = null;

Here you declared a variable 'scan' and this scan variable is initialized with null. It means you haven't created instance of 'Scan' class. 'scan' variable does not refer to any valid memory location. Since this instance is not created so if you call any method on this 'scan' variable you will get NullPointerException

Vikas Sachdeva
  • 5,633
  • 2
  • 17
  • 26
0

The difference is that if you attempted to run the first version, your program would crash with a NullPointerException (aka NPE) at the first scan.addColumn(...) call.

When you call an instance method, you need an object to call the instance on. But when you initialize scan like this:

  Scan scan = null;

you are setting its value to a null reference ... which means "no instance". And when you then attempt to call the addColumn method on the null reference, you will get an NPE.

That is what the compiler error message is trying to tell you. The compiler can see that though your program is valid Java ... it will crash when you run it.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Since when does `null` mean _no instance_? `null` is `null`. Within the context of `Scan scan = null` it means `scan` does not contain an instance of `Scan` or anything else that does not reference to `null`. – dbf Nov 19 '16 at 14:26
  • @dbf - The JLS states: *"The reference values (often just references) are pointers to these objects, and a special null reference, which refers to no object."*. (JLS 4.3.1) In other words `null` means no object / no instance. – Stephen C Nov 19 '16 at 14:48
  • Seriously .. I guessed authors of Java books would get this right, guess I was wrong ;) – dbf Nov 19 '16 at 15:19
  • @dbf Says the guy who wrote *"it means `scan` does not contain an instance"* ;P. No variable *contains* an object. – Tom Nov 19 '16 at 15:23
  • @dbf - To be honest, I don't understand your quibble. Perhaps you don't get the distinction between the words "means" and "is"? – Stephen C Nov 19 '16 at 15:29