Comparing NULL values in Java - java

I have a code that compares data but that data contains null values not blank spaces ("") they may/may not be same i guess but I tried comparing repetitive NULL values like
for(int i=0;i<length;i++)
{
String data=sample_data.getData();// contains about
1000 null rows
if(data.equals(null) || data.equals("") || data== null
||data.isEmpty())
System.out.print("No Data");
}
I could have used .contains but that wont work because it is in the loop.
None of the method makes it print the output as No Data I have tried using try catch as well because in order to make sure if theres any error found and was correct at the same time isEmpty() and .equals(null) were somewhere or the other throwing the exception as NULL but even after removing and simply using ==Null realizing the fact that data is itself null and comparing like null.equals wont work but in case of == I found that the error exception got changed to value as 1 thereafter the same error and I wasnt able to recognize this.
Its the sample here which I am showing I cant post the exact but the above makes quite clear. Is there any other way I could treat these NULL values.
Note: I dont want to use a loop to iterate data as well and each time comparing NULL values because already theres an outer loop for large set of values dont go by the sample code.

Restoring windows to factory settings will restore windows (as its name suggests); it won't spoil anything.

In this
String data=sample_data.getData();// contains about 1000 null rows
if(data.equals(null) || data.equals("") || data== null
you are apparently concerned that data may be null.
If indeed it is null, then the very first clause in your if statement will throw a null pointer exception... because you can't call the equals method on a null reference.
You want
if (data == null || data.isEmpty())
and that covers all the cases of nothing, except possibly the case where data is "some number of space characters", but I'll leave that for you.

Related

Comparing files with a specific structure in java

I have two txt files with a specific structure. There should be som empty rows and rows with some data. Something like this:
#RELATION Table
#RECORD 1
ID '5'
SOMETHING '10'
The point is, there can be 10 'empty' rows in one file and there can none in second and if the data equals, it should not matter. Any ideas how to effectively do it with big files ?
BufferedReader should be used to read from file, supplying a FileReader to its constructor:
https://docs.oracle.com/javase/7/docs/api/java/io/BufferedReader.html
So you will have two of them, one for each file.
Have an "infinite loop" such as: while(true){}
Within this container loop, you should have two internal loops, one for each file.
In each internal loop, using the method readLine() you should advance the cursor to the next line.
declare this outside of the container loop:
String lineFromFileA, lineFromFileB;
and then:
while((lineFromFileA = bufferedReaderA.readLine()) != null){
if(!lineFromFileA.isEmpty())
break;
}
do the same with lineFromLineB.
an alternative to the above loop is:
while((lineFromFileA = bufferedReaderA.readLine()) != null && lineFromFileA.isEmpty());
After the two internal loops, both lineFromFileA and lineFromFileB are either null or has a value that is not an empty String.
If both are null, then you are done with the comparison, the two files are equal and you may return true from the function.
If one contains null and the other does not, return false. The files are different.
If both are not null, then check the equals() method if the two Strings are identical, if they are not, return false from the function. If they are equal, do nothing, the next iteration of the container loop will handle the next line.

String tokenizer.nextToken() return value

When using an if statement, for instance,
if((isInt(search1.nextToken()) == true) && search1.nextToken() != "x")
would the result returned by search1.nextToken() have different values? This is all wrapped in a while loop as well, and I'm trying to figure out what would happen.
Yes, it would have different values. Whenever you do nextToken it would read the next available token. I would suggest to try with a simple java program to understand better.

How do you handle incomplete data in a Processing table?

I'm parsing a CSV using Processing's Table interface, but some rows are missing some data. I want to pull all the data available into my table, but I'm not sure how to handle the missing data--I keep getting NullPointerException when I loop over the table with dataTable.getInt on the missing values.
I don't have a background in statically typed languages, so I've no idea how to conditionally assign this data short of putting a separate try/catch around each assignment. Surely there's a better way?
Before calling dataTable.getInt method check if dataTable is not null like
if(dataTable != null) {
int my_nt = dataTable.getInt
}
//else skip since it is empty
Since your're using getInt--you should perform a regex search/replace ,<not numeric>, with ,<some int>,. In your case it may be as simple as replacing ,, with ,0,
Also, as Hassan suggests, double check that dataTable is not null.
Ok, so I figured out a way to do this:
First, call dataTable.makeNullEmpty(), which turns all the null values into empty strings.
Then, you can use a pattern like this:
String total_value = dataTable.getString(i, 4);
if(total_value.length() > 0) s.total_value = parseInt(total_value);
and you get assignment only if an int is there to be parsed.

charAt :error java.lang.NullPointerException

I am trying to use talend to check if the 4th character = 4 then I conversion S _ if not we keep the value
the input file is an Excel file
who can help me
row1.B.charAt(4) == '4'? StringHandling.CHANGE(StringHandling.LEFT(row1.B,9) ,"_","S"):row1.B
I have this error
[statistics] connected
Exception in component tMap_1
java.lang.NullPointerException
at projectname.test_0_1.test.tFileInputExcel_2Process(test.java:1140)
at projectname.test_0_1.test.runJobInTOS(test.java:1672)
at projectname.test_0_1.test.main(test.java:1540)
Either row1 or row1.B (use proper caps! attributes begin with lower case) are null
UPDATE: Regardint the comment to your question, then row1.B is null. Check for it and either control it in the condition ((row1.B != null) && (....)) or ((row1.B == null) || (...)) or (more probably) check your logic to assign a proper value to it.
A null pointer exception is caused when you dereference a variable that is pointing to null.
In you case either row1 or row1.B are null.
The cleanest way to do this is to write a user routine and then just call the function from tMap on the row input.
E.g. userFunction(row1.B)
Make the function output whatever string manipulation you need.
This also allows you to handle the case where the B cell in Excel is null. You can't do that in a one liner in tMap.

Strings don't seem to be equal in Java on Android, even though they print the same

I've got a problem that I'm rather confused about. I have the following lines of code in my android application:
System.out.println(CurrentNode.getNodeName().toString());
if (CurrentNode.getNodeName().toString() == "start") {
System.out.println("Yes it does!");
} else {
System.out.println("No it doesnt");
}
When I look at the output of the first println statement it shows up in LogCat as "start" (without the quotes obviously). But then when the if statement executes it goes to the else statement and prints "No it doesn't".
I wondered if the name of the node might have some kind of non-printing character in it, so I've checked the length of the string coming from getNodeName() and it is 5 characters long, as you would expect.
Has anyone got any idea what's going on here?
Use String's equals method to compare Strings. The == operator will just compare object references.
if ( CurrentNode.getNodeName().toString().equals("start") ) {
...
Use CurrentNode.getNodeName().toString().equals("start").
In Java, one of the most common mistakes newcomers meet is using == to compare Strings. You have to remember, == compares the object identity (Think memory addresses), not the content.
You need to use .equals
if ("start".equals(CurrentNode.getNodeName().toString()) { ... }

Categories

Resources