Results 1 to 18 of 18
  1. #1
    jamesreno is offline Member
    Join Date
    Apr 2014
    Posts
    12
    Rep Power
    0

    Default Code compiles but get null pointer exeption

    package assignment2;

    public class AirLock {

    private boolean lockState = false;

    public AirLock(boolean newLock){
    lockState = newLock;
    }

    public boolean isLockState() {
    return lockState;
    }

    public void setLockState(boolean lockState) {
    this.lockState = lockState;
    }
    __________________________________________________ __________________________________________________ ___

    public class AirlockDoor implements Runnable{

    private String name;
    private AirLock airlock;
    private String doorName;

    public AirlockDoor(String name, AirLock airLock){
    this.doorName = name;

    }

    public AirlockDoor() {
    // TODO Auto-generated constructor stub
    }


    public void run() {
    while(true){
    synchronized(airlock){
    System.out.println("" + name + "Open");
    System.out.println("" + name + "Closed");
    }
    }
    }

    public synchronized void requestToOpen() {

    System.out.println(doorName + " requests to be opened");



    }

    }
    __________________________________________________ __________________________________________________ _________________

    public class AstronautSimulator {

    public static void main(String [] args){
    simulate();
    }

    private static void simulate() {
    // Create an AirLock object
    // STEP 1: CREATE AN EMPTY VERSION OF THE AirlockDoor CLASS
    AirlockDoor[] doors = new AirlockDoor[2];

    // For this prototype this can be an empty (no method)
    // class. It is a common object used in the Door class
    // that contains the single lock-key that protects the
    // imaginary airlock room from having two doors open at
    // the same time
    // STEP 2: CREATE THIS CLASS
    AirLock MainAirLock = new AirLock(false);

    // Give the doors a name (for debugging purposes) and
    // a common airlock object used to store the "key" used
    // by a critical section of code in the Door class


    doors[0] = new AirlockDoor("Door 1", MainAirLock);
    doors[1] = new AirlockDoor("Door 2", MainAirLock);

    // AirlockDoor implements Runnable and so make sure
    // we tie the objects to Thread objects

    Runnable threadJob = new AirlockDoor();

    // STEP 3: CREATE TWO THREADS FOR THE TWO DOORS AND START THEM
    // ENTER CODE HERE

    Thread doorController1 = new Thread(threadJob);

    doorController1.setName("DoorLock");

    doorController1.start();

    // Loop infinitely trying to open airlock doors

    while (true){
    // random() returns a value in range 0.0..<1.0 multiplied by 10 and mod doors.length
    // to return either 0 or 1 in order to randomly decide which door to request
    int doorNum = (int)((Math.random() * 10) % doors.length);

    // STEP 4: MAKE A REQUEST TO OPEN THE DOOR AND THEN ADD A DELAY BEFORE
    // TRYING TO OPEN ANOTHER DOOR
    // ENTER CODE HERE

    doors[doorNum].requestToOpen();


    try{
    Thread.sleep(800);
    }
    catch(InterruptedException ex){
    ex.printStackTrace();
    }



    }
    }
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,038
    Rep Power
    6

    Default Re: Code compiles but get null pointer exeption

    airlock in AirLockDoor is never initialized.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    jamesreno is offline Member
    Join Date
    Apr 2014
    Posts
    12
    Rep Power
    0

    Default Re: Code compiles but get null pointer exeption

    how do i initilise airlock

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,038
    Rep Power
    6

    Default Re: Code compiles but get null pointer exeption

    You have a constructor that intializes it when you create an instance of AirLockDoor(). Curious question though. Did you write this code yourself? Because if you did it should be obvious which constructor to use. And you need to assign the passed value of airlock to the instance field in the class.

    Regards,
    Jim
    Last edited by jim829; 04-08-2014 at 12:27 AM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    jamesreno is offline Member
    Join Date
    Apr 2014
    Posts
    12
    Rep Power
    0

    Default Re: Code compiles but get null pointer exeption

    My friend helped me with some of his code as im a newbie at coding so i dont know what goes into this could you be of assistance

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,038
    Rep Power
    6

    Default Re: Code compiles but get null pointer exeption

    Quote Originally Posted by jamesreno View Post
    My friend helped me with some of his code as im a newbie at coding so i dont know what goes into this could you be of assistance
    Well, if you are someone who is new to Java then I recommend that you start slowly. I can also tell you that the approach you are taking with the threads is not sound. If you want to grow your own threads then you need an understanding of wait, notify, and object locking. Creating and using your own threads is relatively advanced. I recommend you focus on the more basic stuff. You may want to check out the Java tutorials in my signature.

    Regards,
    Jim
    Last edited by jim829; 04-08-2014 at 03:16 AM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    jamesreno is offline Member
    Join Date
    Apr 2014
    Posts
    12
    Rep Power
    0

    Default Re: Code compiles but get null pointer exeption

    i have to do it is for a assignment so i would really appricate teh help

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Code compiles but get null pointer exeption

    Please edit your post and wrap your code with code tags:
    [code]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.

    What specific questions do you have?
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    jamesreno is offline Member
    Join Date
    Apr 2014
    Posts
    12
    Rep Power
    0

    Default Re: Code compiles but get null pointer exeption

    Java Code:
    package assignment2;
    
    public class AirLock {
        
        private boolean lockState = false;
       
        public AirLock(boolean newLock){
                lockState = newLock;
        }
    
        public boolean isLockState() {
                return lockState;
        }
    
        public void setLockState(boolean lockState) {
                this.lockState = lockState;
        }
    ______________________________________________________
    
    public class AirlockDoor implements Runnable{
    	
        private String name;
    	private AirLock airlock;
    	private String doorName;
    	
    	
    	public AirlockDoor(String name, AirLock airlock){
                this.doorName = name;
                this.airlock = airlock;
    
        }
    
        public AirlockDoor() {
                // TODO Auto-generated constructor stub
        }
    
       
        public void run() {
            while(true){
                synchronized(airlock){
                  System.out.println("" + name + "Open"); 
                              System.out.println("" + name + "Closed");
                        }
                }
        }
    
        public synchronized  void  requestToOpen() {
    
                System.out.println(doorName +  " requests to be opened");
               
               
    
        }
    
    }
    
    ___________________________________________________________________________-
    
    public class AstronautSimulator {
        
        public static void main(String [] args){
                simulate();
        }
    
        private static void simulate() {
                // Create an AirLock object
                // STEP 1: CREATE AN EMPTY VERSION OF THE AirlockDoor CLASS
        	class AirlockDoor{
        		
        	}
                AirlockDoor[] doors = new AirlockDoor[2];
               
                // For this prototype this can be an empty (no method)
                // class. It is a common object used in the Door class
                // that contains the single lock-key that protects the
                // imaginary airlock room from having two doors open at
                // the same time
                // STEP 2: CREATE THIS CLASS
                AirLock MainAirLock = new AirLock(false);
               
                // Give the doors a name (for debugging purposes) and
                // a common airlock object used to store the "key" used
                // by a critical section of code in the Door class
               
               
                doors[0] = new AirlockDoor("Door 1", MainAirLock);
                doors[1] = new AirlockDoor("Door 2", MainAirLock);
               
                // AirlockDoor implements Runnable and so make sure
                // we tie the objects to Thread objects
               
                Runnable threadJob = new AirlockDoor();
               
        // STEP 3: CREATE TWO THREADS FOR THE TWO DOORS AND START THEM
                // ENTER CODE HERE
               
                Thread doorController1 = new Thread(threadJob);
               
                doorController1.setName("airlock");
               
                doorController1.start();
               
                // Loop infinitely trying to open airlock doors
               
                while (true){
                        // random() returns a value in range 0.0..<1.0 multiplied by 10 and mod doors.length
                        // to return either 0 or 1 in order to randomly decide which door to request
                        int doorNum = (int)((Math.random() * 10) % doors.length);
                       
                        // STEP 4: MAKE A REQUEST TO OPEN THE DOOR AND THEN ADD A DELAY BEFORE
                        // TRYING TO OPEN ANOTHER DOOR
                        // ENTER CODE HERE
                       
                        doors[doorNum].requestToOpen();
                       
                       
                        try{
                                Thread.sleep(800);
                        }
                        catch(InterruptedException ex){
                                ex.printStackTrace();
                        }
                       
                       
                       
                }
        }
    }

  10. #10
    jamesreno is offline Member
    Join Date
    Apr 2014
    Posts
    12
    Rep Power
    0

    Default Re: Code compiles but get null pointer exeption

    Exception in thread "main" java.lang.Error: Unresolved compilation problems:
    The constructor AirlockDoor(String, AirLock) is undefined
    The constructor AirlockDoor(String, AirLock) is undefined
    Type mismatch: cannot convert from AirlockDoor to Runnable
    The method requestToOpen() is undefined for the type AirlockDoor

    at assignment2.AstronautSimulator.simulate(AstronautS imulator.java:30)
    at assignment2.AstronautSimulator.main(AstronautSimul ator.java:6)

    im just not sure what i have to do next i know i need to do what jim said but im not sure how to go about it

  11. #11
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,038
    Rep Power
    6

    Default Re: Code compiles but get null pointer exeption

    First, when is your assignment due? What methods and classes, if any, are you required to use?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  12. #12
    jamesreno is offline Member
    Join Date
    Apr 2014
    Posts
    12
    Rep Power
    0

    Default Re: Code compiles but get null pointer exeption

    AirlockDoor class , e constructor that takes a String and an Airlock , an empty Airlock class , two thread objects

  13. #13
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,038
    Rep Power
    6

    Default Re: Code compiles but get null pointer exeption

    Okay, I am having a difficult time figuring out your design. You have a tight loop in the run method which doesn't do much. But here is one approach.

    Have two threads of an AirLockDoor.
    Each thread shares a boolean value which indicates the door is open.
    When one thread opens a door, it sets the boolean to true.
    When this value is true, the other door cannot be opened.
    After a while, the open door closes and sets the value to false.
    Then the other door may open.

    Those methods must be synchronized so they can't open both doors at the same time.

    The problem with this approach is that a single door may open and close several times before the
    other door gets a chance. So you would need to add additional checks to ensure a door can't
    be opened again until the other door has had a chance.

    You won't need notify and wait. Just Thread.sleep and some boolean values.
    That's the best I can offer at this time.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  14. #14
    jamesreno is offline Member
    Join Date
    Apr 2014
    Posts
    12
    Rep Power
    0

    Default Re: Code compiles but get null pointer exeption

    Task 2: Look at the existing code and fill in the blanks
    Examine the Door interface. Then take a look at the AstronautSimulator class. This is the one
    you need to update. It will not compile because you will need to create an AirlockDoor class and
    an Airlock Class. The following steps you need to follow are also listed in the
    AstronautSimulator class:

    Step 1: Create an empty AirlockDoor class. Note that you will also need to create an
    appropriate constructor that takes a String and an Airlock.

    Step 2: Create an empty Airlock class. This one can remain empty for the reasons discussed in
    the code comments.

    Step 3: Create two thread objects using the Thread class: one for each of the two doors. Also
    start the threads. You will need to do something to the AirlockDoor class, so that your code
    compiles. Just do enough to get it to compile.

    Step 4: Make a request to open a door (hint: see the interface) and then add an 800 millisecond
    delay before trying to open another door. You will need to add a stubbed out method (empty
    and will need implementing in Task 3) to the AirlockDoor class.

  15. #15
    jamesreno is offline Member
    Join Date
    Apr 2014
    Posts
    12
    Rep Power
    0

    Default Re: Code compiles but get null pointer exeption

    would you be able to help with some of the code just so i can get on the right track as i have very limited time
    Last edited by jamesreno; 04-08-2014 at 04:31 AM.

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Code compiles but get null pointer exeption

    Most of us are here to help you design and write code and get it to work, not to write code.

    Make a list of the problems you are trying to solve, start at the top of the list and work on that one.
    When it compiles, executes and tests OK, move to the next one.

    What's the next item on the list you are working on?
    If you don't understand my response, don't ignore it, ask a question.

  17. #17
    jamesreno is offline Member
    Join Date
    Apr 2014
    Posts
    12
    Rep Power
    0

    Default Re: Code compiles but get null pointer exeption

    stuck on this part

    Step 2: You will also need to implement the constructor. Simply store the parameter values in
    instance variables. These instance variables will be shared by threads. Refer back to the slides
    and lecture examples to see what implication this has on the way you should declare these
    instance variables.



    my lockoperator and doorName just shows null on my debugger

  18. #18
    jamesreno is offline Member
    Join Date
    Apr 2014
    Posts
    12
    Rep Power
    0

    Default Re: Code compiles but get null pointer exeption

    Java Code:
    /**
     * Simulates astronauts trying to open spaceship airlock doors
     * @author cwl
     *
     */
    public class AstronautSimulator {
    	 
    	public static void main(String [] args){
    		simulate();
    	}
    
    	private static void simulate() {
    		// Create an AirLock object
    		// STEP 1: CREATE AN EMPTY VERSION OF THE AirlockDoor CLASS
    		class AirlockDoor{
    		  
    		  private String name;
    		  private AirLock airLock;
    		  
    		    
    		  public AirlockDoor(String name ,AirLock airlock){
    		     this.name = name;
    		     airLock= new AirLock();
    		  
    		  }
    		    
    		  }
    		AirlockDoor[] doors = new AirlockDoor[2];
    		
    		// For this prototype this can be an empty (no method)
    		// class. It is a common object used in the Door class
    		// that contains the single lock-key that protects the
    		// imaginary airlock room from having two doors open at
    		// the same time
    		// STEP 2: CREATE THIS CLASS
    		class Airlock{
    		    
    		  }
    		AirLock airLock = new AirLock();
    		
    		// Give the doors a name (for debugging purposes) and
    		// a common airlock object used to store the "key" used
    		// by a critical section of code in the Door class
    		doors[0] = new AirlockDoor("1234", airLock);
    		doors[1] = new AirlockDoor("1235", airLock);
    		
    		// AirlockDoor implements Runnable and so make sure
    		// we tie the objects to Thread objects
            // STEP 3: CREATE TWO THREADS FOR THE TWO DOORS AND START THEM
    		// ENTER CODE HERE
            Thread doorController1 = new Thread();
    		Thread doorController2 = new Thread();
    		
    		doorController1.start();
    		doorController2.start();
    		
    		// Loop infinitely trying to open airlock doors
    		while (true){
    			// random() returns a value in range 0.0..<1.0 multiplied by 10 and mod doors.length
    			// to return either 0 or 1 in order to randomly decide which door to request
    			int door = (int)((Math.random() * 10) % doors.length);
    			
    			// STEP 4: MAKE A REQUEST TO OPEN THE DOOR AND THEN ADD A DELAY BEFORE
    			// TRYING TO OPEN ANOTHER DOOR
    			// ENTER CODE HERE
    		}
    	}
    }
    ______________________________________________________________________________________________
    /**
     * Write a description of class AirLock here.
     * 
     * @author (your name) 
     * @version (a version number or a date)
     */
    public class AirLock {
        
        
        
        
    }
    _______________________________________________________________________
    /**
     * Represents a door. Door are automatic and will open for a short time and then
     * close again automatically.
     * @author cwl
     *
     */
    public abstract interface Door {
    	/**
    	 * Make a request to open the door. The door will in time open.
    	 * How long this takes depends on the implementation
    	 */
    	public void requestToOpen();
    }

    ok so ive done
    Step 1: Create an empty AirlockDoor class. Note that you will also need to create an
    appropriate constructor that takes a String and an Airlock.

    Step 2: Create an empty Airlock class. This one can remain empty for the reasons discussed in
    the code comments.

    Step 3: Create two thread objects using the Thread class: one for each of the two doors. Also
    start the threads. You will need to do something to the AirlockDoor class, so that your code
    compiles. Just do enough to get it to compile.

    now im at this :

    Step 4: Make a request to open a door (hint: see the interface) and then add an 800 millisecond
    delay before trying to open another door. You will need to add a stubbed out method (empty
    and will need implementing in Task 3) to the AirlockDoor class.

    ?????????????

    what do i do here im worried

Similar Threads

  1. Replies: 1
    Last Post: 03-19-2012, 01:50 PM
  2. Replies: 8
    Last Post: 03-11-2012, 05:28 PM
  3. null pointer exeption : how solve it ?
    By miki_ir in forum New To Java
    Replies: 11
    Last Post: 09-05-2011, 03:38 AM
  4. Null Pointer Exeption
    By Aldane in forum AWT / Swing
    Replies: 3
    Last Post: 03-19-2011, 11:11 AM
  5. null pointer exeption
    By mj23brm in forum New To Java
    Replies: 4
    Last Post: 02-11-2009, 12:50 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
  •