Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

can i assign null to Long variable

i have a pl/sql procedure and from java UI i need to send values .Somehow in the java side i have to manipulate the long value to be null when no value is passed.

5 Answers

Alexander Nikiforov
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Alexander Nikiforov
Java Web Development Techdegree Graduate 22,175 Points

It is perfectly normal for Long to be null, but If you are using primitive type long you cannot make it null. So you have to implement other logic. May be you have to set undecided long value to 0L, and prohibit normal chosen values to be not 0L

-------------------------------------below is a Database function---------------

func1 (Var1 Number, var2 String) IF var1 is null then some logic ; else some logic;

----------------------------------below is java function --------------- --java wrapper calls the above function and finally implemented in UI-----

Javafunct1 (long var1,String var2)

{
long VAR11 = FETCH THE VALUE FROM DATABASE;(WHICH IS NULL)
String VAR22 =FETCH THE VALUE FROM DATABASE;(WHICH IS NULL)

------SOME LOGIC------
IF (VAR11  != <value from db>)
    {
        Javafunct2{VAR11,VAR22};
    }
} 

The above function leads to null pointer exception ;

I need some solution so that i can manipulate in java so that the long based variable will satisfy the null condition(present in db).

Alexander Nikiforov
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Alexander Nikiforov
Java Web Development Techdegree Graduate 22,175 Points

It seems pretty abstract to me. If you can share your code on GitHub for example than it will be easier to help.

If you cannot share, then all I can say based on your code: to write like this:

javafunct1 (long var1, String var2) {
  Long VAR11 = "FETCH THE VALUE FROM DATABASE"; // (WHICH IS NULL, but can be because we put it to Long)
  String VAR22 = "FETCH THE VALUE FROM DATABASE"; // (WHICH IS NULL)

  long smallLongVar11;
  if (VAR11 == null) {
    smallLongVar11 = 0L;
  } else {
    smallLongVar11 = VAR11;
  }
  // ------SOME LOGIC------
  if (smallLongVar11  != "<value from db>") {
      Javafunct2(smallLongVar11 ,VAR22);
  }
  // here we can also do something if VAR11 == null

}

Thanx for your quick reply.Will revert with the code.

Thanks but i have tweaked the logic in Database only so no more handling in java is required.