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