-
Relatively Simple Question
Hi guys,
I'm trying to write a java program that does, among other things, compare two arrays of booleans. The problem I run in to is that some of these boolean variables can be null (signifying yes, know, or unknown) but I'm not sure how to compare a variable to null. Using this code with IntelliJ gives me a syntax error:
Code:
else if(firstAttributes[x] == null || secondAttributes[x] == null)
{
distance = distance + 0.5;
}
It tells me that operator '==' cannot be applied to 'boolean', 'null.'
I distinctly remember being able to deal with this situation in some way, but I can't remember how. Any ideas?
-
I don't think a boolean can ever be null.
-
 Originally Posted by Phaelax
I don't think a boolean can ever be null.
You are right; boolean is a primitive and it cannot be null.
Happiness is good health and a bad memory.
-
I think I found a workaround for my problem...I havn't tried this out, but i think this code should do:
Code:
try
{
if(firstAttributes[x] != secondAttributes[x])
{
distance++;
}
}
catch(NullPointerException e)
{
distance = distance + 0.5;
}
I believe that this will do the trick, although I havn't tested it out...
-
 Originally Posted by thepsion5
I think I found a workaround for my problem...
I would call it the correct way of coding, not a workaround, although I usually do not approve of handling NullPointerException within the code.
In your case, I don't see a possible situation where a NPE would occur.
Happiness is good health and a bad memory.
-
Booleans are by default false, aren't they? As in, if you declare a boolean without initialising it true/false, its set to false? Or am I being a silly bunny?
-
You would have to initialize, otherwise you'd get a warning when you compile.
-
Not in an array.
Code:
boolean[] myArray = new boolean[10]; // array of ten booleans initialized to false
Same goes for instance and class variables.
Code:
public class JavaClass {
public static void main(String args[]) {
System.out.println(new MyClass().getMyBoolean); // prints false
}
}
class MyClass {
private boolean myBoolean;
public boolean getMyBoolean() { return myBoolean; }
}
Last edited by destin; 04-19-2006 at 07:09 AM.
Similar Threads
-
Replies: 2
Last Post: 03-06-2005, 09:14 AM
-
By Andrew Murphy in forum VB Classic
Replies: 1
Last Post: 09-03-2002, 01:17 AM
-
Replies: 0
Last Post: 11-04-2000, 02:57 AM
-
By Alex Nitulescu in forum VB Classic
Replies: 1
Last Post: 10-21-2000, 05:13 AM
-
By Gary McCallum in forum Java
Replies: 2
Last Post: 10-20-2000, 11:20 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks