Sunday, May 22, 2011

lvalue required as left operand of assignment


"lvalue required as left operand of assignment"
This is how the C / C++ compiler would complain if we try to assign a value to some constant.
We assign values to variables using assignment operator(=) in C / C++. The assignment operator assigns the value of the right hand side argument to the left hand side variable. for example, in the following statements:

     int a,b;
b=20;
a=b;

The line #2 will assign the value 20 to the variable b. The line #3 will assign the value of b (20) to the variable a. This works fine as the values are being assigned to variables not to the constants. What will happen if we change the above code as follow and try to compile?

int a,b;
b=5;
20=b;
a=b;

The compile will prompt the error "lvalue required as left operand of assignment" for line #3. This is because the value of b (5) is being assigned to a constant (20). The constants can not change their value, so the expression in line #3 is invalid.
Two values are associated with the assignment operator : the lvalue and the rvalue. Lvalue is an expression which is present at the left hand side of the assignment operator and rvlaue is present at the right hand side. The error “Lavalue required” means that the expression present in the left hand side of the assignment operator should not be there as the value can not be assigned to it.

Here is another code snippet that will show another scenario where the error occurs:

#include <stdlib.h>
void main()
{
int arr[10];
int* ptr;
ptr=(int*) calloc(2,10);
arr=ptr; //lvalue required error will be prompted for this
}

Here at line #7 the value is being assigned to a constant pointer, so the error will occur. The line #7 is trying to make the pointer arr point to the location allocated by calloc in line #6. This  is invalid because the array name(arr) is a constant pointer.

Sunday, May 15, 2011

How to deal with ArrayIndexOutOfBoundsException



The name itself indicates that this exception is thrown if an attempt is made to access the array elements with an illegal index. Illegal indices are those which exceeds the size of array or which are negative. Here is an example to demonstrate the ArrayIndexOutOfBounds Exception.

class ArrayIndexOutOfBoundsExceptionDemo1
{
public static void main(String[] args)
{
int[] array=new int[]{1,2,3,4,5};
int i=0;

System.out.println(array[0]);
System.out.println(array[1]);
System.out.println(array[2]);
System.out.println(array[3]);
System.out.println(array[4]);
System.out.println(array[5]);
}
}

The above code will produce output like this:



1
2
3
4
5
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
        at ArrayIndexOutOfBoundsExceptionDemo1.main(ArrayIndexOutOfBoundsExcepti
onDemo1.java:13)

Press any key to continue...


This means everything was OK till the index of element which was being accessed was less than the size of array. As soon as the index becomes equal to or greater than the size of the array an exception is thrown. As the array index starts with 0, in the above code we can access elements till the index is between 0 and 4. As soon as we try to access the element with the index 5, an exception is thrown. This will also happen if we inset the below statement in the above code:


System.out.println(array[-1]);

This is because -1 is not a valid array index.

Whenever the 
ArrayIndexOutOfBoundsException is thrown, then get the stack trace and try to see where the constraints on array index are being violated. This will help you to troubleshoot the problem.

Saturday, May 14, 2011

NumberFormatException

NumberFormatException is often thrown while converting a string value to a numeric value. The exception is thrown when the string can not be converted to a valid number. For example the string "1234" can be converted to an int value as follows:

int num=Integer.parseInt("1234");

What about the string "12xy"? This can not be converted to a number, hence the NumberFormatException will be thrown while executing the below statement:

int num=Integer.parseInt("12xy");

In short the NumberFormatException is thrown to indicate that an attempt is made to convert a string to a number but the string is not in appropriate format.


Sunday, May 8, 2011

How to debug: java.lang.NullPointerException

JVM throws NullPointerException whenever it tries to execute an expression which ultimately evaluates to:
null.someMethod();
Here is a short code snippet that demonstrates a scenario:
class NullPointerExceptionDemo
{
                public static void main(String args[])
                {
                                String nullString = null;
                                String nonNullString="This is not a null string";                   
                                if(nullString.equals(nonNullString))
                                {
                                                System.out.println("This will not execute");
                                }
                               
                }
}

The above code will compile fine. The compiler won’t complain about the code as it is syntactically fine. While executing the code, JVM will throw the NullPointerException. The output will be something like this:


Exception in thread "main" java.lang.NullPointerException
        at NullPointerExceptionDemo.main(NullPointerExceptionDemo.java:7)
Press any key to continue...


This is because the expression :

nullString.equals(nonNullString)

evaluates to :

null.equals("This is not a null string")

While troublesooting this type of exceptions it is important to get the stack trace of the exception and then find where a null value is being assigned to the reference variable on which we are invoking some method. The stack trace of an exception can be obtained by calling “printStackTrace()” method from catch block. This is useful technique of troubleshooting NullPointerException when there is a mess of method calls, when it is very difficult to know where the null value is being assigned.

Here is an example code snippet that shows how to use the stack trace to debug the NullPointerException problem:



On execution the output will be like this:


java.lang.NullPointerException
        at NullPointerExceptionDemo1.method3(NullPointerExceptionDemo1.java:25)
        at NullPointerExceptionDemo1.method2(NullPointerExceptionDemo1.java:20)
        at NullPointerExceptionDemo1.method1(NullPointerExceptionDemo1.java:16)
        at NullPointerExceptionDemo1.main(NullPointerExceptionDemo1.java:8)
Press any key to continue...


This says that the exception was first thrown at line 25 from method3, where it was not handled so thrown up to the caller method : method2. In method2, the exception was not handled here. It was line 20 that caused exception for method2. Again it was thrown to method1 at line 16 and again up to main at line 8. In main method it was handled.

Thus the stack trace tells us the root where the exception was thrown and how it was thrown up and where it was handled