Sqlite Integer null == java int 0 [duplicate] - java

This question already has answers here:
Checking for a null int value from a Java ResultSet
(13 answers)
Closed 6 years ago.
I guess that an integer null value within a sqlite table is not equivalent to 0 in Java. So how can I check whether the int I extract from my table with
if(resultSet.getInt(columnNumber) != 0){
System.out.println("int is not null");
}
is not null? I do not want to use the sqlite constraint "IS NOT NULL".

It all depends what this method is doing: resultSet.getInt(...)
if getInt is returning a primitive integer, then that can not be null, but if that is an object of the class Integer then you need to check the nullabilty by doing a normal check like: resultSet.getInt(...)!= null and then after been sure that is ot null check if is not zero:
resultSet.getInt(...)!=0
Example:
if(resultSet.getInt(columnNumber) != null){
if(resultSet.getInt(columnNumber) != 0){
System.out.println("int is not zero");
} else {
System.out.println("int is zero");
}
}else{
System.out.println("int is null");
}

Related

Why am I getting a NullPointerException for this loop? [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 3 years ago.
I'm writing a for loop to iterate through the array and find the index of the employee with the social security number of 123456789. Why do I keep getting a NullPointerException?
.getSocSecNum() returns a String
empnew[21] = Empthe1;
String temp = "123456789"
for(int i = 0; i < empnew.length - 1; i++){
if(empnew[i].getSocSecNum().compareTo(temp) == 0){
System.out.println("the location of the employee is " + i);
}
}
I want it to output "the location of the employee is 21" but all I get is a NullPointerException
The problem is most likely that empnew has at least one null in it.
So in the loop, you try to get empnew[i].getSocSecNum(), which expands to null.getSocSecNum(), which gives you a null pointer exception, because you can't access class members on null, because it isn't a real object.
The easiest solution might be to just check that empnew[i] != null before accessing the value:
if (empnew[i] != null && empnew[i].getSocSecNum...) {
...
}

Why abstract variable null does not equal to null [duplicate]

This question already has answers here:
String concatenation and comparison gives unexpected result in println statement
(5 answers)
Closed 7 years ago.
I've programmed for quite a time, but I never got to this. I deploy on JRE 1.8.0_66.
I wrote an abstract class called Grenade. Then I made these statements in class Profile:
public class Profile {
Grenade varGrenade; // Field
public void check() {
varGrenade = null; // set reference to null
System.out.println("Am i null: " + this.varGrenade == null);
}
}
This statement returns
Am i null: false
If I would want to print the result of varGreanade, it would print null. Where have I gone wrong? (Don't know if it has something with abstract Grenade class) How do I check it for null without throwing NullPointerException?
It's just an issue of parentheses. What you want is
System.out.println("Am i null: " + (this.varGrenade == null));
What you're getting is
System.out.println(("Am i null: " + this.varGrenade) == null);
"Am i null: " + this.varGrenade is not null.
You need parentheses to force it to concatenate to the null-check.
(just like 1 + 2 == 3 doesn't mean 1 + (2 == 3))

JCombobox and String.equals(null) [duplicate]

This question already has answers here:
How to check if my string is equal to null?
(28 answers)
Closed 8 years ago.
Here is my code :
if (ChoixPortCom.equals(null) == true ) JOptionPane.showMessageDialog(null, "Choose Port COM");
and I get the famous java.lang.NullPointerException
My JCombobox is filled like :
1st -> nothin/empty null no String nothing
2nd -> COM1
3nd -> COM2
....
Why is "if" condition not right ?
choixPortCom.equals(null) will never be true. If choixPortCom is not null, then the expression will return false as expected. If choixPortCom is null, then the expression will throw a NullPointerException, since you are attempting to call a method on null; this is what's happening in your case. The appropriate way to check for null is:
if (choixPortCom == null) // I've assumed a more common naming convention
There is also an Objects class in Java 7 that has some useful methods for null-checking. For example, Objects.requireNonNull():
Objects.requireNonNull(choixPortCom, "input can't be null!")
It should be
if (ChoixPortCom == null)
Now if ChoixPortCom is null it will throw a NullPointer because you are trying to invoke a method (equals) on a null reference.
And something I like to think of as a best practice is to always use brackets:
if (ChoixPortCom == null) {
JOptionPane.showMessageDialog(null, "Choose Port COM");
}

Error when check an empty element in array [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
why cannot I check if a place in array is empty ?
I got this wrong message:
The operator == is undefined for the argument type(s) int, null"
on the marked line
private static int findNr(int[] trans)
{
int emptyPlace=0;
for (int i=0; i<trans.Length -1;i++)
{
--> if( trans[i] = null) <--
return emptyPlace = trans[i];
}
return emptyPlace;
}
You can not compare primitive data type for null. int is primitive data type.
You have to do
if( trans[i] == null)
instead of
if( trans[i] = null)
^-----------Mistake
Anyway in which language you have written the code?
You try to assign null to the array element at position i
To check if the element is null you have to do:
if(trans[i] == null)
{
...
}
but this expression doesn't make any sense since an int can never be null (unless its a nullable int (int?)) so the condition will always be false
Primitive cannot be checked with the null. So for particular primitive types use their default value to check.
i.e for int default value is 0 and for
double default value is 0.0 etc.
So check as:
if(intr[i]==0){
//some logic
}
if( trans[i] = null)
In the above line your are using assignment operator = in place of comparison operator ==. If condition expects the final value after computation to be of the type boolean. Hence you get the error.
Also your trans[i] is an primitive int value which cannot be compared to a null. (Only objects can be null in java)
The assignment operator = will only work in case of boolean variables, something like this:
boolean flag = false;
if(flag=true) {
// this condition will be true
}
First, you mispelled the == comparison. Should be:
if (trans[i] == null)
Second, the above doesn't works. Primitive values can never be null, when you declare any int variable, it's 0 by default.
Thats why:
int[] v = new int[3];
for (int i = 0; i < v.length; i++)
System.out.println(v[i]);
Outputs this:
0
0
0
You should either work with Integer objects array or review your logic.
1- You have forgot '=' ---> if (trans[i] == null)
2- You can't compare primitive value (int) with null

Obtaining index from the Joptionpane [duplicate]

This question already has answers here:
How to select an index value from a String Array in a JOptionPane
(2 answers)
Closed 8 years ago.
I want to access the index of the option selected by the user.Eg, in the figure below,if i choose microsoft option then it should give me the index 1.Is this possible?
Well you get "Microsoft" (well the Object showing Microsoft at least) as the return value from the show call, is that good enough?
If you need the index just find the index of that return value in the input array you provided to the dialog.
See the input section of the java tutorial:
http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html#input
Assuming you are using showInputDialog(..):
Object[] possibilities = {"Broadcom...", "Microsoft"};
Object result = JOptionPane.showInputDialog( frame, "Capture Interfaces", "Input", JOptionPane.PLAIN_MESSAGE, icon, possibilities, possibilities[0]);
if (result != null) {
//result is the choosen object, if you need the index even so:
int index = 0;
for (Object o : possibilities) {
if (result == o)
break;
index++
}
//index is now the index...
}

Categories

Resources