Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Passing a Boolean flag from one class to another

Posted on 2004-08-24
16
208 Views
Last Modified: 2010-05-18
Hi Experts

I am a newbie to java, Simple question, how do i pass a boolean flag from one class to another...

I have a class Library

public void library()
    {
       open = true;
    }

why cant i say this in my other class "Customer"

public void borrowBook(String Title)
   
      private Library aLibrary;

      if (aLibrary.open == true)
        {....

If i do this i get boolean canot be derefrenced....



0
Comment
Question by:baz_can_fix_it
  • 8
  • 6
  • 2
16 Comments
 
LVL 92

Expert Comment

by:objects
ID: 11888658
how/where is the open variable defined?
0
 
LVL 1

Author Comment

by:baz_can_fix_it
ID: 11888714
Hi objects, i owe you 50 which i'll add to this question...

its in the Library class

public class Library {  
    private List aBook= new ArrayList();
    public boolean open = true;
.....

Sorry typo

that meant to be

public void libStatus() // not public void library()

    {
       open = true;
    }
Cheers

PS hope i didnt get you in trouble...

0
 
LVL 92

Expert Comment

by:objects
ID: 11888743
that all looks fine. can you post the complete code for both classes and I'll see what the problem is.

> PS hope i didnt get you in trouble...

don't worry about it :)
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 1

Author Comment

by:baz_can_fix_it
ID: 11888854
import java.util.*;

public class Customer
{
    private String cusName;
    private double amount;
    private Library aLibrary;

    public Customer (String name, double funds)
    {
        cusName = name;
        amount = funds;
       
    }
   
    public void buyBOOK (String title)
    {
        if (aLibrary.closed == false)
        {
           //do somestuff
        }
        else
        System.out.println("The Library is closed!");
    }
   
}


///////////////////////////////////////


public class Library {  
    private List aBook = new ArrayList();
    public boolean close = true;
    private String bookTitle;
    private String theLibraryName;
    private double till;
    private double total;
    private int count=0;
   

    public Library (String theLibraryName)
    {
        this.theLibraryName = theLibraryName;    
    }

    public void buyBooks(String newTitle, String ratings,double cost)
    {
            if (close == false)
            {
            till = cost;
            total = total + till;
            aBook.add(new BOOK(newTitle,ratings,cost));
        }
        else
        System.out.println("The Library is Closed, Please open Library first");
    }
   
     public void closeLibrary()
     {
         if (close == false)
            {
         double result = 0.0;
         result = total;
         close = true;
         System.out.println("Total takings for the day is:  $"+result);
            }
         else
         System.out.println("The Library is already Closed, Please open Library first");
         double result = 0.0;
         //return result;
     }


    public void openLibrary()
    {
        closed = false;
        double result = 0.0;
        total = 0;
    }

}



plz note i used close instead of my original open...

cheers
0
 
LVL 92

Expert Comment

by:objects
ID: 11888877
>     if (aLibrary.closed == false)

should be:

    if (aLibrary.close == false)
0
 
LVL 1

Author Comment

by:baz_can_fix_it
ID: 11888901
sorry that was a typo here it is actually all closed not close...i re typed it becuase if i want to paste the whole code is too much...

Cheers
0
 
LVL 1

Author Comment

by:baz_can_fix_it
ID: 11888958
I also get null point exception in here...

    public void buyBOOK (String title)
    {
        if (aLibrary.closed == false)
        {
           //do somestuff
        }
        else
        System.out.println("The Library is closed!");
    }
0
 
LVL 92

Accepted Solution

by:
objects earned 50 total points
ID: 11888972
aLibrary never gets set so it is null
0
 
LVL 19

Expert Comment

by:ramazanyich
ID: 11889654
your if statement should be
if(!aLibrary.closed){
...
}

0
 
LVL 1

Author Comment

by:baz_can_fix_it
ID: 11890910
Hi objects, what should i be doing...

ramazanich, I still get null point exception....

emmm i thought this was going to be simple....

Chhers
0
 
LVL 19

Assisted Solution

by:ramazanyich
ramazanyich earned 20 total points
ID: 11891286
you didn't initialize aLibary in your Customer class.
Add following line to constructor of Customer:

aLibrary = new Library("some name");
0
 
LVL 92

Expert Comment

by:objects
ID: 11898204
perhaps pass a library to you ctor:

    public Customer (Library lib, String name, double funds)
    {
        aLibrary = lib;
        cusName = name;
        amount = funds;
       
    }
0
 
LVL 1

Author Comment

by:baz_can_fix_it
ID: 11898312
Ok got it working...here is how

I placed this in my Library Class

public boolean IsClosed()
    {
      return closed;      
    }

now in my Customer Class I can say

if (!aShop.IsClosed())...

I'll give you guys some point for trying to help me anyway...thanx again

0
 
LVL 1

Author Comment

by:baz_can_fix_it
ID: 11898316
typo...if (!aLibrary.IsClosed())...

Cheers
0
 
LVL 1

Author Comment

by:baz_can_fix_it
ID: 11898328
Objects... here is the 50 i owe you...

ramazanyich...20 for you :)

Thank you both
0
 
LVL 92

Expert Comment

by:objects
ID: 11898372
Thanks mate :)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
custom annotations 9 42
jdbc error in jsp application 20 63
How do I remove an object from a 3 40
JavaFX TableView not displaying correctly 3 55
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question