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 4 of 4

Thread: Null Pointer Exception error

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Null Pointer Exception error

    Sorry if this looks bad not too concerned with efficiency right now, getting null pointer
    exception after while loop in constructor. Reading in from a file and setting values that are checked in the constructor. Commented problem area in caps.Also its not actually
    finished i'm just testiing as I go. Now it is part of a project but I don't expect any more than simply pointing me in the right direction to fix this problem.
    Updated exception occurs at bolded area no matter what the input. Compiler error java.lang.NullPointerException:null.

    import java .util.Scanner ;
    import java .io.*;
    import java .util.ArrayList;
    public class Room
    {
        Scanner s = new Scanner(System.in);
        Scanner file = new Scanner("rooms.txt");
        private String hotelType;
        private String roomType;
        private String input;
        private int numOfRooms= 0;
        private int occNoMin = 0;
        private int occNoMax= 0 ;
        private int[] rates = new int[] {50,65,70,75,80,85,90,100};
        //ArrayList<String> roomFile = new ArrayList<String>();
        private  boolean cont = false;
     
        public Room()
        {
            System.out.println("Select hotel type -> eg.3-star - 5-star");
            input = s.next();
     
            while(cont != true)
            {
               // input = s.next();
                if(input.compareToIgnoreCase("3-star") == 0)
                {
                    setHotelType();
                    cont = true;
                }
                if(input.compareToIgnoreCase("4-star") == 0)
                {
                    setHotelType();
                    cont = true;
                }
                if(input.compareToIgnoreCase("5-star") == 0)
                {
                    setHotelType();
                    cont = true;
                }
                else
                {
                    System.out.println("Not a known hotel star level");
                    cont = true;
                }
            }
            [B]if(hotelType.compareToIgnoreCase("3-star") == 0)[/B] // NULL POINTER EXCEPTIONS OCCUR HERE DEPENDING ON INPUT
            {
                System.out.println("Select room type -> Classic  Double/Single/Twin/Family");//Deluxe/Executive
            }  
            else if(hotelType.compareToIgnoreCase("4-star") == 0)
            {
                System.out.println("Select room type -> Deluxe  Double/Single/Twin/Family");
            }
            else if(hotelType.compareToIgnoreCase("5-star") == 0)
            {
                System.out.println("Select room type -> Executive  Double/Single/Twin/Family");
            }
     
            input = s.next();
            setRoomType();
     
        }
     
        public void setHotelType()
        {
            String hotelCheck;
            ArrayList<String> arr = new ArrayList<String>();
            while(file.hasNext())
            {
                file.useDelimiter(",");
                hotelCheck = file.next() ;
                //hotelCheck = hotelCheck.substring(0,hotelCheck.length()-1);
                if(hotelCheck.compareToIgnoreCase("3-star")==0)
                {
                    hotelType = hotelCheck;
                }
                else  if(hotelCheck.compareToIgnoreCase("4-star")==0)
                {
                    hotelType = hotelCheck;
                }
                else  if(hotelCheck.compareToIgnoreCase("5-star")==0)
                {
                    hotelType = hotelCheck;
                }
     
                //hotelCheck.split(",");
                //arr = hotelCheck;
                //for(int i= 0; i<hotelCheck.length();i ++)
                //{
                //  if(hotelCheck[i].compareToIgnoreCase(input) ==0)
                //{
                // hotelCheck[i] = hotelType;
                //}
                //}
            }
     
        }
     
        public void setRoomType()
        {
            String roomCheck;
            while(file.hasNext())
            {
                file.useDelimiter(",");
                roomCheck = file.next() ;
     
                if(roomCheck.compareToIgnoreCase("Single")==0)
                {
                    roomType = roomCheck;
                }
                if(roomCheck.compareToIgnoreCase("Twin")==0)
                {
                    roomType = roomCheck;
                }
                if(roomCheck.compareToIgnoreCase("Double")==0)
                {
                    roomType = roomCheck;
                }
                if(roomCheck.compareToIgnoreCase("Family")==0)
                {
                    roomType = roomCheck;
                }
     
            }
        }
     
        public String getHotel()
        {
            return hotelType;
        }
     
        public String getRoom()
        {
            return roomType;
        }
     
        public int getNumRooms()
        {
            return numOfRooms;
        }
     
        public int getMinOcc()
        {
            return occNoMin;
        }
     
        public int getMaxOcc()
        {
            return occNoMax;
        }
     
     
    }
    Last edited by Keitho55; November 8th, 2011 at 05:37 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Null Pointer Exception error

    You need to post the full text of the error message. It shows what statement the error occurred on.

    You should look at the statement with the error and see what variable is null. Then look back in your code to see why that variable does not have a valid value.

  3. #3
    Junior Member
    Join Date
    Nov 2011
    Location
    china
    Posts
    12
    My Mood
    Sad
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Null Pointer Exception error

    String不是基本数据类型,在初始化的时候,指向的null,当你调用null的任何方法的时候,就会 报空指针一场

    所以,private String hotelType="";
    -----------------------------------------------------------------------------------------------------

    google translate:

    String is not a basic data type, in the initialization time, pointing to the null, null when you call any method, it reported a NULL pointer
    so ,private String hotelType="";

  4. #4
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Null Pointer Exception error

    Exception clearly identifies the problem. Your hotelType is a String object and you must check if it's not null. And you are trying to do
    null.compareToIgnoreCase("3-star")
    What do you expect a null to do?

Similar Threads

  1. NULL POINTER EXCEPTION error
    By beefwithrice in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 28th, 2011, 06:26 AM
  2. Null Pointer Exception Help !!
    By AlterEgo1234 in forum Member Introductions
    Replies: 1
    Last Post: March 27th, 2011, 10:07 AM
  3. [SOLVED] Null Pointer Exception
    By musasabi in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 11th, 2010, 09:25 PM
  4. JComboBox null pointer Exception error
    By F_Arches in forum AWT / Java Swing
    Replies: 2
    Last Post: November 29th, 2009, 02:32 PM
  5. Getting Null Pointer Exception in runtime
    By RoadRunner in forum Exceptions
    Replies: 1
    Last Post: April 26th, 2009, 01:21 PM