DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 8 of 8
  1. #1
    Join Date
    Apr 2006
    Posts
    2

    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?

  2. #2
    Join Date
    Mar 2004
    Posts
    635
    I don't think a boolean can ever be null.

  3. #3
    Join Date
    Apr 2006
    Posts
    2
    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...

  4. #4
    Join Date
    Oct 2005
    Location
    New Jersey
    Posts
    255
    Quote 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.

  5. #5
    Join Date
    Oct 2005
    Location
    New Jersey
    Posts
    255
    Quote 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.

  6. #6
    Join Date
    Apr 2006
    Posts
    20
    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?

  7. #7
    Join Date
    Mar 2004
    Posts
    635
    You would have to initialize, otherwise you'd get a warning when you compile.

  8. #8
    Join Date
    Dec 2005
    Location
    New Jersey
    Posts
    290
    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

  1. Replies: 2
    Last Post: 03-06-2005, 09:14 AM
  2. Very Simple Question - I think
    By Andrew Murphy in forum VB Classic
    Replies: 1
    Last Post: 09-03-2002, 01:17 AM
  3. Simple Threading question
    By R in forum Java
    Replies: 0
    Last Post: 11-04-2000, 02:57 AM
  4. Simple Data Environment Question
    By Alex Nitulescu in forum VB Classic
    Replies: 1
    Last Post: 10-21-2000, 05:13 AM
  5. Simple Class Variables question
    By Gary McCallum in forum Java
    Replies: 2
    Last Post: 10-20-2000, 11:20 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


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


Sponsored Links