What is wrong with this line of java code? - java

if (current.leftChild.iData != '+' && current.rightChild.iData != '+')
For this line of code i am trying to check if the leftChild of the current node and the rightChild of the current node both == '+' Or character plus sign. If either of them do not = the character + i want to execute other code. I am getting a null pointer exception for this line and the method i use to call this line of code.
iData is just the information inside the node.

If you are getting a null pointer exception that means either the following cases.
1. current is null.
2. leftChild is null/ rightChild is null.
3. iData is null
But since you haven't provided a lot of details. I won't be able to help you.

Either current.leftChild or current.rightChild (or current itself) is not being set properly and is null. Trying to access the members of null results in a NullPointerException.

If current, current.leftChild or current.rightChild is null, you will get a NullPointerException.
You need to perform a null check first.

Use breakpoints to see the values of current.leftChild.iData and current.rightChild.iData.
Or you can write:
System.out.println("leftChild: " + current.leftChild.iData);
System.out.println("leftChild: " + current.rightChild.iData);
You will need to check the changes to these values because somewhere in your code it has set them to null. Once they are null, the system will give you the NullPointerException because those objects no longer exists.

First of all try printing each piece of that to find out what is null, you should get an NPE when you get to the piece that is causeing you trouble, this will seperate left and right into seperate failures. Note that if you make these seperate lines and both are failing you will only get the failure for the first one.
Now:
if (current.leftChild.iData != '+' && current.rightChild.iData != '+') For >this line of code i am trying to check if the leftChild of the current node and >the rightChild of the current node both == '+' Or character plus sign. If >either of them do not = the character + i want to execute other code
The code you wrote will not (once its not throwing exceptions) test what you say you want it to. You said you want to see if either is != to '+', but this code only tests if both are != '+', therefore you are missing some conditions which I believe that you inteded to catch. In fact if you really want to execute other code if either does not equal '+' then what you want is
if (current.leftChild.iData == '+' && current.rightChild.iData == '+')
{
//code you execute if both are equal to '+'
}
else
{
//code you execute if either (or both) is not equal to '+'
}

Related

Comparing NULL values in 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.

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.

Equivalent boolean statements have different results

I am confused about how && operator is working
if (StringUtils.isNotEmpty(cartModification.getStatusCode())
&& (cartModification.getStatusCode().equalsIgnoreCase(UNFCommerceCartModificationStatus.Sell_Out)))
Above statement is coming as true
while
if (StringUtils.isNotEmpty(cartModification.getStatusCode())
&& cartModification.getStatusCode().equalsIgnoreCase(UNFCommerceCartModificationStatus.Sell_Out))
is evluaated as false.Only difference between the 2 statements are braces.
As an additional input i have checked it with debugger and
StringUtils.isNotEmpty(cartModification.getStatusCode() =true
cartModification.getStatusCode().equalsIgnoreCase(UNFCommerceCartModificationStatus.Sell_Out)=true
I just added extra parenthesis in the second part and it was evaluated as true, Data is same as i have pointed out in question.
Since the commentbox isn't really approriate for this one:
How about trying this in a single run.
if(StringUtils.isNotEmpty(cartModification.getStatusCode())
&& cartModification.getStatusCode().equalsIgnoreCase(UNFCommerceCartModificationStatus.Sell_Out))
System.out.println("First try is true");
if(StringUtils.isNotEmpty(cartModification.getStatusCode())
&& (cartModification.getStatusCode().equalsIgnoreCase(UNFCommerceCartModificationStatus.Sell_Out)))
System.out.println("Second try is true");
No matter what, you should either get no or two lines (since both statements are the same).
Note it's possible that just one line is printed, this means that any call on getStatusCode() invokes a change on any value used in at least one of the conditions.

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