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.
Code :
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;
}
}
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.
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="";
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
Code :
null.compareToIgnoreCase("3-star")
What do you expect a null to do?