Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Complies but shows null expection on console

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Complies but shows null expection on console

    okay i'm trying to stop the console displaying a null expection but i'm not sure why its doing it i was wondering if you guys could help me


    package  miniass3;
    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;
            }
     
    }

    package miniass3;
    public class AirlockDoor implements Runnable {
     
            private AirLock lockOperator;
            private String doorName;
     
            public AirlockDoor(String name, AirLock airLock){
                    doorName = name;
     
            }
     
            public AirlockDoor() { }
     
            public void run() {    
                    while (true){
                            if(lockOperator.isLockState() == false){
                                    System.out.println("False");
                            }
                    }
            }
     
            public synchronized  void  requestToOpen() {
     
                    System.out.println(doorName +  " requests to be opened");
            }
         }

    package uk.ac.aber.dcs.airlock;
     
     
    import miniass3.AirLock;
    import miniass3.AirlockDoor;
     
    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);
                    Thread doorController2 = new Thread(threadJob);
     
                    doorController1.setName("DoorLock");
                   doorController2.setName("DoorLock");
     
                    doorController1.start();
                   doorController2.start();
     
                    // Loop infinitely which try's to open the 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
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Complies but shows null expection on console

    NullPointer is a runtime exception. Runtime exceptions are not caught by the compiler, because they only occur during program runtime (the compiler doesn't run the code to make sure it works, it just checks your syntax).
    NullPointer indicates you forgot to set a variable, but you are attempting to use that variable to call a method. Provide the full stack-trace message of the exception. The stack-trace should indicate which line the exception is occurring on. When we look at the line of code, we should be able to figure out which variable has a null value.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

Similar Threads

  1. button doesnt shows up on the screen
    By poldz123 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 20th, 2013, 07:35 AM
  2. Mutiple classfile, JPanel nothing shows up
    By Lorack in forum AWT / Java Swing
    Replies: 71
    Last Post: June 25th, 2012, 02:58 PM
  3. Table with CustomModel when clicked shows old entry
    By Nesh108 in forum AWT / Java Swing
    Replies: 6
    Last Post: November 9th, 2011, 06:38 AM
  4. Replies: 2
    Last Post: January 7th, 2011, 09:10 PM
  5. Struts Application, Complies perfectly but error at run time
    By Prarthana in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 18th, 2010, 01:49 AM