Can not set int field to null value - java

I have this int column:
#Column(length = 4)
private int contract_owner_id;
I don't need to set always value for every table row. I get this exception when I make select query:
Can not set int field org.entity.contracts.contract_owner_id to null value
Is there some way to set numb setups when there is no data into the table column?

The primitive datatype int isn't nullable. You need to use the Wrapper class Integer in this case. So just replace the type.

use Integer instead of int
ex-:
private Integer contract_owner_id;

Could be because you are using primitive datatype, try to use Wrapper instead (Long, Integer, Double)

Related

Delete value for previously assigned int field [duplicate]

Can an int be null in Java?
For example:
int data = check(Node root);
if ( data == null ) {
// do something
} else {
// do something
}
My goal is to write a function which returns an int. Said int is stored in the height of a node, and if the node is not present, it will be null, and I'll need to check that.
I am doing this for homework but this specific part is not part of the homework, it just helps me get through what I am doing.
Thanks for the comments, but it seems very few people have actually read what's under the code, I was asking how else I can accomplish this goal; it was easy to figure out that it doesn't work.
int can't be null, but Integer can. You need to be careful when unboxing null Integers since this can cause a lot of confusion and head scratching!
e.g. this:
int a = object.getA(); // getA returns a null Integer
will give you a NullPointerException, despite object not being null!
To follow up on your question, if you want to indicate the absence of a value, I would investigate java.util.Optional<Integer>
No. Only object references can be null, not primitives.
A great way to find out:
public static void main(String args[]) {
int i = null;
}
Try to compile.
In Java, int is a primitive type and it is not considered an object. Only objects can have a null value. So the answer to your question is no, it can't be null. But it's not that simple, because there are objects that represent most primitive types.
The class Integer represents an int value, but it can hold a null value. Depending on your check method, you could be returning an int or an Integer.
This behavior is different from some more purely object oriented languages like Ruby, where even "primitive" things like ints are considered objects.
Along with all above answer i would like to add this point too.
For primitive types,we have fixed memory size i.e for int we have 4 bytes and char we have 2 bytes. And null is used only for objects because there memory size is not fixed.
So by default we have,
int a=0;
and not
int a=null;
Same with other primitive types and hence null is only used for objects and not for primitive types.
The code won't even compile. Only an fullworthy Object can be null, like Integer. Here's a basic example to show when you can test for null:
Integer data = check(Node root);
if ( data == null ) {
// do something
} else {
// do something
}
On the other hand, if check() is declared to return int, it can never be null and the whole if-else block is then superfluous.
int data = check(Node root);
// do something
Autoboxing problems doesn't apply here as well when check() is declared to return int. If it had returned Integer, then you may risk NullPointerException when assigning it to an int instead of Integer. Assigning it as an Integer and using the if-else block would then indeed have been mandatory.
To learn more about autoboxing, check this Sun guide.
instead of declaring as int i declare it as Integer i then we can do i=null;
Integer i;
i=null;
Integer object would be best. If you must use primitives you can use a value that does not exist in your use case. Negative height does not exist for people, so
public int getHeight(String name){
if(map.containsKey(name)){
return map.get(name);
}else{
return -1;
}
}
No, but int[] can be.
int[] hayhay = null; //: allowed (int[] is reference type)
int hayno = null; //: error (int is primitive type)
//: Message: incompatible types:
//: <null> cannot be converted to int
As #Glen mentioned in a comment, you basically have two ways around this:
use an "out of bound" value. For instance, if "data" can never be negative in normal use, return a negative value to indicate it's invalid.
Use an Integer. Just make sure the "check" method returns an Integer, and you assign it to an Integer not an int. Because if an "int" gets involved along the way, the automatic boxing and unboxing can cause problems.
Check for null in your check() method and return an invalid value such as -1 or zero if null. Then the check would be for that value rather than passing the null along. This would be a normal thing to do in old time 'C'.
Any Primitive data type like int,boolean, or float etc can't store the null(lateral),since java has provided Wrapper class for storing the same like int to Integer,boolean to Boolean.
Eg: Integer i=null;
An int is not null, it may be 0 if not initialized. If you want an integer to be able to be null, you need to use Integer instead of int . primitives don't have null value. default have for an int is 0.
Data Type / Default Value (for fields)
int ------------------ 0
long ---------------- 0L
float ---------------- 0.0f
double ------------- 0.0d
char --------------- '\u0000'
String --------------- null
boolean ------------ false
Since you ask for another way to accomplish your goal, I suggest you use a wrapper class:
new Integer(null);
I'm no expert, but I do believe that the null equivalent for an int is 0.
For example, if you make an int[], each slot contains 0 as opposed to null, unless you set it to something else.
In some situations, this may be of use.

How do I handle null value returned by a query for fields with no value in them and datatype as integer

I have a table to which I had to add to fields of datatype integer, recently.
I use hibernate and my database in PostgreSQL.
My query to get the object corresponding to the table now returns null for the fields that were recently added. I am trying to check if they are null and to set 0 if the value is null.
I am unable to find a way to do this....can anybody help....
The hibernate query returns a pojo corresponding to my table and I want to check if one of the attributes in that pojo is null, but that attribute is an integer, and that is my trouble.
If you want it 0 if null then you just can give this value in the declaration,
Integer a=0;
If you use annotation then use can try default value. like
#Column(name="col_name", columnDefinition=" default '0' ")
Another way can be the setter method, If the field is private and you have public setter method then check null in that setter
public void setA(Integer a)
{
if(a==null)
{
this.a=0;
}else this.a=a
}
I suggest using int in your POJO instead of Integer. This way null doesn't get into the database. If you have to use Integer, make sure you give it an initial value of 0, and if you have any setters, ensure you check that the incoming argument is not null.
So either do:
public class MyPOJO {
private int intVal = 0;
...
}
or:
public class MyPOJO {
private Integer intVal = 0;
...
public void setIntVal(Integer intVal) {
if(intVal != null) {
this.intVal = intVal;
}
}
...
}
I tried all the answers like setting nullable=true and columnDefinition=" default '0' " but nothing seemed to work. So I altered the default value of the column as 0, this resolved the issue.
NOTE: Please be careful about possible loss of information while modifying the default value.

Saving Integer value into database

How do I save an Integer type (not int) into Database?
Using plain JDBC, I'd use the following:
Integer myInteger = ...;
PreparedStatement ps = ...;
if (myInteger == null) {
ps.setNull(1, Types.INTEGER);
} else {
ps.setInt(1, myInteger); // will be autounboxed
}
I think just use Integer is OK.Or use intValue() to convert it.
http://docs.oracle.com/javase/7/docs/api/java/sql/PreparedStatement.html#setInt(int, int)
Depending on your database system (mysql, postgre, ...), the integer type will or won't exist in your database. It is then better to use java Integer functions to make an Integer from your database value, which will probably be int or even bigint, depending on what's needed.
As I said in my comment, something like Integer myinteger = new Integer(yourdatabasevalue) should work fine.

Java declaration confusion for datatypes

I have a doubt about null assigning to variable in Java. In my program I have assigned null to String variable as String str_variable = null;. For the learning purpose i assigned null integer variable as int int_variable = null; It shows error Add cast with Integer. So that rewrite the above int declaration as Integer int_variable = null;. This does not shows errors. I do not know the reason of these two kind of declaration.
Please the difference between to me.
String str_variable = null;
int int_variable = null; // error.
Integer int_variable1 = null; // no error.
String and Integer are both classes, in a way they are not native data types that is why it is always okay for you to set null as an initial value, however for int you must always initialize it with a number, one good way to find out their appropriate initialization value is to create variables outside your main(), example String var1; int var2; then use System.out.println(var1); System.out.println(var2); within the main()
to see what was placed as an initial value when you run the program.
int is a primitive, Integer is a class.
See http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
int is a primitive type, Integer is a wrapper class type extending Object class. Non-referencing objects can be null but primitives cannot. That's why you get an error message saying you need casting.
You can use a line like int num = (Integer) null;, this is how casting is done, however you will get NullPointerException when you try to use num anywhere in your code since a non-referencing(null) Integer object doesn't hold / wrap a primitive value.

Default value for int in text field

I want to change default value for int property to empty field not 0. How can I do it?
You need to use Integer class and not int for that.
An int will always be initialized to 0 It can't be null.

Categories